进阶使用参考链接
https://chai2010.gitbooks.io/advanced-go-programming-book/content/ch4-rpc/ch4-04-grpc.html
1.生成接口
proto文件编写完成后,执行命令
老版本命令为:
protoc --go_out=plugins=grpc:./ *.proto
新版本命令为:
protoc --go_out=./ *.proto
protoc --go-grpc_out=./ *.proto
生成go文件
若出现错误:
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC
则可使用新版命令或版本降级
go get -u github.com/golang/protobuf/protoc-gen-go@v1.3.2
2.重写方法
创建结构体,成员变量为Unimplemented<rpc方法名>Server
重写方法,方法接受者为上述结构体,入参为context.Context, <proto中定义的message>,返回值为
<proto中定义的message>,error
3.服务端注册并启动服务

4.客户端链接
使用client即可像调用本地方法一样调用远程函数

2932

被折叠的 条评论
为什么被折叠?



