Post

protoc 工具

gRPC

安装 protoc 工具

https://github.com/protocolbuffers/protobuf/releases

  • -I 参数:指定 import 路径,可以指定多个-I 参数,编译时按顺序查找,不指定时默认查找当前目录
  • –go_out :golang 编译支持,支持以下参数 - plugins=plugin1+plugin2 - 指定插件,目前只支持 grpc,即:plugins=grpc - M 参数 - 指定导入的.proto 文件路径编译后对应的 golang 包名(不指定本参数默认就是.proto 文件中 import 语句的路径) - import_prefix=xxx - 为所有 import 路径添加前缀,主要用于编译子目录内的多个 proto 文件,这个参数按理说很有用,尤其适用替代一些情况时的 M 参数,但是实际使用时有个蛋疼的问题导致并不能达到我们预想的效果,自己尝试看看吧 - import_path=foo/bar - 用于指定未声明 package 或 go_package 的文件的包名,最右面的斜线前的字符会被忽略 - 末尾 :编译文件路径 .proto 文件路径(支持通配符)

    完整实例

使用前安装:go get -u github.com/golang/protobuf/protoc-gen-go

1
2
3
4
5
6
7
8
9
10
11
12
protoc -I . --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=foo/,import_path=foo/bar:. ./*.proto


protoc -I . --go_out=plugins=grpc:. dict.protoc
protoc -I . --php_out=plugins=grpc:. zhly_pay.protoc
protoc --java_out=:. zhly_pay.protoc
protoc --js_out=:. zhly_pay.protoc
protoc -I=$DIR  yun_mao.protoc  \ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$OUT_DIR



protoc --js_out=import_style=commonjs,binary:. yun_mao.protoc

下载工具依赖

go get google.golang.org/grpc go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/

创建.protoc 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
syntax = "proto3";  //语法声明

package gRPCDemo; //包名

// Greeter 微服务
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// HelloRequest 请求数据格式
message HelloRequest {
  string name = 1;
}

// HelloReply 响应数据格式
message HelloReply {
  string message = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
syntax = "proto3";

package protoc;

service Greeter {
  // 1、 查找最空闲的 tcpcluster
 rpc LookupCluster(LookupClusterRequest) returns (LookupClusterResponse){}

  // 2、根据 clientid 发送数据。
    rpc SendOutByClientId(SendOutByClientIdRequest) returns (SendOutByClientIdResponse){}

  // 3、查找每个 tcpcluster 终端数
  rpc LookUpAllClusterNumber(LookUpAllClusterNumberRequest) returns(LookUpAllClusterNumberResponse){}

}


 // 1、 查找最空闲的 tcpcluster Request
message LookupClusterRequest{
    string args = 1;
}

 // 1、 查找最空闲的 tcpcluster Response
message LookupClusterResponse{
   string clientId = 1;
}

// 2、根据 clientid 发送数据  Request
message SendOutByClientIdRequest{
    string clientId = 1;
    string  mgs = 2;
}

// 2、根据 clientid 发送数据  Response
message SendOutByClientIdResponse{
    int64 code = 1;
}

// 3、查找每个 tcpcluster 终端数  Request
message LookUpAllClusterNumberRequest{

}

// 3、查找每个 tcpcluster 终端数  Response
message LookUpAllClusterNumberResponse{
        map<string, int64> number = 1;
}



grpcui

1
2
3
4
5
6
7
8
9
10
go get github.com/fullstorydev/grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui

验证下
grpcui -help
成功则会在 gopath/bin 目录下生成 grpcui

grpcui -bind 0.0.0.0 -port 8506 -plaintext 47.244.225.92:8502
            webiui地址          端口号             grpc地址
grpcui -bind 0.0.0.0 -port 8506 -plaintext 0.0.0.0:9002
This post is licensed under CC BY 4.0 by the author.