Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目
本项目基于Spring Boot 2.0.5+yidongnan/grpc-spring-boot-starter 2.0.1.RELEASE,通过grpc-lib,grpc-server,grpc-client 3个模块来模拟grpc的服务调用。
主要参考自yidongnan/grpc-spring-boot-starter和LogNet/grpc-spring-boot-starter的官方example
yidongnan项目的主要优点有:项目使用maven和gradle两种方式,grpc-client连接grpc-server的channel通过注解自动注入,较优雅。缺点是文档没LogNet的丰富。两者功能相近,所以我使用了yidognnan的starter。
本项目已上传至GitHub:https://github.com/linshenkx/grpc-springboot-lin
文章目录
一 项目结构
1 项目结构
根项目(com.linshen:grpc-springboot-lin:1.0-SNAPSHOT)下有3个子模块:
- grpc-lib:包含原始proto文件,并负责将其转换为java代码
- grpc-server:grpc服务端
- grpc-client : grpc客户端
2 模块功能
- 相互关系
根项目负责依赖版本管理,grpc-server和grpc-client都依赖grpc-lib的接口,其中,grpc-server提供了接口的具体实现,而grpc-client则调用了grpc-server的实现,他们通过grpc-lib来确定使用规范(接口即契约) - 对应MVC关系
- grpc-lib就是service(接口,为提供实现)
- grpc-server就相当于serviceImpl(service的实现类)
- grpc-client就是controller的角色.
- 框架依赖
- grpc-lib需要将proto文件转换成java类,故需依赖于protobuf-maven-plugin插件,另外还需要io.grpc下的grpc-all,否则生成的java类会提示缺少依赖
- grpc-server 负责提供rpc服务,不需要spring-web支持
- grpc-client 调用rpc服务
二 项目搭建
0 编写根项目pom文件
如下,根项目主要负责定义项目子模块,全局属性和版本管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linshen</groupId>
<artifactId>grpc-springboot-lin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<!--引入的子模块-->
<modules>
<module>grpc-lib</module>
<module>grpc-server</module>
<module>grpc-client</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.0.5.RELEASE</spring-boot.version>
<net-devh-grpc.version>2.0.1.RELEASE</net-devh-grpc.version>
</properties>
<dependencyManagement>
<dependencies>
<!--依赖维护平台,在添加第三方依赖的时候,不需要写版本号,它能够自动帮我们挑选一个最优的版本-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--公共grpc模块-->
<dependency>
<groupId>com.linshen</groupId>
<artifactId>grpc-lib</artifactId>
<version>${project.version}</version>
</dependency>
<!--核心grpc-spring-boot依赖-->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>${net-devh-grpc.version}</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>${net-devh-grpc.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
1 创建grpc-lib模块
创建maven管理的java项目,pom文件如下
proto文件目录是${project.basedir}/src/main/proto
生成的java代码在${project.basedir}/src/main/java
使用maven指令compile
可完成代码生成,或直接在IDE里用proto插件运行compile
和compile-custom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>grpc-lib</artifactId>
<packaging>jar</packaging>
<name>grpc-lib</name>
<parent>
<groupId>com.linshen</groupId>
<artifactId>grpc-springboot-lin</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<os.plugin.version>1.6.0</os.plugin.version>
<grpc.version>1.15.1</grpc.version>
<protoc.version>3.6.1</protoc.version>
<protobuf.plugin.version>0.6.1</protobuf.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os.plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<!--默认值-->
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<!--默认值-->
<!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
<clearOutputDirectory>false</clearOutputDirectory>
<!--更多配置信息可以查看https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html-->
</configuration>
<executions>
<execution>
<!--在执行mvn compile的时候会执行以下操作-->
<phase>compile</phase>
<goals>
<!--生成OuterClass类-->
<goal>compile</goal>
<!--生成Grpc类-->
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 创建grpc-server模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>grpc-server</artifactId>
<packaging>jar</packaging>
<name>grpc-server</name>
<parent>
<groupId>com.linshen</groupId>
<artifactId>grpc-springboot-lin</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.linshen</groupId>
<artifactId>grpc-lib</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3 创建grpc-client模块
可以看到,这里比grpc-server多了spring-web依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>grpc-client</artifactId>
<packaging>jar</packaging>
<name>grpc-client</name>
<parent>
<groupId>com.linshen</groupId>
<artifactId>grpc-springboot-lin</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.linshen</groupId>
<artifactId>grpc-lib</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三 功能实现
1 在grpc-lib模块利用proto文件生成java代码
(1) 编写proto文件
这里用的greeter.proto内容如下
syntax = "proto3";
option java_package = "com.linshen.grpc.lib";
// The greeter 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;
}
(2) 生成java代码
根据上文pom文件的设置,将proto文件放到src/main/proto目录下,运行protobuf插件的compile和compile-custom功能,生成java代码到src/main/java目录下。
生成后文件目录如下
2 在grpc-server模块实现接口
(1) 编写application文件
首先在application.yml文件指定应用名和grpc端口,如下
spring:
application:
name: local-grpc-server
grpc:
server:
port: 9898
(2) 编写GreeterService类
@Slf4j
@GrpcService(GreeterOuterClass.class)
public class GreeterService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
String message = "Hello " + request.getName();
final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage(message);
responseObserver.onNext(replyBuilder.build());
responseObserver.onCompleted();
log.info("Returning " +message);
}
}
3 在grpc-client模块调用服务
(1) 编写application文件
grpc需要开放接口,故需在application.yml指定端口,另外还需指定grpc-server地址和端口,这里将grpc-server的地址以环境变量的方式开放出来,默认在本地。yml文件如下
server:
port: 9090
spring:
application:
name: local-grpc-client
grpc:
client:
local-grpc-server:
host:
- ${LOCAL-GRPC-HOST:127.0.0.1}
port:
- 9898
enableKeepAlive: true
keepAliveWithoutCalls: true
(2)编写GrpcClientService服务类
注意,@GrpcClient根据value将yml文件里的配置绑定到Channel
@Service
public class GrpcClientService {
@GrpcClient("local-grpc-server")
private Channel serverChannel;
public String sendMessage(String name) {
GreeterGrpc.GreeterBlockingStub stub= GreeterGrpc.newBlockingStub(serverChannel);
GreeterOuterClass.HelloReply response = stub.sayHello(GreeterOuterClass.HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
}
}
(3)编写GrpcClientController类
@RestController
public class GrpcClientController {
@Autowired
private GrpcClientService grpcClientService;
@RequestMapping("/")
public String printMessage(@RequestParam(defaultValue = "Michael") String name) {
return grpcClientService.sendMessage(name);
}
}
四 项目启动并测试
依次启动grpc-server和grpc-client,打开http://localhost:8080/或http://localhost:8080/?name=lin即可看到结果
同时grpc-server项目日志输出如下
需要注意的是,如果想要执行插件的jar打包,run启动等命令,需要先在根项目下运行mvn install
,注意不是mvn install-install
,否则会出现·Failed to collect dependencies·问题
五 其他
gRPC-Java指导:https://grpc.io/docs/tutorials/basic/java.html
gRPC-Java的GitHub地址:https://github.com/grpc/grpc-java
yidongnan/grpc-spring-boot-starter的GitHub地址:https://github.com/yidongnan/grpc-spring-boot-starter
LogNet/grpc-spring-boot-starter的GitHub地址:https://github.com/LogNet/grpc-spring-boot-starter
gRPC 官方文档中文版1.0:https://doc.oschina.net/grpc
Maven Protocol Buffers Plugin 插件指南:https://www.xolstice.org/protobuf-maven-plugin/index.html