grpc protobuf

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.prot

6、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

 

7 server端代码示例:
复制代码
from concurrent import futures
import grpc
import SimpleCal_pb2
import SimpleCal_pb2_grpc

class 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=5))
  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 80 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的流量进行抓包分析

 

posted on   我和你并没有不同  阅读(295)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示