introduce
This article describes how to use GRPC with Go as the client and Python as the server to communicate. I was wondering how to connect the two files until I realized that I only needed to compile the same proto file… 😓)
Realize the function
The Python implementation method f(name) returns “hello “+name, which is returned by the go call
Install the configuration
Go
- The personal configuration is Go 1.12, using go Mod project management
- Because some packages will be walled, all have to be configured
GOPROXY
I configured ali’sGOPROXY="https://mirrors.aliyun.com/goproxy/"
Install GRPC, Protobuf compiler and corresponding GO plugin
go get google.golang.org/grpc
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/proto-gen-go
Copy the code
Note: If you use the command line in the Goland compiler, you also need to configure the proxy
python3
Also install GRPC, Protobuf, etc
pip3 install grpcio
pip3 install protobuf
pip3 install grpcio-tools
Copy the code
start
I used the Goland compiler and then introduced the Python interpreter
The project structure
My first taste, may not standard, please correct
-project
-go
-main.go
-micro
-hello.proto
-python
-server.py
Copy the code
This is the directory and file that you need to create yourself. The go package is the GO code, micro is the microservice configuration file, and the Python package is the Python code
Micro package
Start by creating the proto file –hello.proto
syntax = "proto3"; // Select the version package micro; Service Greeter {RPC SayHello (HelloRequest) returns (HelloReply) {}} message HelloRequest {string name = 1; } message HelloReply { string msg = 1; }Copy the code
Note: Here package must be consistent with the current path
Proto syntax is not explained in detail, so you need to check it out for yourself
Compiling the proto file begins by moving the command line to the micro package, and then executing the go and Python compilation statements, respectively
Go :hello.proto is the file to be compiled. After compilation, the hello.pb.go file is generated in the current package
protoc --go_out=plugins=grpc:. hello.proto
Copy the code
Python: Two files hello_pb2.py and hello_pb2_grpc.py are generated when compiled
python3 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto
Copy the code
Once the files are successfully generated, you can write client and server code
The python server
Write the following code in the Python package server.py file
from concurrent import futures
import time
import grpc
from micro import hello_pb2
from micro import hello_pb2_grpc
class Greeter(hello_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return hello_pb2.HelloReply(msg = "hello "+request.name)
def serve(a):
server = grpc.server(futures.ThreadPoolExecutor(max_workers = 10))
hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)
server.add_insecure_port('[: :] : 50051')
print("Service startup")
server.start()
try:
while True:
time.sleep(60*60*24)
except KeyboardInterrupt:
server.stop(0)
if __name__=='__main__':
serve()
Copy the code
Create a service with port number 50051 and try to start the service
Go to the client
Write the following code in the go package main.go
package main
import (
"context"
pb "cymdemo/micro"
"fmt"
"google.golang.org/grpc"
)
const address = "localhost:50051"
func main(a) {
conn,err := grpc.Dial(address,grpc.WithInsecure())
iferr ! =nil {
fmt.Println(err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
name := "world"
res,err := c.SayHello(context.Background(),&pb.HelloRequest{Name:name})
iferr ! =nil{
fmt.Println(err)
}
fmt.Println(res.Msg)
}
Copy the code
Start the Python server and then start the Go client to get the call results
hello world
Copy the code