这篇文件介绍如何构建go语言版gRPC服务端。
环境配置
安装protobuf编译工具(参考https://github.com/google/protobuf/releases)
$ wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-cpp-3.5.1.zip
$ unzip protobuf-cpp-3.5.1.zip
####参照protobuf-3.5.1/src/README.md
$ sudo apt-get install autoconf automake libtool curl make g++ unzip
$ cd protobuf-3.5.1
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.
安装protoc-gen-go
$ go get -u github.com/golang/protobuf/protoc-gen-go
$ cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go
$ go build
$ go install
编写代码
参考:
结构如下https://github.com/grpc/grpc-go/tree/master/examples/helloworld
gRPC-test/
├── main.go
└── proto-src
├── helloworld.pb.go
└── helloworld.proto
先看看helloworld.proto文件,这个文件是从https://github.com/grpc/grpc-go/tree/master/examples/helloworld/helloworld里面抄过来的。
// Copyright 2015 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. syntax = "proto3"; option java_multiple_files = true; option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
在文件目录下执行
$ protoc -I ./ --go_out=plugins=grpc:. ./helloworld.proto
我们就可以看到生成的helloworld.pb.go文件。这个文件是自动生成的,不用做任何改动。
然后我们就可以编写main.go文件了(参考https://github.com/grpc/grpc-go/tree/master/examples/helloworld/greeter_server),如下
/* * * Copyright 2015 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ //go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto package main import ( "log" "net" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/reflection" pb "./proto-src" ) const ( port = ":50051" ) // server is used to implement helloworld.GreeterServer. type server struct{} // SayHello implements helloworld.GreeterServer func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{Message: "this is fuck test ---> Hello " + in.Name}, nil } func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) // Register reflection service on gRPC server. reflection.Register(s) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } }
执行,发现有几个库死活找不着,这是因为Google在中国被屏蔽。
如果报cannot find package "golang.org/x/***",那就在$GOPATH/src/golang.org/x/下执行
$ git clone https://github.com/golang/***.git
如果报cannot find package "google.golang.org/genproto/***",那就在$GOPATH/src/google.golang.org/下执行
$ git clone https://github.com/google/go-genproto.git,并把go-genproto文件夹名称改为genproto
如果报cannot find package "google.golang.org/grpc/***",那就在$GOPATH/src/google.golang.org/下执行
$ git clone https://github.com/grpc/grpc-go.git,并把grpc-go文件夹名称改为grpc