当前位置: 首页 > news >正文

go实现grpc-快速开始

准备工作

  • Go, 最新版的

    如果不会安装看Getting Started.

  • Protocol buffer compilerprotoc, version 3.

    想要安装, 请读Protocol Buffer Compiler Installation.

  •  为 protocol compiler安装Go plugins:

    1. 想要安装运行以下命令:

      $ 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
      
    2. 更新环境变量好让protocol 可以直接调用:

      $ export PATH="$PATH:$(go env GOPATH)/bin"

记得保存.

获取例子代码

这些代码是 grpc-go 仓库的一部分.

  1. 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
    
  2.  进入quick start example 目录:

    $ cd grpc-go/examples/helloworld

运行范例

从 examples/helloworld 目录:

1.编译并执行服务端代码:

$ go run greeter_server/main.go
  1. 再开一个终端编译并运行客户端代码

  2. output:

  3. $ 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 user's 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 user's 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_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 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下运行下面的命令:

  1. 运行 server:

    $ go run greeter_server/main.go
    
  2. 运行另一个终端,以下命令,这次我们增加了一个参数:

  3. $ 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.
http://www.lryc.cn/news/169401.html

相关文章:

  • linux上的init 0-6指令作用以及一些快捷键和系统指令
  • Mixin 混入
  • pycharm快捷键
  • 【面试刷题】——Linux基础命令
  • 第四步 Vue2 配置ESLint
  • [.NET学习笔记] - Thread.Sleep与Task.Delay在生产中应用的性能测试
  • 【单线图的系统级微电网仿真】基于 PQ 的可再生能源和柴油发电机组微电网仿真(Simulink)
  • 人脸识别技术应用安全管理规定(试行)|企业采用人脸打卡方式,这4条规定值得关注
  • leetcode 817. 链表组件(java)
  • 分布式事务基础理论
  • 《打造高可用PostgreSQL:策略与工具》
  • 【八大经典排序算法】快速排序
  • vue 父组件给子组件传递一个函数,子组件调用父组件中的方法
  • docker 获取Nvidia 镜像 | cuda |cudnn
  • uTool快捷指令
  • R reason ‘拒绝访问‘的解决方案
  • 许战海战略文库|品类缩量时代:制造型企业如何跨品类打造份额产品?
  • BIT-4-数组
  • L9945的H桥续流模式
  • Ubuntu20.04安装Nvidia显卡驱动、CUDA11.3、CUDNN、TensorRT、Anaconda、ROS/ROS2
  • linux下使用crontab定时器,并且设置定时不执行的情况,附:项目启动遇到的一些问题和命令
  • linux下二进制安装docker最新版docker-24.0.6
  • 计算机视觉 01(介绍)
  • Java下部笔记
  • 链表基本操作
  • Linux学习笔记-Ubuntu系统下配置用户ssh只能访问git仓库
  • 央媒发稿不能改?媒体发布新闻稿有哪些注意点
  • 计算机竞赛 深度学习 opencv python 公式识别(图像识别 机器视觉)
  • KPM算法
  • 全流程GMS地下水数值模拟及溶质(包含反应性溶质)运移模拟技术教程