grpc of microservice component
summary
grpc is a high-performance, open source and general rpc framework. It is designed for mobile and http/2. It brings characteristics such as two-way flow, flow control, header compression, multiple multiplexing requests of single tcp connection and so on. In short, it can be considered as a better protocol than http.
rpc
- That is, the remote procedure call protocol requests from the remote computer through the network, which is similar to the web request, but the web request uses the http high-level protocol, while rpc mostly uses the tcp protocol, which is the network layer protocol, which reduces the packaging of some information and speeds up the speed of network processing. golang has its own rpc package, which can be used to build rpc services.
rpc usage instance
- Server
package rpc import ( "fmt" "net" "net/http" "net/rpc" ) type Panda int /* Methods can be exported Method must have two parameters, both of which are export type or built-in type The second argument to the method must be a pointer Method has only one return value, the error interface type */ // Getinfo function keyword (object) function name (send content to the opposite end and return content to the opposite end) return type func (this *Panda) Getinfo(argType int, replyType *int) error { fmt.Println("Print the content sent by the opposite port:", argType) *replyType = argType + 1 return nil } func Server() { // The server must register an object class and instantiate it as an object pd := new(Panda) rpc.Register(pd) // connect to a network rpc.HandleHTTP() listener, err := net.Listen("tcp",":8888") if err != nil { fmt.Println("Server startup failed err:",err) return } http.Serve(listener,nil) }
- client
package rpc import ( "fmt" "net/rpc" ) //rpc client func Client(){ //Establish a network connection cli, err := rpc.DialHTTP("tcp","127.0.0.1:8888") if err != nil { fmt.Println("Network connection failed...") return } var pd int err = cli.Call("Panda.Getinfo",1000,&pd) if err!=nil{ fmt.Println("Call failed...") return } fmt.Println("Call succeeded,pd:",pd) }
grpc
- In gprc, you can directly call the application method of an exception machine on the server side like calling a local object, which makes it easier to create distributed applications and services. Like other RPCs, it is also the same idea. Define a service, Specifies the method that it can call remotely (including parameters and return types), implement this interface on the server side, and run a grpc server to process the customer's call requests. The client side has a stub, which can be the same as the server. The grpc client and server can run and interact in a variety of environments. For example, you can create a grpc server in java, and you can use go , python, ruby, etc. create clients. At the same time, many google APIs will also have grpc version interfaces, which can quickly integrate google functions into applications.
- protobuf is used by default. Generally speaking, proto3 should be used. All languages within the scope of grpc can be used to avoid compatibility problems.
- Environment construction
https://blog.csdn.net/sitebus/article/details/107481874
- Code example
- protobuf.proto
syntax = "proto3" ; package gprc ; //Define service protocol -- go_ out=./ *. There will be no rpc service after proto compilation // Add plug-in protocol -- go_ out=plugins=grpc:./ *. proto service HelloServer{ // Greeting function rpc SayHello(HelloRequest)returns(HelloResponse){} // Name function rpc SayName(NameRequest)returns(NameResponse){} } message HelloRequest{ string name = 1; } message HelloResponse { string msg = 1; } message NameRequest{ string name = 1; } message NameResponse{ string msg = 1; }
- Server
package gprc import ( "context" "fmt" "google.golang.org/grpc" "net" pb "unioj/utils/future/gprc/protobuf" ) type server struct { } func (this *server)SayHello(ctx context.Context, in *pb.HelloRequest) (out *pb.HelloResponse,err error){ return &pb.HelloResponse{Msg: "hellooooooo"},err } func (this *server)SayName(ctx context.Context, in *pb.NameRequest) (out *pb.NameResponse,err error){ return &pb.NameResponse{Msg: in.Name+" good morning"},err } func Server() { //grpc listen,err := net.Listen("tcp",":8888") defer func(){ err := recover();if err!=nil{ fmt.Println("Network establishment failed",err) } }() if err != nil{ panic(err) } srv := grpc.NewServer() pb.RegisterHelloServerServer(srv,&server{}) err = srv.Serve(listen) if err != nil { panic(err) return } }
- client
package gprc import ( "context" "fmt" "google.golang.org/grpc" pb "unioj/utils/future/gprc/protobuf" ) func Client() { // Connect server conn, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure()) if err != nil { fmt.Println(err) return } defer conn.Close() cli := pb.NewHelloServerClient(conn) // Calling a function through a handle re,err := cli.SayHello(context.Background(),&pb.HelloRequest{Name: "ethan"}) if err != nil { fmt.Println(err) } fmt.Println("Call result:",re.GetMsg()) res,err := cli.SayName(context.Background(),&pb.NameRequest{ Name: "alban", }) fmt.Println("Second call result:",res.GetMsg()) }