栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2.python + grpc-gateway

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2.python + grpc-gateway

python + grpc-gateway
    • 1. 前言
    • 2. 新建用户
    • 3. 安装工具
    • 4. 开始创建helloworld:
    • 5.测试
    • 6.总结

1. 前言
今天在网上看了硬是没找到使用python代码的grpc-gateway,只能试着使用go的grpc-gateway
监听python grpc服务,不过grpc-gateway本身就是一个插件,使用go的话问题应该也不大。
2. 新建用户
[test@localhost ~]$ useradd test	# 新建 test 用户
[test@localhost ~]$ passwd test		# 修改密码
[test@localhost ~]$ sudo vim /etc/sudoer	# 为 test 用户增加  sudo 权限,使用wq!保存
# 省略前面部分 ...
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL
test	ALL=(ALL)		ALL
# 省略后面部分 ...
[test@oyk-test ~]$ su - test		# 切换至 test 用户
3. 安装工具

最好是有科学上网,这样下载会快很多。

  • 安装golang
[test@oyk-test ~]$ sudo yum install golang -y
[test@oyk-test ~]$ go version
go version go1.13.3 linux/amd64
  • 安装grpcio grpcio-tools
[test@localhost ~]$ sudo pip3 install grpcio grpcio-tools
  • 安装 grpc-gateway
[test@localhost ~]$ echo -e "export GOPROXY=https://goproxy.cnnGOPATH=$(go env 
GOPATH)nPATH=${PATH}:$(go env GOPATH)/bin" >> ~/.bashrc && source ~/.bashrc
[test@localhost ~]$ curl https://raw.githubusercontent.com/1397436824/
grpc-gateway-sh/main/install-grpc-gateway.sh | bash
  • 安装protoc-gen-go、protoc-gen-go-grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
  • 更新为go1.17.2,如果比这个版本更新,可以不做操作
[test@localhost ~]$ go get golang.org/dl/go1.17.2@latest
[test@localhost ~]$ ~/go/bin/go1.17.2 download
[test@localhost ~]$ sudo unlink /bin/go
[test@localhost ~]$ sudo ln -s ~/go/bin/go1.17.2 /bin/go
[test@localhost ~]$ go version
go version go1.17.2 linux/arm64
4. 开始创建helloworld:
  • 生成stubs源码
[test@localhost ~]$ mkdir helloworld && cd helloworld
[test@localhost helloworld]$ go mod init github.com/test/myrepo
[test@localhost helloworld]$ mkdir -p proto/helloworld/
[test@localhost helloworld]$ cat > proto/helloworld/helloworld.proto < 
# 编写grpc服务端,main.py
[test@localhost helloworld]$ cat > main.py < 
# 此时执行 python3 main.py 会报错;
[test@localhost helloworld]$ python3 main.py 
Traceback (most recent call last):
  File "main.py", line 6, in 
    from proto.helloworld import helloworld_pb2_grpc
  File "/home/test/helloworld/proto/helloworld/helloworld_pb2_grpc.py", line 5, in 
    from helloworld import helloworld_pb2 as helloworld_dot_helloworld__pb2
ModuleNotFoundError: No module named 'helloworld'
# 解决方法;
[test@localhost helloworld]$ sed -E -i 's/from helloworld /from . /g' /home/test/helloworld/proto/helloworld/helloworld_pb2_grpc.py
# grpc服务端,现在就可以使用python3 main.py运行了
# 此时的目录结构如下
[test@localhost helloworld]$ tree
.
├── main.py
└── proto
    ├── google
    │   └── api
    │       ├── annotations.proto
    │       └── http.proto
    └── helloworld
        ├── helloworld_pb2_grpc.py
        ├── helloworld_pb2.py
        ├── helloworld.proto
        └── __pycache__
            ├── helloworld_pb2.cpython-37.pyc
            └── helloworld_pb2_grpc.cpython-37.pyc

5 directories, 8 files
  • 下面准备grpc-gateway
[test@localhost helloworld]$ mkdir -p ./gen/go
[test@localhost helloworld]$ go mod init github.com/test/myrepo
# 生成 gateway的相关源码
[test@localhost helloworld]$ python3 -m grpc_tools.protoc -I./proto --go_out ./gen/go/ --go-grpc_out ./gen/go/ ./proto/helloworld/helloworld.proto
[test@localhost helloworld]$ python3 -m grpc_tools.protoc -I ./proto --grpc-gateway_out=logtostderr=true:./proto/helloworld ./proto/helloworld/helloworld.proto
[test@localhost helloworld]$ go mod tidy
# gateway 网关代码,8081监听8080
[test@localhost helloworld]$ cat > helloworld.gw.go < 
5.测试 
# 首先运行
[test@localhost helloworld]$ python3 main.py
# 另开一终端执行
[test@localhost helloworld]$ go run helloworld.gw.go
# 另开一终端执行
[test@localhost helloworld]$ curl -X POST http://localhost:8081/v1/example/echo -d '{"name": "欧力给"}'
{"message":"Hello, 欧力给!"}
# 如果访问没有输出,请将curl 中的 localhost换成具体的IP地址
6.总结

之后,想必大家可以使用任何grpc支持的语言搭配grpc-gateway使用了吧,套路就是:

  • 写出proto文件,并加以修改
  • 生成某语言对应的grpc stub源码
  • 生成gw go语言代码
  • 编写 grpc服务端
  • 编写 grpc 代理(go)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号