A gRPC-demo that implements python as a client calling service

gRPC is a cross-language and platform high-performance RPC framework open sourced by Google.

Its main features are:

Use a transport protocol based on HTTP/2. Brings features such as bidirectional streaming, flow control, header compression, multiplexing requests over a single TCP connection, etc.
Use the data serialization protocol based on ProtoBuf3.x.
Can automatically generate code in multiple languages
​​In this project, I will demonstrate how to use gRPC to create a simple service, I will use Go as the server to provide the service, and Python3 as the client to call the service.

Here, the client sends a set of account and password to the server. After the server receives the account number and password, it will print it out, spliced ​​and Base64 encoded, and then return it to the client.

Now we will officially start.

1. Define the data format

First, we use protobuf to define the transmitted data format. Here we need to create the protobuf/auth.proto file and define the data format we want to transmit and return, as well as functions that can be called remotely. See the code here.

syntax = "proto3";

option go_package = "protobuf";

package auth;

// 定义客户端要发送的数据格式 
message Request {
    
    
  string username = 1;
  string password = 2;
}

// 定义服务端返回的数据格式
message Response {
    
    
  string result = 1;
}

// 定义可以被远程调用的函数
service AUTH {
    
    
  rpc AuthLogin (stream Request) returns (stream Response) {
    
    }
}

2. Automatically generate code

After defining the data format, we need to use protoc to automatically generate client and server code. Here you need to install protoc first.

Mac users can install directly with brew install protobuf.

1. First generate the code that Python3 needs to use.

Use the pip3 install -r requirements.txt command to install the packages required by Python3

python3 -m grpc_tools.protoc -I protobuf/ --python_out=./protobuf --grpc_python_out=./protobuf protobuf/auth.proto

This command will automatically generate the files auth_pb2.py and auth_pb2_grpc.py in the protobuf folder
2. To generate the server code that Go needs to use,
use go get -u google.golang.org/grpc to install the go grpc, due to the wall , we may need some other methods, see here.

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc 
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net 
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text 
go get -u github.com/golang/protobuf/{
    
    proto,protoc-gen-go} 
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto 

cd $GOPATH/src/ 
go install google.golang.org/grpc

protoc -I protobuf/ --go_out=plugins=grpc:protobuf/ protobuf/auth.proto

3. Write server and client code

Please see the server.go file for the Go server code.

Please see the client.py file for the Python client code.

4. Run the service

Start Server:

go run server.go

Start the client:

python3 client.py

It can be seen that the server receives the account password, splices it together, performs Base64 encoding, and then returns it to the client.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324341077&siteId=291194637