网站建设沧州,中国企业网信息查询,爱站挖词,推广营销外包准备工作 Go, 最新版的 如果不会安装看Getting Started. Protocol buffer compiler, protoc, version 3. 想要安装, 请读Protocol Buffer Compiler Installation. 为 protocol compiler安装Go plugins: 想要安装运行以下命令: $ go install google.golang.org/protobuf/cmd/…准备工作 Go, 最新版的 如果不会安装看Getting Started. Protocol buffer compiler, protoc, version 3. 想要安装, 请读Protocol Buffer Compiler Installation. 为 protocol compiler安装Go plugins: 想要安装运行以下命令: $ go install google.golang.org/protobuf/cmd/protoc-gen-gov1.28
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpcv1.2更新环境变量好让protocol 可以直接调用: $ export PATH$PATH:$(go env GOPATH)/bin
记得保存.
获取例子代码
这些代码是 grpc-go 仓库的一部分. Download the repo as a zip file and unzip it,或者直接clone它: $ git clone -b v1.58.0 --depth 1 https://github.com/grpc/grpc-go进入quick start example 目录: $ cd grpc-go/examples/helloworld 运行范例
从 examples/helloworld 目录:
1.编译并执行服务端代码:
$ go run greeter_server/main.go再开一个终端编译并运行客户端代码 output: $ go run greeter_client/main.go
Greeting: Hello world恭喜! 你刚才成功地运行了client-server grpc应用
更新刚才的gRPC服务 接下来你将更新这个应用使用一个额外的服务方法。grpc服务使用protocol buffers.想要学习更多关于怎样在.proto文件中定义一个服务请看 Basics tutorial. 现在,你只需要知道server端和客户端使用一个SayHello() RPC方法使得客户端获取到了服务端的方法返回的响应方法定义像是
// The greeting service definition.
service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}
}// The request message containing the users name.
message HelloRequest {string name 1;
}// The response message containing the greetings
message HelloReply {string message 1;
}打开helloworld/helloworld.proto 然后 添加一个新的SayHelloAgain() 方法, 使用相同的请求和相应类型:
// The greeting service definition.
service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}// Sends another greetingrpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}// The request message containing the users name.
message HelloRequest {string name 1;
}// The response message containing the greetings
message HelloReply {string message 1;
}记得保存
重新生成 gRPC 代码
在你能用新的服务端方法前, 你得先重新生成 .proto file.
因为仍旧处在 examples/helloworld 目录, 运行接下来的命令:
$ protoc --go_out. --go_optpathssource_relative \--go-grpc_out. --go-grpc_optpathssource_relative \helloworld/helloworld.proto这将重新生成helloworld/helloworld.pb.go 和 helloworld/helloworld_grpc.pb.go files, 其中包含:
Code for populating, serializing, and retrieving HelloRequest and HelloReply message types.Generated client and server code.
更新并运行程序 你已经生成了server和cilent代码但你人就需要实现你手写的那些方法
更新服务端
打开 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
}更新客户端
打开 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())记得保存.
运行!
运行你之前写好的程序.在examples/helloworld directory下运行下面的命令 运行 server: $ go run greeter_server/main.go运行另一个终端以下命令这次我们增加了一个参数: $ go run greeter_client/main.go --nameAlice你会看到如下的输出: 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.