Go gRPC
目录
gRPC
是一个现代的开源高性能远程过程调用(RPC - Remote Procedure Call
)框架,可以在任何环境中运行。它可以通过对负载平衡、跟踪、健康检查和身份验证的可插拔支持,有效地连接数据中心内和跨数据中心的服务。它也适用于分布式计算的最后一英里,将设备、移动应用程序和浏览器连接到后端服务。
gRPC 的特性
- 简单服务定义:使用
Protocol Buffers
定义你的服务,这是一个强大的二进制序列化工具集和语言; - 快速启动并扩展:只需一行代码即可安装运行时和开发环境,还可以通过该框架扩展到每秒数百万个
RPCs
; - 跨语言和平台工作:使用多种语言和平台自动为你的服务生成惯用的客户端和服务器桩;
- 双向流和集成身份验证:双向流式传输和完全集成的可插拔身份验证,以及基于
HTTP/2
的传输;
gRPC 与 RPC 的关系
RPC
是一种协议,gRPC
是基于RPC
协议实现的一种框架;gRPC
解决了RPC
的三大问题:- 协议约定;
- 传输协议;
- 服务发现;
gRPC 适用场景
gRPC 环境配置
安装 protobuf
|
|
安装 Go 的协议编译器插件
|
|
更新 PATH 路径
|
|
实例
实例一
定义服务
|
|
在 hello
目录下创建 hello.proto
文件并复制以下内容:
syntax = "proto3";
option go_package = "greeter.io/greeter/examples/hello/hello";
option java_multiple_files = true;
option java_package = "io.greeter.examples.hello";
option java_outer_classname = "HelloProto";
package hello;
// The greeting service definition.
service Greeter {
// Sends a greeting.
rpc hello(HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest { string name = 1; }
// The response message containing the greetings.
message HelloResponse { string message = 1; }
完成后在 greeter
目录下执行以下命令:
|
|
命令完成后在 hello
目录下会生成 hello_grpc.pb.go
和 hello.pb.go
文件。
编写服务端
在 greeter/cmd/server/main.go
文件中添加以下代码:
|
|
启动 grpc server
服务:
|
|
编写客户端
在 greeter/cmd/client/main.go
文件中添加以下代码:
|
|
启动 grpc client
服务:
|
|
最终的代码目录结构:
|
|
实例二
定义服务
|
|
在 route
目录下创建 route.proto
文件并复制以下内容:
syntax = "proto3";
option go_package = "routeguide/route";
option java_multiple_files = true;
option java_package = "io.routeguide.route";
option java_outer_classname = "RouteProto";
package route;
// Interface exported by the server.
service Route {
// A simple RPC.
//
// Obtains the feature at a given position.
//
// A feature with an empty name is returned if there's no feature
// at the give position.
rpc GetFeature(Point) returns (Feature) {}
// A server-to-client streaming RPC.
//
// Obtains the features available within the given Rectangle. Results
// are streamed rather than returned at once (e.g. in a response message
// with a repeated field), as the rectangle may cover a large area and
// contain a huge number of features.
rpc GetFeatures(Rectangle) returns (stream Feature) {}
// A client-to-server streaming RPC.
//
// Accepts a stream of Points on a route being traversed, returning
// a RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}
// A Bidirectional streaming RPC.
//
// Accepts a stream of RouteNotes sent whil a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest interger).
// Latitude should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}
// A latitude-longitude rectangle, represented as two diagonally opposite
// points "lo" and "hi".
message Rectangle {
// One corner of the rectangle.
Point lo = 1;
// The other corner of the rectangle.
Point hi = 2;
}
// A feature names something at a given point.
//
// If a feature could not be named, the name is empty.
message Feature {
// The name of the feature.
string name = 1;
// The point where the feature is detected.
Point location = 2;
}
// A RouteNote is a message sent while at a given point.
message RouteNote {
// The location from which the message is sent.
Point location = 1;
// The message to be sent.
string message = 2;
}
// A RouteSummary is received in response to a RecordRoute rpc.
//
// It contains the number of individual points received, the number
// of detected features, and the total distance covered as the
// cumulative sum of the distance between eath point.
message RouteSummary {
// The number of points received.
int32 point_count = 1;
// The number of known features passed while traversing the route.
int32 feature_count = 2;
// The distance covered in metres.
int32 distance = 3;
// The duration of the traversal in seconds.
int32 elapsed_time = 4;
}
完成后在 routeguide
目录下执行以下命令:
|
|
编写服务端
在 routeguide/cmd/server/main.go
文件中添加以下代码:
|
|
启动 grpc server
服务:
|
|
编写客户端
在 routeguide/cmd/client/main.go
文件中添加以下代码:
|
|
启动 grpc client
服务:
|
|