This is a creation in Article, where the information may have evolved or changed.
Official website
http://www.grpc.io/
Http://www.grpc.io/docs/quickstart/go.html
GRPC Installation
Go Installation
Currently GRPC needs to be supported on go 1.5 or higher. Go installation can refer to: http://www.cnblogs.com/heartchord/p/5127503.html.
Protocol Buffer Installation
Protocol buffer and Go plug-in installation can refer to: http://www.cnblogs.com/heartchord/p/5337863.html.
GRPC Installation
Go get Google.golang.org/grpc
• Official Example Test
The GRPC package comes with a test example, and the path is google.golang.org/grpc/examples, and we use the HelloWorld project under the example to test it.
To compile the server and client:
Go install Google.golang.org\grpc\examples\helloworld\greeter_servergo Install google.golang.org\grpc\examples\ Helloworld\greeter_client
Turn on the two consoles to run Greeter_server.exe and Greeter_client.exe separately:
HelloWorld Engineering Analysis
.proto and. pb.go files
Using Protobuf you will know that no matter what project requires a. proto file to define the structure of the data, the file is Protoc.exe output to get the corresponding language source code files, for example, C + + get xxxx.pb.h and xxxx.pb.cc files. For the Golang, you need the above plug-in support, corresponding to get the Xxxx.pb.go file.
Official documents:
Https://developers.google.com/protocol-buffers/docs/proto3
Https://developers.google.com/protocol-buffers/docs/gotutorial
The Hellorequest and helloreply messages in the proto file are converted to the go language structure and some member functions are added automatically.
The Greeter service in the proto file will automatically generate Greeterclient and Greeterserver interfaces, where Greeterclient will have an internal type of greeterclient, Greeterclient defines the specific behavior of SayHello, while SayHello in greeterserver needs to be defined by itself.
The core point of SayHello specific behavior defined by Greeterclient is the SayHello function that invokes the server:
The service side needs to define the specific behavior of the SayHello:
pb.go File Generation command
// CD $GOPATH/src/google.golang.org/grpc/examples/helloworldprotoc-i helloworld/helloworld/ Helloworld.proto--go_out=plugins=grpc:helloworld