python grpc unary call错误_gRPC Golang/Python 使用

51Reboot 运维开发

Golang 课程

K8S 课程

Python 自动化进阶课程

Python 基础实战课程

运维前端课程

课程试听预约请扫码>>>

52006508cf7cde2035c3c13d35fad52f.png gRPC Golang/Python使用

以前开发网站都是用 http 协议,学过 TCP/IP 协议的人都知道,在传输层 TCP的基础上,应用层 HTTP 就是填充了一定规则的文本。

gRPC 使用和介绍

工作中使用到 gRPC, 其实 http 请求也是一种 rpc 变种,远程进程调用.gRPC底层是 HTTP2 协议。gRPC 一开始由 google 开发,是一款语言中立、平台中立、开源的远程过程调用 (RPC) 系统,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc,grpc-java,grpc-go.其中C版本支持C,C++,Node.js,Python,Ruby,Objective-C,PHP 和 C# 支持.

gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

gRPC 基于以下理念:定义一个服务,指定其能够被远程调用的方法(包含参数和返回类型)。在服务端实现这个接口,并运行一个 gRPC 服务器来处理客户端调用。在客户端拥有一个存根能够像服务端一样的方法,即调用仿佛就在同一台机器。

可以使用不同语言平台进行开发

a1a96fc0904c1ad4440b357ad0b938a0.png

gRPC 默认使用 protocol buffers 来进行消息通讯,这是 Google 开源的一套成熟的结构数据序列化机制

参考:gRPC 官方文档中文版概念(http://doc.oschina.net/grpc)

Protocol

Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

项目地址:

https://github.com/google/protobuf

安装参考: 

https://github.com/google/protobuf/blob/master/src/README.md

安装 The protocol compiler

sudo apt-get install autoconf automake libtool curl make g++ unzip./autogen.sh#####执行./autogen.sh时,报error: configure.ac:1: file'gtest/m4/acx_pthread.m4' does not exist的错误。#在gmock目录新建gtest文件夹,拷贝项目根目录下m4文件夹至gtest文件夹####./configuremakemake checksudo make installsudo ldconfig # refresh shared library cache.

使用(下面 go 语言还需安装插件)

go get -u github.com/golang/protobuf/protogo get -u github.com/golang/protobuf/protoc-gen-go

文档参考: 

protocol-buffers 文档

Go gRPC

Go版库:https://github.com/grpc/grpc-go

获取

go get google.golang.org/grpc

使用

protoc --go_out=plugins=grpc:. *.proto

如果要网关转 http,如下操作

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gatewaygo get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

然后

#!/usr/bin/env bashprotoc -I/usr/local/include -I. \  -I$GOPATH/src \  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \  --go_out=plugins=grpc:. \  msg_newest.protoprotoc -I/usr/local/include -I. \  -I$GOPATH/src \  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \  --grpc-gateway_out=logtostderr=true:. \  msg_newest.proto
Python gRPC
pip install grpcio==1.4.0pip install grpcio-tools==1.4.0pip install protobufpython -m grpc.tools.protoc --python_out=pbdata --grpc_python_out=pbdata -I . msg.proto

转载请注明:http://www.lenggirl.com/cap/grpc.html

c89d5a554793d70370b48adbfc115650.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值