首页 技术 正文
技术 2022年11月9日
0 收藏 613 点赞 4,183 浏览 2569 个字

rpc和http的比较:

http://www.ccutu.com/244407.html

http通信时带报文头,增加了传输成本RPC主要是基于TCP/IP协议的,而HTTP服务主要是基于HTTP协议的【HTTP是应用层协议,而TCP是传输层协议,HTTP协议是在传输层协议TCP之上的,所以效率来看的话,RPC当然是要更胜一筹。】
rpc是长链接RPC框架一般都有注册中心,有丰富的监控管理;发布、下线接口、动态扩展等,对调用方来说是无感知、统一化的操作

介绍grpc的网站:https://doc.oschina.net/grpc?t=60138

grpc与nginx一起使用:https://www.nginx.com/blog/nginx-1-13-10-grpc/

grpc与nginx示例:https://mp.weixin.qq.com/s/exOvWF2nOnqG8GEcitVM-Q

1、ProtoBuffer是google的一款非常高效的数据传输格式框架

2、一个方法仅能接受一个参数

3、对于定义的message,每个值都有一个唯一的number类型的数字,根据官方文档的解释:它是用于以消息二进制格式标识字段,并且在使用过程中不能随便更改,否则会导致数据无法还原。同时,如果数字定义为1~15则使用一个字节来存储,而16~2047需要使用两个字节来存储

4、官网: https://grpc.io/docs/quickstart/python.html

5、 根据protocol生成代码:From the examples/python/helloworld directory, run:

 python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.prot6、python 的grpc加超时时间,一直不知道在哪里加,查了好多地方都没找到,后来pdb单步调试,终于发现了
def __call__(self, request, timeout=None, metadata=None, credentials=None)
response = self.stub_client.fs(fake_pb2.FsRequest(wfid=id,stime=s_m,type=type),timeout=60)
但怎么捕获异常,又不会了,再次看官方文档,在异常相关介绍那里找到了示例
https://grpc.io/docs/guides/error.html

grpc protobuf

7 server端代码示例:
from concurrent import futures
import grpc
import SimpleCal_pb2
import SimpleCal_pb2_grpcclass CalServicer(SimpleCal_pb2_grpc.CalServicer):
def Add(self, request, context): # Add函数的实现逻辑
print("Add function called")
return SimpleCal_pb2.ResultReply(number=request.number1 + request.number2) def Multiply(self, request, context): # Multiply函数的实现逻辑
print("Multiply service called")
return SimpleCal_pb2.ResultReply(number=request.number1 * request.number2)def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=))
SimpleCal_pb2_grpc.add_CalServicer_to_server(CalServicer(),server)
server.add_insecure_port("[::]:50051")
server.start()
print("grpc server start...")
server.wait_for_termination()if __name__ == '__main__':
serve()

使用Nginx来代理gRPC

gRPC是基于HTTP/2协议的,Nginx在1.9.5里开始支持HTTP/2,在1.13.10里开始支持gRPC。为了反向代理gRPC服务,编译Nginx的时候必须要添加这两个参数:--with-http_ssl_module --with-http_v2_module

给Nginx添加如下的server配置:

server {
listen http2; location / {
grpc_pass grpc://localhost:50051;
}
}

把这段server的配置添加到Nginx的http段里,配置和启动好Nginx之后,然后把cal_client.py里的channel = grpc.insecure_channel('localhost:50051') 一行的连接地址替换为Nginx提供的地址就可以了

可以打开*l_pb2_grpc.py你可以看到在个类的__init__方法里,定义了方法函数对应的uri。

class statisticStub(object):
# missing associated documentation comment in .proto file
pass def __init__(self, channel):
"""Constructor. Args:
channel: A grpc.Channel.
"""
self.cal_acc = channel.unary_unary(
'/etl.statistic/cal_acc',
request_serializer=statistic__pb2.staRequest.SerializeToString,
response_deserializer=statistic__pb2.staResponse.FromString,
)

9 可以用wireshark来对http2的流量进行抓包分析

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290