博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GRPC---Go Quick Start
阅读量:5901 次
发布时间:2019-06-19

本文共 4809 字,大约阅读时间需要 16 分钟。

hot3.png

Before you begin

Prerequisites

Go version

gRPC works with Go 1.5 or higher.

$ go version

For installation instructions, follow this guide: 

Install gRPC

Use the following command to install gRPC.

$ go get google.golang.org/grpc

Install Protocol Buffers v3

Install the protoc compiler that is used to generate gRPC service code. The simplest way to do this is to download pre-compiled binaries for your platform(protoc-<version>-<platform>.zip) from here: 

  • Unzip this file.
  • Update the environment variable PATH to include the path to the protoc binary file.

Next, install the protoc plugin for Go

$ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

The compiler plugin, protoc-gen-go, will be installed in $GOBIN, defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler, protoc, to find it.

$ export PATH=$PATH:$GOPATH/bin

Download the example

The grpc code that was fetched with go get google.golang.org/grpc also contains the examples. They can be found under the examples dir: $GOPATH/src/google.golang.org/grpc/examples.

Build the example

Change to the example directory

$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld

gRPC services are defined in a proto file, which is used to generate a corresponding .pb.go. This file is already generated for the helloworld example code and can be found under this directory: $GOPATH/src/google.golang.org/grpc/examples/helloworld/helloworld

This helloworld.pb.go file contains:

  • Generated client and server code.
  • Code for populating, serializing, and retrieving our HelloRequest and HelloReply message types.

Try it!

To compile and run the server and client code, the go run command can be used. In the examples directory:

$ go run greeter_server/main.go

From a different terminal:

$ go run greeter_client/main.go

If things go smoothly, you will see the Greeting: Hello world in the client side output.

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in and . For now all you need to know is that both the server and the client “stub” have a SayHello RPC method that takes a HelloRequest parameter from the client and returns a HelloReply from the server, and that this method is defined like this:

// 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 greetingsmessage HelloReply {  string message = 1;}

Let’s update this so that the Greeter service has two methods. Make sure you are in the same examples dir as above ($GOPATH/src/google.golang.org/grpc/examples/helloworld)

Edit helloworld/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

// 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 greetingsmessage HelloReply {  string message = 1;}

Generate gRPC code

Next we need to update the gRPC code used by our application to use the new service definition. From the same examples dir as above ($GOPATH/src/google.golang.org/grpc/examples/helloworld)

$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

This regenerates the helloworld.pb.go with our new changes.

Update and run the application

We now have new generated server and client code, but we still need to implement and call the new method in the human-written parts of our example application.

Update the server

Edit greeter_server/main.go and add the following function to it:

func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {        return &pb.HelloReply{Message: "Hello again " + in.Name}, nil}

Update the client

Edit greeter_client/main.go to add the following code to the main function.

r, err = c.SayHelloAgain(context.Background(), &pb.HelloRequest{Name: name})if err != nil {        log.Fatalf("could not greet: %v", err)}log.Printf("Greeting: %s", r.Message)

Run!

Run the server

$ go run greeter_server/main.go

On a different terminal, run the client

$ go run greeter_client/main.go

You should see the updated output:

$ go run greeter_client/main.goGreeting: Hello worldGreeting: Hello again world

转载于:https://my.oschina.net/hutaishi/blog/845890

你可能感兴趣的文章
keepalived 原理,安装,配置
查看>>
乐在其中设计模式(C#) - 单例模式(Singleton Pattern)
查看>>
Tensorflow官方语音识别入门教程 | 附Google新语音指令数据集
查看>>
AssetBundle进阶内存优化(Unity 4.x)
查看>>
Windows Home Server 简体中文版安装和配置体验 - 海量图鉴
查看>>
GitHub 版本控制 项目托管 00 总体框架
查看>>
Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)
查看>>
Windows 8部署系列PART3:配置WDS服务器环境
查看>>
Ruby中写一个判断成绩分类的脚本
查看>>
《从零开始学Swift》学习笔记(Day 40)——析构函数
查看>>
Exchange2003-2010迁移系列之十,Exchange证书攻略
查看>>
使用NTFS权限保护数据安全
查看>>
infortrend ESDS RAID6故障后的数据恢复方案
查看>>
【STM32 .Net MF开发板学习-23】DHT11温湿度传感器通信(下)
查看>>
android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
查看>>
extmail集群的邮件负载均衡方案 [lvs dns postfix]
查看>>
SCCM2012SP1---资产管理和远程管理
查看>>
Java获取图片的宽高等信息
查看>>
用C++实现跨平台游戏开发之Irrlicht引擎
查看>>
六年程序生涯
查看>>