docker学习笔记-----grpc 填坑记(二)

1 先来看一张整体结构图:


只要是grpc支持的语言,在不同的client或者server端,都可以用来编写程序。下面我们使用go语言来编写。

2 安装git和go 语言环境获取源码:

go get -u github.com/grpc/grpc-go/examples/helloworld/greeter_client
go get -u github.com/grpc/grpc-go/examples/helloworld/greeter_server


3  安装grpc 

go get google.golang.org/grpc
安装不下来,请参考 docker学习笔记-----grpc 填坑记(一)

安装protocol buffers compiler 

 go get -a github.com/golang/protobuf/protoc-gen-go

4 定义服务 .ptoto 文件

服务用来指定可以被远程调用的方法和参数以及返回值。我们使用的时协议buffer 接口定义语言(IDL)来定义服务(方法,参数,返回类型),然后client 和server都需要使用这个协议产生的接口代码  .pb.go文件 ,来写自己的具体方法。

syntax = "proto3";

option java_package = "io.grpc.examples";

package helloworld;

// The greeter 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;
}


5  产生client 和server 短的接口代码,

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

6 然后实现client 和server 的代码。

7 运行server ,在运行client 观察运行结果。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值