Make a deal with user information Micro Service
The client user name, users can query the basic information from the server
GRPC proto user.proto defined client requests, the server response data format user.pb.go automatically generated, data exchange function provides server.go micro-services server client.go micro service client
1. Preparation of proto file
// version syntax = "proto3"; // the specified package name generated user.pb.go package proto; data format requested by the client is defined // Message UserRequest { // definition request parameter String name =. 1; } // definition of the service end of the respective amount of data format Message UserResponse { // definition of response parameters Int32 ID =. 1; String name = 2; Int32 Age =. 3; // field modifier // repeated represents a variable array, similar to the slice type repeated string =. 4 Hobby; } // corresponds to the interface scale // service defined service called open -service UserInfoService { // equivalent methods within the interface // definition request parameters UserRequest, response parameters of UserResponse RPC GetUserInfo (UserRequest) Returns (UserResponse ) {} }
2. Generate .go file
- pycharm open command, the input interface file command to generate:
protoc -I . --go_out=plugins=grpc:. ./user.proto
3. Write the server
main Package Import ( "context" "FMT" Pb "Micro-Go / Micro-My / GRPC / proto" "google.golang.org/grpc" "NET" ) // defines the server interface to achieve the agreed type UserInfoService struct { } var UserInfoService U = {} // server need to implement an interface FUNC (S * UserInfoService) GetUserInfo (CTX context.Context, pb.UserRequest REQ *) (* pb.UserResponse RESP, ERR error) { name: REQ = .name // check the user information database IF name == "zhangsan" { RESP & pb.UserResponse = { Id:. 1, the name: name, Age: 22 is, // field slice Hobby: [] string { "Sing ", " run "," basketball "}, } } err = nil return } FUNC main () { // listener 1. addr: = "127.0.0.1:8080" LIS, ERR: = net.Listen ( "TCP", addr) IF ERR = nil {! Fmt.Printf ( "monitor Exception: S% \ n-", ERR) } fmt.Printf (" begins listening:% S \ n-", addr) // 2. examples of GRPC S: = grpc.NewServer () // 3. GRPC services registered on the micro- // the second parameter type required Interface type variable pb.RegisterUserInfoServiceServer (S, & U) // 4. start gRPC service s.Serve (LIS) }
4. Write the client
main Package Import ( "context" "FMT" Pb "Micro-Go / Micro-My / GRPC / proto" "google.golang.org/grpc" ) FUNC main () { // create GRPC 1. server connected conn , ERR: = grpc.Dial ( "127.0.0.1:8080", grpc.WithInsecure ()) ! = nil {IF ERR fmt.Printf ( "connection exception:% S \ n-", ERR) } the defer conn.Close ( ) // instantiate gRPC client 2. client: = pb.NewUserInfoServiceClient (Conn) // 3. assembly parameter REQ: = new new (pb.UserRequest) req.Name = "zhangsan" // call interface 4. resp, err : = client.GetUserInfo (context.Background (), REQ) IF ERR = nil {! fmt.Printf ( "response to an exception:% s \ n",err)err) } ( ":% v \ n response result", RESP) fmt.Printf }