久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

python golang中grpc 使用示例代碼詳解

瀏覽:3日期:2022-07-23 11:41:04

python

1、使用前準備,安裝這三個庫

pip install grpciopip install protobufpip install grpcio_tools

2、建立一個proto文件hello.proto

// [python quickstart](https://grpc.io/docs/quickstart/python.html#run-a-grpc-application)// python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto// helloworld.protosyntax = 'proto3';package test;service Greeter { rpc SayHello(HelloRequest) returns (HelloReply) {} rpc SayHelloAgain(HelloRequest) returns (HelloReply) {}}service Greetera{ rpc SayStudent(Studentid) returns (Student){}}message Student { string msg=1;//json}message Studentid{ string id=1;}message HelloRequest { string name = 1;}message HelloReply { string message = 1;}

3、執行命令就會對應生成兩個py文件

hello_pb2.py

hello_pb2_grpc.py

python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto

4、py服務端代碼hello.server.py:

from concurrent import futuresimport timeimport grpcimport hello_pb2import hello_pb2_grpcimport json# 實現 proto 文件中定義的 GreeterServicerclass Greeter(hello_pb2_grpc.GreeterServicer): # 實現 proto 文件中定義的 rpc 調用 def SayHello(self, request, context): return hello_pb2.HelloReply(message = ’hello {msg}’.format(msg = request.name)) def SayHelloAgain(self, request, context): return hello_pb2.HelloReply(message=’hello {msg}’.format(msg = request.name))class Gretera(hello_pb2_grpc.GreeteraServicer): def SayStudent(self,request,context): print(request.id) if request.id=='0': c=hello_pb2.Student(msg=json.dumps({'name':'owen','age':22,'sex':'男'})) else: c=hello_pb2.Student(msg=json.dumps({'name':'lihui','age':23,'sex':'女'})) return cdef serve(): # 啟動 rpc 服務 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) hello_pb2_grpc.add_GreeteraServicer_to_server(Gretera(),server) server.add_insecure_port(’[::]:50052’) server.start() try: while True: time.sleep(60*60*24) # one day in seconds except KeyboardInterrupt: server.stop(0)if __name__ == ’__main__’: serve()

py客戶端代碼hello.client.py:

import grpcimport hello_pb2import hello_pb2_grpcimport jsondef run(): # 連接 rpc 服務器 channel = grpc.insecure_channel(’localhost:50051’) # 調用 rpc 服務 stub = hello_pb2_grpc.GreeterStub(channel) response = stub.SayHello(hello_pb2.HelloRequest(name=’czl’)) print('Greeter client received: ' + response.message) response = stub.SayHelloAgain(hello_pb2.HelloRequest(name=’nsdnfkjda’)) print('Greeter client received: ' + response.message) stub1 = hello_pb2_grpc.GreeteraStub(channel) response1 = stub1.SayStudent(hello_pb2.Studentid(id=’1’)) print(json.loads(response1.msg))if __name__ == ’__main__’: run()

golang

由于grpc是跨語言的所以這里用golang做為示范,golang客戶端代碼,小編這里也踩了許多坑,最主要的是兩個proto文件一定要一致,golang 中使用必須安裝protoc,windows將環境變量指向安裝目錄的bin下面:

1、protocal buffer安裝

從 https://github.com/google/protobuf/releases 下載 對應自己的系統(環境變量記得改)

2、安裝 golang protobuf

go get -u github.com/golang/protobuf/proto // golang protobuf 庫go get -u github.com/golang/protobuf/protoc-gen-go //protoc --go_out 工具

3、安裝 gRPC-go

go get google.golang.org/grpc

4、生成go文件

protoc --go_out=plugins=grpc:文件目錄 對應的.proto文件protoc --go_out=plugins=grpc:. hello.proto

生成hello.pb.go,調用的實現hello_go_client.go:

package mainimport ( 'context' 'encoding/json' 'google.golang.org/grpc' 'log' 'student/test' //對應的生成文件目錄)type Studenmsg struct { Name string Age int Sex string}func main() { // 建立連接到gRPC服務 conn, err := grpc.Dial('127.0.0.1:50052', grpc.WithInsecure()) if err != nil { log.Fatalf('did not connect: %v', err) } // 函數結束時關閉連接 defer conn.Close() // 創建Waiter服務的客戶端 t := test.NewGreeteraClient(conn) tr,err:=t.SayStudent(context.Background(),&test.Studentid{Id:'1'}) if err != nil { log.Fatalf('could not greet: %v', err) } var st Studenmsg err=json.Unmarshal([]byte(tr.Msg),&st)//這里說明一下發過來的數據是json格式轉化成struct if err!=nil{ log.Println(err.Error()) } log.Println(st.Name,st.Age,st.Sex)}

總結

到此這篇關于python golang中grpc 使用示例代碼詳解的文章就介紹到這了,更多相關python golang grpc 使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 99久久精品免费看国产四区 | 性开放xxxhd视频 | 久久99精品视频 | 国产中文字幕在线 | 欧洲一区二区三区 | 国产精品久久久 | 亚洲网站免费观看 | 亚洲国产精品久久久久秋霞不卡 | 日本久久久一区二区三区 | 亚洲综合欧美 | 欧美 日韩 国产 一区 | 亚洲综合在线视频 | 国产精品美女久久久久久免费 | 欧美另类一区二区 | 一本大道综合伊人精品热热 | 另类中文字幕 | 欧美日韩亚洲视频 | 国产在线乱 | 久久久精品一区 | 91看片官网 | 嫩草影院懂你的 | 欧美三级不卡 | 国产精品一区二区不卡 | 欧美顶级毛片在线播放 | 日韩精品2区 | av大片 | 久久不卡日韩美女 | 中文字幕日韩一区二区 | 精品在线播放 | 精品一区二区不卡 | 国产成人亚洲综合 | 国产免费一区二区三区四区五区 | 国产综合一区二区 | 亚洲福利| 99亚洲| 一本大道综合伊人精品热热 | 日韩中文字幕电影 | 91高清视频在线观看 | 国产精品无码永久免费888 | 91极品视频在线观看 | 免费观看视频www |