最近需要将其他团队的gprc服务包装为http接口,开始想尝试一下Envoyproxy,之前没有使用过,进行了一些调研,发现无法支持双工流的方式。z
那就只能选择工程包装的方式了,没有写过相关代码,实验了GPRC demo的搭建,记录一下。
Go版本完全按照官网流程,尝试了命令行和集成开发工具两种方式。Idea失败(主要是不知道怎么管理go应用),切换成Goland成功。
一、环境准备
安装Protocol Buffers,本机环境为mac,注意参考官网文档
brew install protobu
源下载:Releases · protocolbuffers/protobuf · GitHub
安装Go plugin,不明白为啥叫gprc,原生不支持go版本的proto文件编译,也许是插件化一切吧。
-
Go plugins for the protocol compiler:
-
Install the protocol compiler plugins for Go using the following commands:
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
-
Update your
PATH
so that theprotoc
compiler can find the plugins:$ export PATH="$PATH:$(go env GOPATH)/bin"
-
二、获取代码
clone代码,一定要使用分支
$ git clone -b v1.48.0 --depth 1 https://github.com/grpc/grpc-go
进入样例代码目录
$ cd grpc-go/examples/helloworld
三、运行样例
编译执行服务端代码
$ go run greeter_server/main.go
结果图:
编译执行客户端代码
$ go run greeter_client/main.go
结果图:
增加参数:
$ go run greeter_client/main.go --name=Alice
总之跟着官网文档做就可以了,开发工具我用了Goland才成功,Idea还不会配置,代码报错。
参考文章:
gRPC-JSON transcoder — envoy 1.24.0-dev-6c7513 documentation