zoukankan      html  css  js  c++  java
  • [golang grpc] 框架介绍

    官方网站


    http://www.grpc.io/

    http://www.grpc.io/docs/quickstart/go.html

    grpc安装


    • go安装

           目前grpc需要go 1.5以上版本支持。go安装可以参考:http://www.cnblogs.com/heartchord/p/5127503.html

    • protocol buffer安装

           protocol buffer和go插件安装可以参考:http://www.cnblogs.com/heartchord/p/5337863.html

    • grpc安装

    go get google.golang.org/grpc

    • 官方例子测试

           grpc包下自带测试例子,路径为google.golang.org/grpc/examples,我们使用例子下的helloworld工程进行测试。

          

    ▶ 编译server和client:

    go install google.golang.orggrpcexampleshelloworldgreeter_server
    go install google.golang.orggrpcexampleshelloworldgreeter_client

    ▶ 开启两个控制台分别运行greeter_server.exe和greeter_client.exe:

          

          

    helloworld工程分析


    • .proto和.pb.go文件

           使用过protobuf就会知道,无论什么工程都需要一个.proto文件来定义数据的结构,该文件经过protoc.exe输出即可得到对应语言的源代码文件,例如c++得到的就是xxxx.pb.h和xxxx.pb.cc文件。而对于golang,则需要上述的插件支持,对应得到的是xxxx.pb.go文件。

           官方文档:

           https://developers.google.com/protocol-buffers/docs/proto3

           https://developers.google.com/protocol-buffers/docs/gotutorial

           

    ▶ proto文件中HelloRequest和HelloReply消息将被转换为go语言结构体,并自动添加了一些成员函数。

    ▶ proto文件中Greeter服务将自动生成GreeterClient和GreeterServer接口,其中GreeterClient将拥有一个内部类型greeterClient,greeterClient定义了SayHello的具体行为;而GreeterServer中的SayHello需要自行定义。

    ▶ greeterClient定义的SayHello具体行为核心一点为调用服务端的SayHello函数:

          

    ▶ 服务端需要定义SayHello的具体行为:

            

    • pb.go文件生成命令

    // cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
    protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

          

  • 相关阅读:
    linux 常用命令
    git 常见命令
    合并两个有序链表---python
    Code Contract for .NET
    Kruskal最小生成树算法
    逻辑-哲学
    停机问题
    逆向工程
    .net framework
    python 类库
  • 原文地址:https://www.cnblogs.com/heartchord/p/5535108.html
Copyright © 2011-2022 走看看