go使用grpc

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sureSand/article/details/82858047
  1. 前言

最近需要使用grpc框架,然后到网上找资料,在此我强烈鄙视那些复制粘贴的家伙,第一步就错了

go get google.golang.org/grpc 

这个根本安装不起,是官方将代码迁移了,会报错:

package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc"(https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

  1. 安装grpc
    以下操作均在GOPATH下进行:
mkdir -p src/google.golang.org
cd src/google.golang.org
git clone https://github.com/grpc/grpc-go grpc
cd -
## 另外, grpc依赖的其他包需要一并下载
mkdir -p src/golang.org/x
cd src/golang.org/x
git clone https://github.com/golang/net
git clone https://github.com/golang/text
cd -
  1. 安装grpc插件
go get -u github.com/golang/protobuf/protoc-gen-go
## 不能直接访问google.golang.org网址时, 从github下载然后放到google.golang.org目录
mkdir -p src/google.golang.org/
cd src/google.golang.org
git clone https://github.com/google/go-genproto genproto
  1. 安装protoc
    去这里protoc下载

下载对应的protoc,我这里下的是protoc-3.6.1-win32.zip

下好之后解压就行,然后把bin里面的protoc.exe加入到环境变量path中

  1. 运行go grpc的hello world
    grpc自带一个示例:
$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
  • 其中helloworld中的helloworld.proto是用来生成 gRPC 客户端和服务器端的接口,message 是代表数据结构(里面可以包括不同类型的成员变量,包括字符串、数字、数组、字典……),service代表 RPC 接口。变量后面的数字是代表进行二进制编码时候的提示信息,1~15 表示热变量,会用较少的字节来编码。另外,支持导入。默认所有变量都是可选的(optional),repeated 则表示数组。主要 service rpc 接口只能接受单个 message 参数,返回单个 message;
  • helloworld.pb.go是通过protoc生成的
  • 然后green_client和green_server分别是grpc调用的客户端和服务端

在helloworld目录中:
运行:$ go run greeter_server/main.go

打开另一个的终端,到相同的目录下:
运行:$ go run greeter_client/main.go

你会看到Greeting: Hello world客户端的输出。

  • 更新rpc服务
  • 在helloworld.proto添加一个新的方法
// 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代码,即helloworld.pb.go
    在($GOPATH/src/google.golang.org/grpc/examples/helloworld)这个目录,运行以下命令,将重新生成helloworld.pb.go:
$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

可以观察到helloworld.pb.go已经多了一个SayHelloAgain的方法

  • 更新并重新运行程序
    编辑greeter_server/main.go并添加以下函数:

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

更新客户端
编辑greeter_client/main.go将以下代码添加到主函数的最后。

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)

运行服务器

$ go run greeter_server/main.go

在不同的终端上运行客户端

$ go run greeter_client/main.go

输出:

Greeting: Hello world
Greeting: Hello again world

猜你喜欢

转载自blog.csdn.net/sureSand/article/details/82858047
今日推荐