Protocol Buffers
,是Google公司开发的一种数据描述语言,相似于XML可以将结构化数据序列化,可用于数据存储、通讯协议等方面。html
它不依赖于语言和平台而且可扩展性极强。现阶段官方支持C++
、JAVA
、Python
三种编程语言,但能够找到大量的几乎涵盖全部语言的第三方拓展包。java
google在2008年7月7号将其做为开源项目对外公布git
虽然Protocol Buffers
很早就被开源出来,被使用的频率并无Json
和XML
多,大多数被用于游戏开发协议,RPC
和即时通信.然而这样的数据交换利器比Json
和XML
的利处多太多了,但更小,更快,更简单github
该文章是继是时候该了解一波Protocol Buffers了[Android]后的Java后台SpringBoot快速集成Protocol Buffers编程
setup 1网络
配置 protobuf
,protobuf-java-util
,protobuf-java-format
依赖app
<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>
复制代码
注意:maven
protobuf-java-util
是protobuf
相关的工具类编程语言
protobuf-java-format
是能够将protobuf
转换为Json
格式转换器ide
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包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
下能找到生成的文件,收取成果
注意:
Protobuf Support
插件com\hk\protocolbuffer
目录是option java_package
指定的包名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/