go实现grpc-快速开始

准备工作

  • Go, 最新版的

    如果不会安装看Getting Started.

  • Protocol buffer compilerprotocversion 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 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() 方法, 使用相同的请求和相应类型:

// 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 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
    

接下来

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值