curl

用polygolt测试hello

echo '{"name": "lk"}' | java -jar polyglot.jar --command=call --endpoint=localhost:50051 --full_method=helloworld.Greeter/SayHelloAgain --proto_discovery_root=/home/jinlei1/ksyun/src/google.golang.org/grpc/examples/helloworld/helloworld

grpc-gateway-example:

echo '{"id": "9999999"}' | java -jar ~/polyglot.jar --command=call --endpoint=localhost:9099 --full_method=grpc.gateway.examples.examplepb.EchoService/Echo --proto_discovery_root=/home/jinlei1/ksyun/src/github.com/grpc-ecosystem/grpc-gateway/examples/examplepb --add_protoc_includes /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis,/home/jinlei1/tools/include,${HOME}/ksyun/src/github.com/grpc-ecosystem/grpc-gateway

test the gateway:

curl localhost:8080/v1/example/echo/1 -XPOST

具体实现

入参的 key 会做大小写校验,这个比较好:

➜  servant git:(master) ✗ curl 'localhost:9998/v1/example/echo?UniqueName=ccc&name=aa&app=app'

{"message":"Hello (*pb.HelloRequest)(0xc4201b2900)(name:\"aa\" UniqueName:\"ccc\" )\n"}%

proto文件:

52 // The request message containing the user's name.¬

53 message HelloRequest {¬

~ 54    string name = 1;¬

+ 55    int32 id = 2;¬

+ 56    string UniqueName = 3;¬

+ 57    string App = 4;¬

58 }¬

可见,入参顺序和pb顺序无关,app并没有传进去。很好

proto3 不支持require了。。需要应用去查必须传?郁闷。。

设置int,会自动检测int是否正确,传string会报错

将一个接口改为post只需改一下pb文件即可。好方便

repeated = array

可以满足kop的get和post

header

返回header

Grpc-Metadata-Bar: bar1

Grpc-Metadata-Foo: foo1

Trailer: Grpc-Trailer-Foo

Trailer: Grpc-Trailer-Bar

grpc-gateway

grpc

 0  0x0000000000b77513 in main.(*server).Form
    at ./servant-example.go:29
 1  0x00000000008c1c1a in newgit.op.ksyun.com/golang-vc/kls-pb.git/example._Example_Form_Handler.func1
    at /home/jinlei1/ksyun/src/newgit.op.ksyun.com/golang-vc/kls-pb.git/example/example.pb.go:145
 2  0x0000000000b73ddd in newgit.op.ksyun.com/golang-vc/servant%2egit.KopUnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/newgit.op.ksyun.com/golang-vc/servant.git/middleware.go:74
 3  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
 4  0x0000000000b75662 in newgit.op.ksyun.com/golang-vc/servant%2egit.RetUnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/newgit.op.ksyun.com/golang-vc/servant.git/middleware.go:212
 5  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
 6  0x0000000000b739c9 in newgit.op.ksyun.com/golang-vc/servant%2egit.HeaderUnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/newgit.op.ksyun.com/golang-vc/servant.git/middleware.go:42
 7  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
 8  0x0000000000b4ea6b in github.com/grpc-ecosystem/go-grpc-middleware/validator.UnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/validator/validator.go:26
 9  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
10  0x0000000000b4e658 in github.com/grpc-ecosystem/go-grpc-middleware/recovery.UnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/recovery/interceptors.go:25
11  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
12  0x0000000000b49601 in github.com/grpc-ecosystem/go-grpc-middleware/tags.UnaryServerInterceptor.func1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/tags/interceptors.go:21
13  0x0000000000b483e5 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2.1.1
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:31
14  0x0000000000b48783 in github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func2
    at /home/jinlei1/ksyun/src/github.com/grpc-ecosystem/go-grpc-middleware/chain.go:38
15  0x00000000008c0d76 in newgit.op.ksyun.com/golang-vc/kls-pb.git/example._Example_Form_Handler
    at /home/jinlei1/ksyun/src/newgit.op.ksyun.com/golang-vc/kls-pb.git/example/example.pb.go:147
16  0x000000000082f5ef in google.golang.org/grpc.(*Server).processUnaryRPC
    at /home/jinlei1/ksyun/src/google.golang.org/grpc/server.go:921
17  0x0000000000832aef in google.golang.org/grpc.(*Server).handleStream
    at /home/jinlei1/ksyun/src/google.golang.org/grpc/server.go:1143
18  0x000000000083e425 in google.golang.org/grpc.(*Server).serveStreams.func1.1
    at /home/jinlei1/ksyun/src/google.golang.org/grpc/server.go:638
19  0x000000000045e3e1 in runtime.goexit
    at /home/jinlei1/os/go/src/runtime/asm_amd64.s:2337

grpc-gateway get

github.com/grpc-ecosystem/grpc-gateway/runtime.PopulateQueryParameters(0x1011be0, 0xc42031e100, 0xc42033c3c0, 0x105f1a0, 0xc4202c78c0, 0x0)
newgit.op.ksyun.com/golang-vc/kls-pb.git/example.request_Example_Form_0(0x10129e0, 0xc42033c390, 0x1013d00, 0xc4202bbbc0, 0x1008720, 0xc42000ec90, 0xc42036e100, 0xc42033c2d0, 0x0, 0x4, ...)
newgit.op.ksyun.com/golang-vc/kls-pb.git/example.RegisterExampleHandlerClient.func1(0x1011aa0, 0xc420468000, 0xc42036e100, 0xc42033c2d0)
github.com/grpc-ecosystem/grpc-gateway/runtime.(*ServeMux).ServeHTTP(0xc42009f1d0, 0x1011aa0, 0xc420468000, 0xc42036e100)
net/http.(*ServeMux).ServeHTTP(0xc4202bbda0, 0x1011aa0, 0xc420468000, 0xc42036e100)
net/http.serverHandler.ServeHTTP(0xc4202ce4e0, 0x1011aa0, 0xc420468000, 0xc42036e100)
net/http.(*conn).serve(0xc42038e140, 0x1012920, 0xc420324140)
created by net/http.(*Server).Serve