前言
Protocol Buffers
,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。
它不依赖于语言和平台并且可扩展性极强。现阶段官方支持C++
、JAVA
、Python
三种编程语言,但可以找到大量的几乎涵盖所有语言的第三方拓展包。
google在2008年7月7号将其作为开源项目对外公布
虽然Protocol Buffers
很早就被开源出来,被使用的频率并没有Json
和XML
多,大多数被用于游戏开发协议,RPC
和即时通讯.然而这样的数据交换利器比Json
和XML
的利处多太多了,但更小,更快,更简单
该文章是继是时候该了解一波Protocol Buffers了[Android]后的Java后台SpringBoot快速集成Protocol Buffers
SpringBoot中快速使用Protocol Buffers
(一)SpringBoot 环境下Maven配置 Protocol Buffers编译环境
-
setup 1
配置
protobuf
,protobuf-java-util
,protobuf-java-format
依赖<dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.googlecode.protobuf-java-format</groupId> <artifactId>protobuf-java-format</artifactId> <version>1.4</version> </dependency> </dependencies> 复制代码
注意:
-
protobuf-java-util
是protobuf
相关的工具类 -
protobuf-java-format
是可以将protobuf
转换为Json
格式转换器 -
setup 2
配置编译器,编译MAven插件,文件输出环境
<plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.0</version> <extensions>true</extensions> <configuration> <!--默认值--> <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot> <!--默认值--> <!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>--> <!--<outputDirectory>${project.build.sourceDirectory}</outputDirectory>--> <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件--> <clearOutputDirectory>false</clearOutputDirectory> <!--默认值--> <temporaryProtoFileDirectory>${project.build.directory}/protoc-dependencies </temporaryProtoFileDirectory> <!--更多配置信息可以查看https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html--> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> <!--也可以设置成局部变量,执行compile或test-compile时才执行--> <!--<configuration>--> <!--<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>--> <!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>--> <!--<temporaryProtoFileDirectory>${project.build.directory}/protoc-dependencies</temporaryProtoFileDirectory>--> <!--</configuration>--> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>add-protobuf-generate-sources</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>target/generated-sources/protobuf/java</source> </sources> </configuration> </execution> <execution> <id>add-protobuf-generate-test-sources</id> <phase>generate-sources</phase> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>target/generated-test-sources/protobuf/java</source> </sources> </configuration> </execution> </executions> </plugin> 复制代码
-
注意:
- 这里的指定的输出路径是
arget/generated-sources/protobuf/java
,该路径下的java文件都是直接使用和打入jar包和war包
(二)java项目中编写.proto文件,生成java文件
-
setup 1
java项目中编写.proto文件了.例如: 新建 Result.proto
package com.hk.protocolbuffer; // 关注1:包名 option java_package = "com.hk.protocolbuffer"; option java_outer_classname = "Result"; // 关注2:option选项 // 关注3:消息模型 message AppResult { optional string message = 1; required string data = 2; optional string version = 3; optional string mobile = 5; optional int32 code= 6[default = 500]; optional string email = 7; } 复制代码
java项目中编写.proto文件了.例如: 新建 Demo.proto
package protocobuff_Demo; // 关注1:包名 option java_package = "com.hk.protocolbuffer"; option java_outer_classname = "Demo"; // 关注2:option选项 // 关注3:消息模型 // 下面详细说明 // 生成 Person 消息对象(包含多个字段,下面详细说明) message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; } 复制代码
-
setup 2
使用mvn clean install 命令 构建,在项目的
arget/generated-sources/protobuf/java
下能找到生成的文件,收取成果注意:
- 如果文件无法被idea 识别,请安装
Protobuf Support
插件 com\hk\protocolbuffer
目录是option java_package
指定的包名- .proto语法参照: developers.google.com/protocol-bu…
- 如果文件无法被idea 识别,请安装
(三)Java项目中SpringBoot配合 Protobuf 使用
-
setup 1
配置
ProtobufHttpMessageConverter
,其他的基本配SpringBoot都自动配置好了,直接使用就可以了/** * This example demonstrates serving up REST payloads encoded using Google Protocol Buffers. */ @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean ProtobufHttpMessageConverter protobufHttpMessageConverter() { return new ProtobufHttpMessageConverter(); } } 复制代码
-
setup 2
使用举例
@RestController @RequestMapping("/app") public class ApiRegisterController { @Resource(name = "appUserService") private AppUserService appUserService; @PostMapping("register2") public Demo.Person register2(@RequestBody Result.AppResult appBaseResult) throws Exception { return Demo.Person.newBuilder().addPhone(Demo.Person.PhoneNumber.newBuilder().setNumber(appBaseResult.getData()).build()).setId(1).setName("张三").build(); } } 复制代码
由于protobuf
调试比较头痛的问题,我在网上找到了一些调试工具(欢迎补充):
结束
相关文章
第一篇-网络篇:
第二篇-Retrofit源码解析
第三篇-Android组件化和快速实现MVP
第三篇-是时候该了解一波Protocol Buffers了[Android]
第三篇-是时候该了解一波Protocol Buffers了[Java]
更新中....
关于个人
Github:github.com/chengzichen
CSDN : blog.csdn.net/chengzichen…
个人博客 : chengzichen.github.io/