浠嬬粛鏈枃浠嬬粛濡備綍浣跨敤grpc涓巊o浣滀负瀹㈡埛绔紝python浣滀负鏈嶅姟绔繘琛岄€氫俊銆傦紙棰樺璇濓細涓€鐩磋糠鑼€庝箞鎶婁粬浠袱涓仈绯昏捣鏉ワ紝鍚庢潵鍙戠幇鍙渶瑕佺紪璇戝悓涓€涓猵roto鏂囦欢灏卞彲浠ヤ簡...馃槗)瀹炵幇鍑芥暟python瀹炵幇鏂规硶f(name)锛岃繑鍥?hello"+name锛岃幏鍙杇o璋冪敤鐨勮繑鍥炲€笺€傚畨瑁呴厤缃瓽o涓汉閰嶇疆鏄痝o1.12锛屼娇鐢╣omod椤圭洰绠$悊鍥犱负鏈変簺鍖呬細琚睆钄斤紝閮介渶瑕侀厤缃瓽OPROXY锛屾垜閰嶇疆浜嗛樋閲岀殑GOPROXY="https://mirrors.aliyun.com/goproxy/"鏉ュ畨瑁単rpc,protobuf缂栬瘧鍣ㄥ拰瀵瑰簲鐨刧oplugingogetgoogle.golang.org/grpcgogetgithub.com/golang/protobuf/protogogetgithub.com/golang/protobuf/proto-gen-go娉細濡傛灉鍦ㄥ懡浠よ涓娇鐢╣oland缂栬瘧鍣紝杩橀渶瑕侀厤缃唬鐞唒ython3锛岃繕瑕佸畨瑁単rpc銆乸rotobuf绛夈€傚彧闇€鍦ㄧ孩妗嗗唴閫夋嫨鎮ㄨ嚜宸辩殑鍙h瘧鍛樺嵆鍙€傞」鐩粨鏋勬槸鎴戝垵灏濓紝鍙兘涓嶈鑼冿紝璇锋寚姝?project-go-main.go-micro-hello.proto-python-server.py杩欐槸闇€瑕佽嚜宸卞垱寤虹殑鐩綍鍜屾枃浠躲€俫o浠g爜鍦╣o鍖呬腑锛宮icro鏄井鏈嶅姟閰嶇疆鏂囦欢锛宲ython鍖呮槸python浠g爜銆傚井鍖呴鍏堝垱寤簆roto鏂囦欢--hello.protosyntax="proto3";//閫夋嫨鐗堟湰鍖卪icro;//灏佽serviceGreeter{rpcSayHello(HelloRequest)returns(HelloReply){}}messageHelloRequest{stringname=1;}messageHelloReply{stringmsg=1;}娉ㄦ剰锛氳繖閲岀殑灏佽蹇呴』鍜屽綋鍓嶈矾寰勪竴鑷磒roto璇硶涓嶈缁嗚В閲娿€備綘闇€瑕佽嚜宸辨鏌ュ拰缂栬瘧proto鏂囦欢銆傞鍏堝皢鍛戒护琛岀Щ鍔ㄥ埌寰寘涓紝鐒跺悗鎵цgo鍜宲ython缂栬瘧璇彞go:hello.proto鏄渶瑕佺紪璇戠殑鏂囦欢銆傜紪璇戝悗浼氬湪褰撳墠鍖?-go_out=plugins=grpc:涓嬬敓鎴恏ello.pb.go鏂囦欢protoc銆俬ello.protopython锛氱紪璇戝悗浼氱敓鎴恏ello_pb2.py鍜宧ello_pb2_grpc.py杩欎袱涓枃浠秔ython3-mgrpc_tools.protoc--python_out=銆?-grpc_python_out=銆?鎴戙€傚鏋渉ello.proto鏂囦欢鐢熸垚鎴愬姛锛屽氨鍙互缂栧啓瀹㈡埛绔拰鏈嶅姟绔殑浠g爜浜嗐€俻ython鏈嶅姟鍣ㄥ湪python鍖卻erver.py涓嬪湪鏂囦欢涓啓鍏ュ涓嬩唬鐮乫romconcurrentimportfuturesimporttimeimportgrpcfrommicroimporthello_pb2frommicroimporthello_pb2_grpcclassGreeter(hello_pb2_grpc.GreeterServicer):defSayHello(self,request,context):returnhello_pb2.HelloReply)+freply(msg="hserve():server=grpc.server(futures.ThreadPoolExecutor(max_workers=10))hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)server.add_insecure_port('[::]:50051')print("Servicestart")server.start()try:whileTrue:time.sleep(60*60*24)exceptKeyboardInterrupt:server.stop(0)if__name__=='__main__':serve()鍒涘缓涓€涓湇鍔$鍙e彿涓?0051锛屽彲浠ュ皾璇曞惎鍔ㄦ湇鍔oclient鍐欏湪go鍖呬笅鐨刴ain.go涓互涓嬩唬鐮佸寘mainimport("context"pb"cymdemo/micro""fmt""google.golang.org/grpc")constaddress="localhost:50051"funcmain(){conn,err:=grpc.Dial(鍦板潃锛実rpc.WithInsecure())iferr!=nil{fmt.Println(err)}deferconn.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)}鍏堝惎鍔╬ythonserver锛屽啀鍚姩goclient鑾峰彇璋冪敤缁撴灉helloworld
