go gRPC 快速启动
快速启动
本指南通过一个简单的工作示例让您开始使用 gRPC。
先决条件
Go, any one of the three latest major releases of Go.
安装说明, 查看 Go’s 入门手册 指南.
Protocol buffer 解析器,
protoc
, version 3.安装说明, 查重 Protocol Buffer Compiler Installation.
Go plugins for the protocol compiler:
使用如下命令安装 protocol compiler plugins for Go :
$ 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
Update your
PATH
so that theprotoc
compiler can find the plugins:$ export PATH="$PATH:$(go env GOPATH)/bin"
获取示例代码
示例代码是 grpc-go repo 的一部分.
下载 zip 文件 和 unzip , 或克隆仓库:
$ git clone -b v1.48.0 --depth 1 https://github.com/grpc/grpc-go
进入目录:
$ cd grpc-go/examples/helloworld
Run 示例
进入 examples/helloworld
目录:
解析和运行 server code:
$ go run greeter_server/main.go
新开 terminal, 解析和运行 client code 查看 client output:
$ go run greeter_client/main.go Greeting: Hello world
恭喜! 您刚刚使用 gRPC 运行了一个 client-server 应用程序.
更新 gRPC 服务
在本节中,您将使用额外的服务器方法更新应用程序。使用 protocol buffers 定义 gRPC 服务。要了解有关如何在 .proto
文件中定义服务的更多信息,请参阅 基础教程。现在,您需要知道的是,server 和 client stub 都有一个 SayHello()
RPC方法,该方法从 client 获取一个 HelloRequest
参数,并从 server 返回一个 HelloReply
,该方法定义如下:
// 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;
}
打开 helloworld/helloworld.proto
和新建一个 SayHelloAgain()
方法, 同样的 request 和 response 类型:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (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;
}
记得保存文件!
重新生成 gRPC 代码
在使用新的服务方法之前,需要重新编译更新 .proto
文件。
在 examples/helloworld
目录中运行以下命令:
$ 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
文件,其中包含:
- 用于填充、序列化和检索
HelloRequest
和HelloReply
消息类型的代码。 - 生成的 client 和 server 代码。
更新并运行应用程序
您已经重新生成了 server 和 client 代码,但仍然需要在示例应用程序的 human-written 部分中实现和调用新方法。
更新服务器
打开 greeter_server/main.go
。转到并向其添加以下函数:
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello again " + in.GetName()}, nil
}
更新 client
greeter_client/main.go
在main()
函数体内添加如下代码:
r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
记得保存文件!
Run!
像以前一样运行 client 和 server 。从 examples/helloworld
目录执行以下命令:
Run the server:
$ go run greeter_server/main.go
从另一个终端运行 client 。这次,添加一个名称作为命令行参数:
$ go run greeter_client/main.go --name=Alice
您将看到以下输出::
Greeting: Hello Alice Greeting: Hello again Alice
下一步
- Learn how gRPC works in Introduction to gRPC and Core concepts.
- Work through the Basics tutorial.
- Explore the API reference.