Summary
gRPC uses protocol buffers as IDL (Interface Definition Language), which provides a cross-platform, cross-language RPC (Remote Procedure Call) mechanism, this paper introduces the basic use of gRPC through an example program.
Preconditions
-
Go Version 1.6 and above ( Delivery)
$ go version
-
Install gRPC
$ go get -u google.golang.org/grpc
-
Install Protocol Buffers v3
The Protocol compiler is used to generate gRPC service code.Download the zip file package to install it Delivery.
- Unzip the zip file
- Add binary file (bin/protoc) to path environment variable
Next install the Go plug-in for protoc:
$ go get -u github.com/golang/protobuf/protoc-gen-go
The protoc-gen-go compiler plug-in is installed in $GOBIN and the variable is defined as $GOPATH/bin. protocol, protoc-related commands must exist in the $ PATH environment variable.
$ export PATH=$PATH:$GOPATH/bin
Download Code
Download gRPC Sample Code Library GIthub The sample code is located in the $GOPATH/src/google.golang.org/grpc/examples directory.
$ go get google.golang.org/grpc #Or download it from github $ go get github.com/grpc/grpc
Build program
#Switch to Sample Code Directory $ cd $GOPATH/src/google.golang.org/grpc/examples
The.proto file defines the gRPC service and is also used to generate the corresponding.pb.go file.The.pb.go file is generated from.proto using the protoc compiler.
For the sample program, the helloworld.pb.go file has been generated ahead of time (as defined by helloworld.proto) at $GOPATH/src/google.golang.org/grpc/examples/helloworld/helloworld
The file helloworld.pb.go contains the following:
- Generated client and server code
- Generate defined HelloRequest and HelloReply type operation code
Run the program
Run with the go run directive.
Run the server:
$ go run greeter_server/main.go
Run client (new terminal):
$ go run greeter_client/main.go
If you print Greeting: Hello world, the client-server based gRPC application is running successfully.
Modify Program
Objective: To add a new method (SayHelloAgain) to the server for client invocation.The gRPC service causes the protocol to be defined; here address And this address More information on defining the.proto file can be found.Now all you need to know is that there is a SayHello method on both the server and the client "stub" that accepts the HelloRequest type parameter from the client and returns the HelloReply type result from the server with the following code:
// greeting service definition. service Greeter { // Send greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message body contains user's name. message HelloRequest { string name = 1; } // response message contains greetings message HelloReply { string message = 1; }
Add a SayHelloAgain method to Greeter to ensure that the current working directory is $GOPATH/src/google.golang.org/grpc/examples/helloworld.
Modify the helloworld/helloworld.proto file to add a SayHelloAgain method with HelloRequest and HelloReply types.The code is as follows:
// greeting service definition. service Greeter { // Send greeting rpc SayHello (HelloRequest) returns (HelloReply) {} // New SayHelloAgain rpc SayHelloAgain (HelloRequest) returns (HelloReply) {} } // The request message body contains user's name. message HelloRequest { string name = 1; } // response message contains greetings message HelloReply { string message = 1; }
Generate gRPC code
Now regenerate the gRPC code with the defined service.The working directory is ($GOPATH/src/google.golang.org/grpc/examples/helloworld).The instructions are as follows:
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
The file helloworld.pb.go is regenerated.
Modify & Rerun
Although server and client code has been regenerated, it is still necessary to modify some of the call code manually.
Modify server
Edit the file greeter_server/main.go and add the following code:
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{Message: "Hello again " + in.Name}, nil }
Modify client
Edit the file greeter_client/main.go and add the following code to the main function:
r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message)
Function
Run the server:
$ go run greeter_server/main.go
Run Client:
$ go run greeter_server/main.go
Expected results:
$ go run greeter_client/main.go Greeting: Hello world Greeting: Hello again world
Complete!