这是我参与「第三届青训营 -后端场」笔记创作活动的第1篇笔记
系统环境:ARM+MacOS+zsh+GoLand+Go1.18
本篇笔记仅介绍如何安装gRPC环境,并运行官方提供的hello world demo程序。
不会对protobuf语法进行介绍
安装 Protocol buffers 相关程序
gRPC 是一个基于 Protocol Buffers 的 RPC 应用程序框架
因此,我们需要先安装 Protocol Buffers 相关程序。
配置环境变量
确保环境变量PATH
中配置了$GOPATH/bin
目录
否则再后续生成的步骤中会出现找不到命令的问题
在~/.zshrc
中增加一行(具体位置根据你的GOPATH进行调整):
export PATH=$PATH:$GOPATH/bin
安装 protobuf compiler
MacOS使用brew安装protoc
brew install protobuf
验证protoc是否安装成功
protoc --version
libprotoc 3.19.4
安装 protocal compiler for Go plugin
该插件用于生成Go代码
会被安装在$GOPATH/bin
目录下
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
gRPC DEMO
获取DEMO程序
官方的DEMO程序可以在这个链接获得
git clone https://github.com/grpc/grpc-go.git
在Goland中打开grpc-go/examples/helloworld
目录
注意:该目录下有一个与根目录同名helloworld目录
本文所有指令均不在该子目录执行
➜ helloworld git:(master) tree
.
├── greeter_client
│ └── main.go
├── greeter_server
│ └── main.go
└── helloworld #本文所有命令均不在该目录下运行
├── helloworld.pb.go #为了演示生成功能 将该文件删除
├── helloworld.proto
└── helloworld_grpc.pb.go #为了演示生成功能 将该文件删除
3 directories, 5 files
go moule配置
运行go mod init helloworld
生成go模块文件
➜ helloworld git:(master) ✗ tree
.
├── go.mod #go模块文件
├── greeter_client
│ └── main.go
├── greeter_server
│ └── main.go
└── helloworld
└── helloworld.proto
运行go get google.golang.org/grpc
获取gRPC相关依赖
➜ helloworld git:(master) ✗ go get google.golang.org/grpc
go: added github.com/golang/protobuf v1.5.2
go: added golang.org/x/net v0.0.0-20201021035429-f5854403a974
go: added golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
go: added golang.org/x/text v0.3.3
go: added google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: added google.golang.org/grpc v1.46.2
go: added google.golang.org/protobuf v1.27.1
根据proto文件生成代码
在当前目录运行下述命令
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
helloworld/helloworld.proto
将会自动生成
helloworld/helloworld.pb.go
helloworld/helloworld_grpc.pb.go
➜ helloworld git:(master) tree
.
├── greeter_client
│ └── main.go
├── greeter_server
│ └── main.go
└── helloworld #本文所有命令均不在该目录下运行
├── helloworld.pb.go #重新生成的.go文件
├── helloworld.proto
└── helloworld_grpc.pb.go #重新生成的.go文件
3 directories, 5 files
修改client与server的代码
由于目前是以helloworld作为模块名
接下来我们需要对两个main.go
文件进行一些修改
greeter_client/main.go
import (
...
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
修改为
import (
...
pb "helloworld/helloworld"
)
greeter_server/main.go
import (
...
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
修改为
import (
...
pb "helloworld/helloworld"
)
运行DEMO程序
启动Server
go run greeter_server/main.go --port 8888
➜ helloworld git:(master) ✗ go run greeter_server/main.go --port 8888
2022/05/31 13:39:31 server listening at [::]:8888
启动Client
go run greeter_client/main.go --addr=localhost:8888 --name=user11
➜ helloworld git:(master) ✗ go run greeter_client/main.go --addr=localhost:8888 --name=user11
2022/05/31 13:40:04 Greeting: Hello user11
此时Server端Log输出
2022/05/31 13:40:04 Received: user11