Google GRPC初试
jiangsoft · · 7191 次点击 · · 开始浏览GRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(ProtocolBuffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。
1 安装
go get google.golang.org/grpc
2 创建helloworld.proto
syntax = "proto3";
option java_package = "io.grpc.examples";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
3.安装 protoc
下载protoc工具:
https://github.com/google/protobuf, 有源码及编译好的, 自行选择
安装go插件:
go get -a github.com/golang/protobuf/protoc-gen-go
4.生成golang文件
protoc -I ./ ./helloworld.proto --go_out=plugins=grpc:helloworld
5.获得实例代码:
$ go get -u github.com/grpc/grpc-common/go/greeter_client
$ go get -u github.com/grpc/grpc-common/go/greeter_server
6.运行
服务端:
$ greeter_server &
客户端:
$ greeter_client
有兴趣的,可以一起讨论!!
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
GRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(ProtocolBuffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。
1 安装
go get google.golang.org/grpc
2 创建helloworld.proto
syntax = "proto3";
option java_package = "io.grpc.examples";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
3.安装 protoc
下载protoc工具:
https://github.com/google/protobuf, 有源码及编译好的, 自行选择
安装go插件:
go get -a github.com/golang/protobuf/protoc-gen-go
4.生成golang文件
protoc -I ./ ./helloworld.proto --go_out=plugins=grpc:helloworld
5.获得实例代码:
$ go get -u github.com/grpc/grpc-common/go/greeter_client
$ go get -u github.com/grpc/grpc-common/go/greeter_server
6.运行
服务端:
$ greeter_server &
客户端:
$ greeter_client
有兴趣的,可以一起讨论!!