DDR爱好者之家 Design By 杰米
python
1、使用前准备,安装这三个库
pip install grpcio pip install protobuf pip 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.proto
syntax = "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 futures
import time
import grpc
import hello_pb2
import hello_pb2_grpc
import json
# 实现 proto 文件中定义的 GreeterServicer
class 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 c
def 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 grpc
import hello_pb2
import hello_pb2_grpc
import json
def 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 main
import (
"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)
}
总结
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年10月28日
2025年10月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]