学习来源
https://naotu.baidu.com/file/c80e753e20e8ab18a80cb573ac08e912?token=e89e127e95b325d5
https://www.bilibili.com/video/BV1Ev411w7yu/?spm_id_from=333.788&vd_source=53e5d5a8d002898905d03bcc15ef7842
安装协议包
当前go版本 go1.15.6
1.创建proto后缀文件,配置。
2.goland 搜索 grpc插件安装
3.https://grpc.io/docs/languages/go/quickstart/ 快速开始,安装包。
这个并非是安装到项目中的仓库的

执行报错。提示这种带版本的方式只能用go get方式来获取。根据go的版本不同。高版本的可以用go install 。
此时,gopath下的bin目录下,两条命令均执行完后会多出两个可执行文件:
4.安装protoc.exe,便于生成文件
https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.0 下载解压提取里面的可执行文件,并放置到上述bin目录下。
安装grpc包
go get google.golang.org/grpc
编译proto文件

文件内容:
syntax = "proto3";package hello_grpc;option go_package = "./;hello_grpc";message Req {string message = 1;}message Res {string message = 1;}service HelloeGRPC {rpc SayHi(Req) returns (Res);}
这里的go_package,包名,需跟package一致,方便调用。
编译命令
protoc —go_out=. —go_opt=paths=source_relative —go-grpc_out=. —go-grpc_opt=paths=source_relative ./hello_grpc.proto
这个比较长,可以将此命令做成一个bat文件,放置于hello_grpc.proto的同个文件夹下。
goland里面使用命令行获取到的gopath或者是gobin有可能跟系统设置的不一样。
如果报以下错误,说明程序没有正确找到插件的位置。需要手动指定位置:
protoc —plugin=protoc-gen-go=%GOBIN%/protoc-gen-go.exe —plugin=protoc-gen-go-grpc=%GOBIN%/protoc-gen-go-grpc.exe —go_out=. —go_opt=paths=source_relative —go-grpc_out=. —go-grpc_opt=paths=source_relative ./hello_grpc.proto
执行命令
生成了两个go文件。
至此grpc环境已经完成
开始使用grpc
先创建服务端
创建client和server文件夹。分别代表两个服务,做调用过程。
先写服务端,分为4个步骤
- 取出server
- 挂载方法
- 注册服务
- 创建监听
官网给出的使用示例:
到这个github仓库中,查看示例。https://github.com/grpc/grpc-go
参考这个:
代码敲入 grpc.NewServer()。出不来时,需要设置goland的gopath
服务端代码
package mainimport ("context""fmt""google.golang.org/grpc"hello_grpc "grpc_test/pb""net")//1.声明服务,组合服务接口type Server struct {//这里组合进一个刚生成出来的空接口,我们需要实现这个接口中的方法//而这个方法正是hello_grpc.proto中的定义的方法。//这个接口的名称也包含了proto文件中service的名称。hello_grpc.UnimplementedHelloeGRPCServer}//2.挂载方法。这个就是服务端对外暴露的方法。在proto文件中也要声明这个方法//返回的是个指针func (s Server) SayHi(ctx context.Context, req *hello_grpc.Req) (res *hello_grpc.Res, err error) {fmt.Println(req.GetMessage())//return &hello_grpc.Res{Message: "这是从服务端返回的msg",}, nil}func main() {//3.注册服务listener, err := net.Listen("tcp", ":8888")if err != nil {fmt.Println("监听8888端口失败")}grpc_server := grpc.NewServer()//将我们提供的服务和grpc的服务绑定起来hello_grpc.RegisterHelloeGRPCServer(grpc_server, &Server{})//4.监听服务err = grpc_server.Serve(listener)if err != nil {fmt.Println("监听8888端口失败-2")}}
创建客户端
参考客户端示例:
客户端代码
package mainimport ("context""fmt""google.golang.org/grpc""google.golang.org/grpc/credentials/insecure"hello_grpc "grpc_test/pb")func main() {//1.创建连接conn, err := grpc.Dial(":8888", grpc.WithTransportCredentials(insecure.NewCredentials()))defer conn.Close()if err != nil {//如果grpc.Dial(":8888"),仅这样会报错,看提示报错加上后面的参数fmt.Println(err)fmt.Println("创建连接失败")}//2.创建客户端client := hello_grpc.NewHelloeGRPCClient(conn)//传入上下文res, err := client.SayHi(context.Background(), &hello_grpc.Req{Message: "这是客户端发出的消息",})fmt.Println(res.GetMessage())}
两边运行起来就能看到效果了。
