不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • python基础

  • python模块

  • django

  • flask

  • SYL

  • Celery

  • 微服务

    • 01.grpc基本使用
      • 01.grpc
        • 1.0 安装
        • 1.1 msg.proto
        • 1.2 msg_server.py
        • 1.3 msg_client.py
        • 1.4 测试
    • 02.grpc_flask
    • 03.nacos_python封装
  • python
  • 微服务
xiaonaiqiang
2021-12-14
目录

01.grpc基本使用

# 01.grpc

  • 参考博文 (opens new window)

# 1.0 安装

  • gRPC由两个部分构成,grpcio 和 gRPC 工具, 后者是编译 protocol buffer 以及提供生成代码的插件。
pip install grpcio
pip install grpcio-tools googleapis-common-protos
1
2

# 1.1 msg.proto

  • 生成文件

  • # 在当前文件夹生成
    python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. msg.proto
    # 指定文件夹生成
    python3 -m grpc_tools.protoc -Iprotos --python_out=compiles --grpc_python_out=compiles protos/recommendations.proto
    
    1
    2
    3
    4
syntax = "proto3";

// service 关键字定义服务,gRPC 提供4种 RPC 类型的服务,这里定义的是第一种单一请求单一回应,类似普通的函数调用
service MsgService {
 rpc GetMsg (MsgRequest) returns (MsgResponse){}
}

// 请求的结构
message MsgRequest {
  string name = 1;
}

// 回应的结果
message MsgResponse {
  string msg = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 1.2 msg_server.py

import grpc
import msg_pb2
import msg_pb2_grpc
# 因为 RPC 应该长时间运行,考虑到性能,还需要用到并发的库
from concurrent import futures
import time
 
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
 
# 在 Server 中,主要是实现服务,按照 msg.proto 定义的,这里需要写一个服务类 MsgServicer ,这个类需要实现之前定义的 GetMsg 
class MsgServicer(msg_pb2_grpc.MsgServiceServicer):
 
  def GetMsg(self, request, context):
    # GetMsg 接收到的请求是在 request 中, msg.proto 中定义的 name 就是 request.name
    # 接着在 GetMsg 中设计 msg.proto中定义的 MsgResponse
    print("Received name: %s" % request.name)
    return msg_pb2.MsgResponse(msg='Hello, %s!' % request.name)
 
 
def serve():
  # 通过并发库,将服务端放到多进程池里运行
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  msg_pb2_grpc.add_MsgServiceServicer_to_server(MsgServicer(), server)
  server.add_insecure_port('[::]:50051')
  server.start()
  try:
    while True:
      time.sleep(_ONE_DAY_IN_SECONDS)
  except KeyboardInterrupt:
    server.stop(0)
 
if __name__ == '__main__':
  serve()
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
32
33

# 1.3 msg_client.py

import grpc

import msg_pb2
import msg_pb2_grpc
 
def run():
  # 连接 服务端
  with grpc.insecure_channel('localhost:50051') as channel:
    # 在这个 channel 上创建 stub , 在 msg_pb2_grpc 里可以找到 MsgServiceStub 这个类相关信息
    stub = msg_pb2_grpc.MsgServiceStub(channel)
    # 这个 stub 可以调用远程的 GetMsg 函数
    response = stub.GetMsg(msg_pb2.MsgRequest(name='world'))
  print("Client received: " + response.msg)
 
 
if __name__ == '__main__':
  run()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 1.4 测试

# 首先启动服务端
python msg_server.py

# 启动客户端
python msg_client.py
1
2
3
4
5
上次更新: 2024/3/13 15:35:10
10.celery踩过的坑
02.grpc_flask

← 10.celery踩过的坑 02.grpc_flask→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式