python rpc微服务框架_grpc的微服务探索实践
對于微服務(wù)的實(shí)踐,一般都是基于Java和Golang的,博主最近研究了下基于Python的微服務(wù)實(shí)踐,現(xiàn)在通過一個(gè)簡單的服務(wù)來分析Python技術(shù)棧的微服務(wù)實(shí)踐
技術(shù)棧:Python3 + grpc + Zookeeper
服務(wù)API:通過學(xué)科獲取相應(yīng)的題型
grpc:由Google公司開源的高性能RPC框架,消息協(xié)議使用Google自家開源的Protocol Buffers協(xié)議機(jī)制,傳輸使用HTTP2.0標(biāo)準(zhǔn),支持雙向流和連接多路復(fù)用
Protocol Buffers部分:
syntax = "proto3";
message Subject {
string name= 1;
}
message QuestionType {
string name= 1;
}
service SimpleRpcServer {//server streaming rpc//客戶端發(fā)送學(xué)科,服務(wù)端多次返回該學(xué)科包含的題型
rpc GetSubjectQuestionTypes (Subject) returns (stream QuestionType) {
}
}
這里定義grpc的接口類型為服務(wù)器流式RPC,即客戶端發(fā)起一次請求,服務(wù)器可以返回多個(gè)響應(yīng)信息,典型的例子有:客戶端向服務(wù)端發(fā)送一個(gè)股票代碼,服務(wù)端就把該股票的實(shí)時(shí)數(shù)據(jù)源源不斷的返回給客戶端:
通過protobuf編譯器和Protocol Buffers生成代碼:
python3 -m grpc_tools.protoc -I. --python_out=.. --grpc_python_out=.. simple_calculate.proto
服務(wù)端開啟服務(wù)器,對外提供rpc調(diào)用流程:
客戶端rpc調(diào)用流程:
Zookeeper服務(wù)注冊與發(fā)現(xiàn)策略:
服務(wù)注冊:
1 defregister_zk(host, port):2 """
3 注冊到zookeeper4 """
5 zk = KazooClient(hosts='{host}:{port}'.format(6 host=settings_info["zookeeper"]["host"],7 port=settings_info["zookeeper"]["port"])8 )9 zk.start()10 zk.ensure_path('/rpc_calc') #創(chuàng)建根節(jié)點(diǎn)
11 value = json.dumps({'host': host, 'port': port})12
13 #創(chuàng)建服務(wù)子節(jié)點(diǎn)
14 zk.create(15 '/rpc_calc/calculate_server',16 value.encode(),17 ephemeral=True,18 sequence=True19 )
服務(wù)治理發(fā)現(xiàn):
1 def _get_servers(self, event=None):2 """
3 從zookeeper獲取服務(wù)器地址信息列表4 """
5 servers =self._zk.get_children(6 '/rpc_calc', watch=self._get_servers7 )8 print(servers)9 self._servers =[]10 for server inservers:11 data = self._zk.get('/rpc_calc/' +server)[0]12 ifdata:13 addr =json.loads(data.decode())14 self._servers.append(addr)15
16 defget_server(self):17 """
18 隨機(jī)選出一個(gè)可用的服務(wù)器19 """
20 return random.choice(self._servers)
服務(wù)端實(shí)現(xiàn)代碼:
1 classSimpleRpcServerServicer(calculate_grpc.SimpleRpcServerServicer):2 """
3 實(shí)現(xiàn)被調(diào)用方法的具體代碼4 """
5
6 def __init__(self):7 self.subject_question_type_db ={8 'Chinese': ['單選', '多選', '填空', '解答', '問答', '作文'],9 'Math': ['單選', '填空', '解答'],10 'English': ['單選', '填空', '作文'],11 'Physics': ['單選', '多選', '填空', '解答'],12 'Chemistry': ['單選', '多選', '填空', '解答'],13 'Biology': ['單選', '多選', '填空', '解答'],14 'History': ['單選', '多選', '填空', '解答', '問答']15 }16
17 defGetSubjectQuestionTypes(self, request, context):18 """
19 服務(wù)器流式RPC調(diào)用 根據(jù)subject獲取question_types20 :param request:21 :param context:22 :return:23 """
24 subject =request.name25 question_types =self.subject_question_type_db.get(subject)26 for question_type inquestion_types:27 yield calculate_pb2.QuestionType(name=question_type)
客戶端實(shí)現(xiàn)代碼:
1 definvoke_get_subject_question_types(stub):2 """
3 根據(jù)學(xué)科獲取題型4 Server Streaming RPC 服務(wù)器流式RPC 客戶端發(fā)送,服務(wù)器響應(yīng)多個(gè)5 :param stub:6 :return:7 """
8 subject = calculate_pb2.Subject(name='Chinese')9 question_types =stub.GetSubjectQuestionTypes(subject)10 for question_type inquestion_types:11 print(question_type.name)
服務(wù)測試:
開啟三個(gè)服務(wù),地址分別是 host:8003 host:8005 host:8009,客戶端開啟兩個(gè),client1 和 client2 ,測試結(jié)果:
已經(jīng)成功注冊了三個(gè)server到Zookeeper,客戶端1使用的是8003端口的server,客戶端2使用的是8005端口的server;grpc框架對于完整的rpc實(shí)現(xiàn)來說,實(shí)質(zhì)上是封裝了 網(wǎng)絡(luò)傳輸、數(shù)據(jù)協(xié)議的打包解包,使得實(shí)現(xiàn)rpc更加簡單,其本質(zhì)仍然是遵守rpc的實(shí)現(xiàn)原理的
總結(jié)
以上是生活随笔為你收集整理的python rpc微服务框架_grpc的微服务探索实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: psp能装安卓软件吗_王思聪:翻译软件能
- 下一篇: nginx 转发慢_学习Nginx的正确