Skip to content

opvexe/rpc

Repository files navigation

1.1 特别需要注意的几个点

安装grpc-gateway:

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

项目目录结构:

└── src
    └── grpc-helloworld-gateway
        ├── gateway
        │   └── main.go
        ├── greeter_server
        │   └── main.go
        └── helloworld
            ├── helloworld.pb.go
            ├── helloworld.pb.gw.go
            └── helloworld.proto

google/api文件:

$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api

将文件拷贝到当前目录下:

# 拷贝以下两个目录
annotations.proto
http.proto
# 编译google.api [包含annotations.pb.go,http.pb.go文件]
cd helloworld
protoc -I . --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. google/api/*.proto

==还需要注意一点===

==将生成好 [包含annotations.pb.go,http.pb.go文件]== 拷贝一份放在这里
GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api

生成pb文件:

cd src/grpc-helloworld-gateway
# 生成helloworld.pb.go
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:. \
helloworld/helloworld.proto 
# 生成helloworld.pb.gw.go
cd helloworld
protoc --grpc-gateway_out=logtostderr=true:. ./helloworld.proto
# 生成helloworld.swagger.json
cd src/grpc-helloworld-gateway

protoc -I/usr/local/include -I. \
-I$GOPATH/src  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--swagger_out=logtostderr=true:. \
helloworld/helloworld.proto

==这里需要注意一下== [grpc注册的端口地址一定要一致gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, "localhost:8181", opts)]

mux :=  runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
		opts := []grpc.DialOption{
			grpc.WithInsecure(),
			grpc.WithBlock(),
		}
		//这里特别注意,注册端口一定要与grpc 服务端口一致 net.Listen("tcp", "localhost:8181")
		err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, "localhost:8181", opts)
		if err!=nil {
			panic(err)
		}
		//cors := cors.AllowAll()
		//serverMux := http.NewServeMux()
		//serverMux.Handle("/", cors.Handler(grpcMux))
		 http.ListenAndServe("localhost:8182", mux)

1.2 Swagger

参考文献:https://segmentfault.com/a/1190000013513469

生成Swagger的说明文件:

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

下载Swagger UI文件:

下载地址:
https://github.com/swagger-api/swagger-ui
==将其`dist`目录下的所有文件拷贝到我们项目中的`$GOPATH/src/grpc-hello-world/third_party/swagger-ui`去==

Swagger UI转换为Go源代码:

go get -u github.com/jteeuwen/go-bindata/...

生成Go代码: [pkg/ui/data/swagger/datafile.go]创建夹会创建

go-bindata --nocompress -pkg swagger -o pkg/ui/data/swagger/datafile.go third_party/swagger-ui/...

检查:回到pkg/ui/data/swagger目录,检查是否存在datafile.go文件

Swagger UI文件服务器:

go get github.com/elazarl/go-bindata-assetfs/...

1.3 etcd集群的搭建

$ docker pull quay.io/coreos/etcd
$ docker-compose up
$ docker ps 
# 验证集群
$ curl -L http://127.0.0.1:32787/v2/members
$ curl -L http://127.0.0.1:32789/v2/members
$ curl -L http://127.0.0.1:32791/v2/members
# 也可以使用etcdctl:
$ docker exec -t etcd1 etcdctl member list

About

微服务 grpc,支持gateway http请求。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages