Python grpc 构建微服务

PreRequirements

requirements.txt

1
2
3
4
5
celery==4.2.1
grpcio==1.19.0
grpcio-tools==1.19.0
ontology-python-sdk==1.3.1
protobuf==3.7.0

Install Dependent Packages

1
2
pyenv activate env_grpc_demo
pip install --no-cache-dir -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

proto/demo.proto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
syntax = "proto3";

package demo;

message RequestData {
string data = 1;
}

message ResponseData {
int64 return_code = 1;
string message = 2;
string data = 3;
}

service DemoService {

rpc CreateOne(RequestData) returns (ResponseData) {}
rpc DeleteOne(RequestData) returns (ResponseData) {}
rpc TransferOne(RequestData) returns (ResponseData) {}
rpc GetCreateNotify(RequestData) returns (ResponseData) {}

}

build.sh

1
2
3
4
python -m grpc_tools.protoc -I/usr/local/include -I. \
--grpc_python_out=. \
--python_out=. \
-I./proto/ demo.proto

Gen demo.proto to python grpc code

1
./build.sh

server.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import logging
import time
from concurrent import futures

import grpc

import demo_pb2
import demo_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class DemoService(demo_pb2_grpc.DemoServiceServicer):

def __init__(self):
pass

def CreateOne(self, request, context):

print("C_2_S CreateOne: >>>>> ", request)

response = demo_pb2.ResponseData(
return_code=0,
message="success",
data=""
)

print("S_2_C CreateOne: <<<<< ", response)

return response

def DeleteOne(self, request, context):

print("C_2_S DeleteOne: >>>>> ", request)

response = demo_pb2.ResponseData(
return_code=0,
message="success",
data=""
)

print("S_2_C DeleteOne: <<<<< ", response)

return response

def TransferOne(self, request, context):

print("C_2_S TransferOne: >>>>> ", request)

response = demo_pb2.ResponseData(
return_code=0,
message="success",
data=""
)

print("S_2_C TransferOne: <<<<< ", response)

return response

def GetCreateNotify(self, request, context):

print("C_2_S GetCreateNotify: >>>>> ", request)

response = demo_pb2.ResponseData(
return_code=0,
message="success",
data=""
)

print("S_2_C GetCreateNotify: <<<<< ", response)

return response


def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
demo_pb2_grpc.add_DemoServiceServicer_to_server(DemoService(), server)
server.add_insecure_port('[::]:9090')
server.start()

print("listen: 9090")

try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)


if __name__ == '__main__':
logging.basicConfig()
serve()

Run Server

1
python server.py

client.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import logging

import grpc

import demo_pb2
import demo_pb2_grpc


def run():
with grpc.insecure_channel('127.0.0.1:9090') as channel:
client = demo_pb2_grpc.DemoServiceStub(channel)

response = client.CreateOne(demo_pb2.RequestData(
data="call create one from client",
))
print(response.return_code, response.message, response.data)


response = client.DeleteOne(demo_pb2.RequestData(
data="call delete one from client",
))
print(response.return_code, response.message, response.data)

response = client.TransferOne(demo_pb2.RequestData(
data="call get transfer one from client",
))
print(response.return_code, response.message, response.data)

response = client.GetCreateNotify(demo_pb2.RequestData(
data="call create one from client",
))
print(response.return_code, response.message, response.data)


if __name__ == '__main__':
logging.basicConfig()
run()

Run Client

1
python client.py