gRPC网关,在 grpc-gateway 基础上使用注册中心实现 API 的动态管理,而不是在编译时静态打包


Keywords
gateway, go-micro, grpc, grpc-gateway, registry
Install
go get github.com/hb-chen/gateway/v2

Documentation

Gateway

Helm install

helm upgrade --install grpc-gateway ./helm --namespace grpc-gateway --no-hooks

Example

网关

$ cd cmd
$ go run main.go --grpc_registry=etcd --server_address=:8080

gRPC 服务

$ cd example/grpc
$ go run main.go --grpc_registry=etcd

go-micro 的 gRPC 服务

$ cd example/micro
$ go run main.go --grpc_registry=etcd

gRPC 服务支持:

  • Codec
    • import _ "github.com/hb-chen/gateway/v2/codec"
    • go-micro 使用 gRPC server 的 Codec() option grpc.Codec("application/grpc+"+jsonCodec.Name(), jsonCodec),
  • 注册中心

测试接口

# POST
curl -X POST -d '{"name":"hbchen"}' http://localhost:8080/v1/example/call
{"code":"0","msg":"Hello hbchen"}

# GET
curl http://localhost:8080/v1/example/call/hbchen
{"code":"0","msg":"Hello hbchen"}

Proto 工具

protoc-gen-hb-grpc protoc-gen-hb-grpc-gateway

$ go install github.com/hb-go/grpc-contrib/protoc-gen-hb-grpc
$ go install github.com/hb-chen/gateway/v2/protoc-gen-hb-grpc-gateway 
protoc --proto_path=.:$GOPATH/src \
--go_out=plugins=grpc:. \
--hb-grpc-gateway_out=logtostderr=true,grpc_api_configuration=example/proto/gateway.yaml:. \
--hb-grpc_out=plugins=desc+registry:. \
example/proto/service.proto

Ref