Author: Become a good little white

Github: this article Github source

CSDN: CSDN_Blog

Hobby: Americano More Ice!

Simple understanding of GO-GPRC

directory

  • directory
    • Firstly, What is gRPC?
    • Without further ado, you will know the Code
      • Configuring the Basic Environment
      • A straw boat borrows an arrow
        • Initialize the
        • After the operation
      • Update the gRPC service
        • Update the proto.
        • The update server
        • Update the client
        • Done, Run it!

Firstly, What is gRPC?

In gRPC, a client application can directly invoke methods on a server application on another machine as if it were a local object, making it easier for you to create distributed applications and services. Like many RPC systems, gRPC is based on the idea of defining services, specifying methods that can be called remotely with parameters and return types. On the server side, the server implements this interface and runs the gRPC server to handle client calls. On the client side, the client has a stub (called a client in some languages) that provides the same methods as the server.

Without further ado, you will know the Code

Configuring the Basic Environment

  • go
  • protocol buffer

To install two required dependencies, the dependency management I use is the Go mod

  1. Download two dependencies
$go get google.golang.org/protobuf/cmd/[email protected] $go get google.golang.org/grpc/cmd/[email protected]Copy the code
  1. Update yourPATHIn order toprotocThe compiler can find these plug-ins
$ export PATH="$PATH:$(go env GOPATH)/bin"
# Of course you can see your Gopath
$ go env GOPATH
Copy the code

A straw boat borrows an arrow

Git clone you, you give me finished

Borrow Code # Doc
$ git clone- b v1.35.0 https://github.com/grpc/grpc-go# Go to the sample list we got
$ cd grpc-go/examples/helloworld
# See what we have
$ ls
greeter_client greeter_server helloworld
Copy the code

Run the example, LET US SEE SEE

Initialize the

  • The Server (Server)
Start the server first
$ go run greeter_server/main.go
Copy the code
  • The Client (Client)
# Restart client
$ go run greeter_client/main.go
Copy the code

After the operation

To see what happens to both, you can parse the internal operating logic

  • On the server, create a gRPC service and wait for the client to connect
  • On the client, create a connection in safe mode => Client connects to server=> Invoke the server method
# server
$ go run greeter_server/main.go
2021/05/31 11:54:19 Received: world
# client
$ go run greeter_client/main.go
2021/05/31 11:54:19 Greeting: Hello world
Copy the code

Update the gRPC service

Update the proto.

Open our helloworld/helloworld. Proto, add one line of code

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Add this code
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}
Copy the code

Let’s to regenerate our gRPC code (the helloworld/helloworld. Pb. Go)

$ protoc --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    helloworld/helloworld.proto
Copy the code

After execution, will find that the helloworld/helloworld. Pb. Go and helloworld/helloworld_grpc pb. Go has been updated

The update server

Open helloWorld /greeter_server/main.go and add a method

func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	return &pb.HelloReply{Message: "Hello again" + in.GetName()}, nil
}
Copy the code

Update the client

Open helloWorld /greeter_client/main.go and add the call method

	r1, err := c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
	iferr ! =nil {
		log.Fatalf("could not greet: %v", err)
	}
Copy the code

Done, Run it!

# server
$ go run greeter_server/main.go
# client
$ go run greeter_client/main.go Dean
Copy the code

Take a look at the output

# server
2021/05/31 15:32:45 Received: Dean
# client
2021/05/31 15:32:45 Greeting: Hello Dean
2021/05/31 15:32:45 Greeting: Hello againDean
Copy the code

Conclusion: if encounter what question or suggestion, can leave a message comment directly! The author will reply immediately

If you feel small white this article is good or helpful to you, look forward to your one key three even 💫! ❤ ️ ni ~