https://github.com/zq2599/blog_demos
內(nèi)容:所有原創(chuàng)文章分類和匯總,及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;
[golang@centos7 src]$ tree helloworld/helloworld/├── gateway│ └── helloworld.gw.go├── helloworld.pb.go├── helloworld.pb.gw.go├── helloworld.proto├── helloworld.swagger.json└── server └── server.go
curl -o install-grpc-gateway.sh \https://raw.githubusercontent.com/zq2599/blog_demos/master/files/install-grpc-gateway.sh \&& chmod a+x ./install-grpc-gateway.sh \&& ./install-grpc-gateway.sh
[golang@centos7 ~]$ cd $GOPATH/bin[golang@centos7 bin]$ ls -al總用量 26708drwxrwxr-x. 2 golang golang 98 12月 19 08:59 .drwxrwxr-x. 5 golang golang 39 12月 19 08:21 ..-rwxr-x---. 1 golang golang 5253272 12月 19 08:20 protoc-rwxrwxr-x. 1 golang golang 8461147 12月 19 08:21 protoc-gen-go-rwxrwxr-x. 1 golang golang 6717463 12月 19 08:59 protoc-gen-grpc-gateway-rwxrwxr-x. 1 golang golang 6908535 12月 19 08:59 protoc-gen-swagger
// 協(xié)議類型syntax = 'proto3';// 包名package helloworld;import 'google/api/annotations.proto';// 定義的服務(wù)名service Greeter { // 具體的遠(yuǎn)程服務(wù)方法 rpc SayHello (HelloRequest) returns (HelloReply) { option (google.api.http) = { post: '/helloworld' body: '*' }; }}// SayHello方法的入?yún)?,只有一個字符串字段message HelloRequest { string name = 1;}// SayHello方法的返回值,只有一個字符串字段message HelloReply { string message = 1;}
protoc -I. \-I$GOPATH/src \-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \--go_out=plugins=grpc:. \helloworld.proto
protoc -I. \-I$GOPATH/src \-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \--grpc-gateway_out=logtostderr=true:. \helloworld.proto
protoc -I. \-I$GOPATH/src \-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \--swagger_out=logtostderr=true:. \helloworld.proto
[golang@centos7 src]$ tree helloworld/helloworld/├── helloworld.pb.go├── helloworld.pb.gw.go├── helloworld.proto└── helloworld.swagger.json0 directories, 4 files
package mainimport ('context''log''net''google.golang.org/grpc'pb 'helloworld')const (port = ':50051')// 定義結(jié)構(gòu)體,在調(diào)用注冊api的時候作為入?yún)ⅲ?/ 該結(jié)構(gòu)體會帶上SayHello方法,里面是業(yè)務(wù)代碼// 這樣遠(yuǎn)程調(diào)用時就執(zhí)行了業(yè)務(wù)代碼了type server struct {// pb.go中自動生成的,是個空結(jié)構(gòu)體pb.UnimplementedGreeterServer}// 業(yè)務(wù)代碼在此寫,客戶端遠(yuǎn)程調(diào)用SayHello時,// 會執(zhí)行這里的代碼func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {// 打印請求參數(shù)log.Printf('Received: %v', in.GetName())// 實例化結(jié)構(gòu)體HelloReply,作為返回值return &pb.HelloReply{Message: 'Hello ' + in.GetName()}, nil}func main() {// 要監(jiān)聽的協(xié)議和端口lis, err := net.Listen('tcp', port)if err != nil {log.Fatalf('failed to listen: %v', err)}// 實例化gRPC server結(jié)構(gòu)體s := grpc.NewServer()// 服務(wù)注冊pb.RegisterGreeterServer(s, &server{})log.Println('開始監(jiān)聽,等待遠(yuǎn)程調(diào)用...')if err := s.Serve(lis); err != nil {log.Fatalf('failed to serve: %v', err)}}
[golang@centos7 server]$ go run server.go 2020/12/13 08:20:32 開始監(jiān)聽,等待遠(yuǎn)程調(diào)用...
package mainimport ('flag''fmt''net/http'gw 'helloworld''github.com/grpc-ecosystem/grpc-gateway/runtime''golang.org/x/net/context''google.golang.org/grpc')var (echoEndpoint = flag.String('echo_endpoint', 'localhost:50051', 'endpoint of YourService'))func run() error {ctx := context.Background()ctx, cancel := context.WithCancel(ctx)defer cancel()mux := runtime.NewServeMux()opts := []grpc.DialOption{grpc.WithInsecure()}err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)if err != nil {return err}return http.ListenAndServe(':9090', mux)}func main() {if err := run(); err != nil {fmt.Print(err.Error())}}
curl \-X POST \-d '{'name': 'will'}' \192.168.133.203:9090/helloworld
{'message':'Hello will'}
[golang@centos7 server]$ go run server.go 2020/12/19 14:16:47 開始監(jiān)聽,等待遠(yuǎn)程調(diào)用...2020/12/19 14:24:35 Received: will