开发环境:使用Intellij IDEA + Maven + Spring Boot 2.x + JDK 8
1.在项目的pom.xml文件下,引入protobuf的Jar开发包依赖;并且可以配置protobuf 的Maven插件,对编写的proto文件编译成Java文件。
<properties>
<protobuf.version>3.6.1</protobuf.version>
<grpc.version>1.16.1</grpc.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<!-- proto文件目录 -->
<protoSourceRoot>${project.basedir}/src/main/proto/protobuf</protoSourceRoot>
<!-- 生成的Java文件目录 -->
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
添加完protobuf的编译插件之后,可以在编辑器的右边看到插件操作,如下图:
通过点击protobuf:compile,就可以将编写的proto文件生成到对应的文件夹。这里上面配置的proto文件目录是
p
r
o
j
e
c
t
.
b
a
s
e
d
i
r
/
s
r
c
/
m
a
i
n
/
p
r
o
t
o
/
p
r
o
t
o
b
u
f
(
不
配
置
的
话
默
认
是
{project.basedir}/src/main/proto/protobuf(不配置的话默认是
project.basedir/src/main/proto/protobuf(不配置的话默认是{project.basedir}/src/main/proto),配置的生成的Java文件目录是
p
r
o
j
e
c
t
.
b
u
i
l
d
.
d
i
r
e
c
t
o
r
y
/
g
e
n
e
r
a
t
e
d
−
s
o
u
r
c
e
s
/
p
r
o
t
o
b
u
f
/
j
a
v
a
(
不
配
置
的
话
默
认
是
{project.build.directory}/generated-sources/protobuf/java(不配置的话默认是
project.build.directory/generated−sources/protobuf/java(不配置的话默认是{project.build.directory}/generated-sources/protobuf/java)。这个可以根据自己的需要进行指定。
2.为了支持网络协议的接收和发送以protobuf格式进行,我们需要为Spring Boot 配置protobuf的序列化和反序列化。
protobuf序列化的作用是将协议体以protobuf的格式response回去,反序列化是在构造发生协议请求时,用protobuf的格式。
具体的Java配置代码示例如下:
package com.dbfor.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@Configuration
public class ProtobufConfig {
/**
* protobuf 序列化
* @return
*/
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
/**
* protobuf 反序列化
* @param converter
* @return
*/
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter converter) {
return new RestTemplate(Collections.singletonList(converter));
}
}
3.接下来,在处理请求回复时,可以直接返回protobuf的对象类型MessageLite。程序示例如下:
@GetMapping(path = "/info/{id}")
public MessageLite taskInfoRequest(@PathVariable("id") long id) {
// ...
// return 对应的protobuf的对象类型MessageLite
}