gRPC 教程

Linux安装gRPC

Brew安装

https://github.com/Linuxbrew/brew

  • 安装命令
1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
  • 安装开发工具
    1
    2
    3
    4
    Debian, Ubuntu, etc.
    sudo apt-get install build-essential
    Fedora, Red Hat, CentOS, etc.
    sudo yum groupinstall 'Development Tools'

对于Arch系则不需要

  • 增加环境变量

添加以下内容到.bashrc

1
2
3
echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >>~/.profile
echo 'export MANPATH="/home/linuxbrew/.linuxbrew/share/man:$MANPATH"' >>~/.profile
echo 'export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:$INFOPATH"' >>~/.profile

  • 安装GCC
1
brew install gcc
  • 安装gRPC
1
2
brew tap grpc/grpc
brew install --with-plugins grpc

定义接口文件

在Protocol Buffers服务接口的方法定义中是不能使用基本类型的,方法参数和返回值都必须是自定义的 message 类型。

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
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.fintend.fiatgw"; //生成的代码必须放在这个路径上,否则启动错误
//java.lang.IllegalArgumentException: service names != xxxx
// option java_outer_classname = "HelloWorldProto";
// option objc_class_prefix = "HLW";

package helloworld; //涉及生成的包名结构

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

生成序序列化和反序列化代码

格式:protoc -I=$SRC_DIR –java_out=$DST_DIR $SRC_DIR/jr.proto

例如,protoc -I=. –java_out=. helloworld.proto

生成通信服务相关代码

  • 编译插件protoc-gen-grpc-java

    1
    2
    3
    4
    5
    git clone https://github.com/grpc/grpc-java

    cd compiler

    ../gradlew install
  • 生成代码

1
protoc --plugin=protoc-gen-grpc-java=/path/protoc-gen-grpc-java --grpc-java_out=$DST_DIR --proto_path=$SRC_DIR $SRC_DIR/jr.proto

例如

1
protoc --plugin=protoc-gen-grpc-java=/home/sulang/git/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=. --proto_path=. any.proto

启动RPC服务器

Spring Boot 依赖库

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
<!-- gRPC -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>1.3.0-RELEASE</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf.nano</groupId>
<artifactId>protobuf-javanano</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<version>1.9.0</version>
<type>pom</type>
</dependency>

实现Service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@GrpcService(KYCServiceGrpc::class)
class KYCServiceImpl : KYCServiceGrpc.KYCServiceImplBase() {
override fun validateKYC(request: KYCRequest?, responseObserver: StreamObserver<KYCReply>?) {
val replyHeaders = ReplyHeaders.newBuilder()
replyHeaders.version = "v1"
replyHeaders.requestId = "KYC-${UUID.randomUUID()}"

val replyBody = ReplyBody.newBuilder()
replyBody.result = true

val kycReply = KYCReply.newBuilder()
kycReply.headers = replyHeaders.build()
kycReply.body = replyBody.build()

responseObserver?.onNext(kycReply.build())
responseObserver?.onCompleted()
}
}

application.properties 配置信息

1
2
3
# 默认地址是0.0.0.0:9090
grpc.server.port=
grpc.server.address=

多文件关联生成命令

int32

1
protoc accounting-common.proto accounting-order-service.proto --java_out=.

Client

Error: java.lang.ClassNotFoundException: org/eclipse/jetty/alpn/ALPN
Solution:

1
2
3
4
5
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.7.Final</version>
</dependency>

doc

/etc/pacman.conf

1
2
3
[home_estan_protoc-gen-doc_Arch_Extra]
SigLevel = Never
Server = https://download.opensuse.org/repositories/home:/estan:/protoc-gen-doc/Arch_Extra/$arch
1
2
pacman -Syu
pacman -S home_estan_protoc-gen-doc_Arch_Extra/protoc-gen-doc
1
protoc --plugin=protoc-gen-doc=./protoc-gen-doc --proto_path=./protos/ --doc_out=./doc --doc_opt=html,index.html protos/*.proto

Link:https://software.opensuse.org/download.html?project=home%3Aestan%3Aprotoc-gen-doc&package=protoc-gen-doc