Springboot集成ProtoBuf的實(shí)例
Springboot集成ProtoBuf
ProtoBuf是一種序列化和解析速度遠(yuǎn)高于JSON和XML的數(shù)據(jù)格式,項(xiàng)目中使用了CouchBase作為緩存服務(wù)器,從數(shù)據(jù)庫中拿到數(shù)據(jù)后通過protobuf序列化后放入CouchBase作為緩存,查詢數(shù)據(jù)的時(shí)候解壓并反序列化成數(shù)據(jù)對象,下面是ProtoBuf的具體使用方法
1、pom.xml引入相關(guān)依賴
<dependency> ? ? <groupId>com.google.protobuf</groupId> ?? ?<artifactId>protobuf-java</artifactId> ?? ?<version>3.5.0</version> </dependency> <dependency> ?? ?<groupId>io.protostuff</groupId> ?? ?<artifactId>protostuff-core</artifactId> ?? ?<version>1.4.0</version> </dependency> <dependency> ?? ?<groupId>io.protostuff</groupId> ?? ?<artifactId>protostuff-runtime</artifactId> ?? ?<version>1.4.0</version> </dependency>
2、新建序列化工具類ProtoBufUtil.java
package com.xrq.demo.utils;?
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
?
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisStd;
?
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
?
/**
?* ProtoBufUtil 轉(zhuǎn)換工具類
?*?
?* @author XRQ
?*
?*/
public class ProtoBufUtil {
?? ?private static Logger log = LoggerFactory.getLogger(ProtoBufUtil.class);
?? ?private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
?
?? ?private static Objenesis objenesis = new ObjenesisStd(true);
?
?? ?@SuppressWarnings("unchecked")
?? ?private static <T> Schema<T> getSchema(Class<T> cls) {
?? ??? ?Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
?? ??? ?if (schema == null) {
?? ??? ??? ?schema = RuntimeSchema.createFrom(cls);
?? ??? ??? ?if (schema != null) {
?? ??? ??? ??? ?cachedSchema.put(cls, schema);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return schema;
?? ?}
?
?? ?public ProtoBufUtil() {
?? ?}
?
?? ?@SuppressWarnings({ "unchecked" })
?? ?public static <T> byte[] serializer(T obj) {
?? ??? ?Class<T> cls = (Class<T>) obj.getClass();
?? ??? ?LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
?? ??? ?try {
?? ??? ??? ?Schema<T> schema = getSchema(cls);
?? ??? ??? ?return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?log.error("protobuf序列化失敗");
?? ??? ??? ?throw new IllegalStateException(e.getMessage(), e);
?? ??? ?} finally {
?? ??? ??? ?buffer.clear();
?? ??? ?}
?? ?}
?
?? ?public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
?? ??? ?try {
?? ??? ??? ?T message = (T) objenesis.newInstance(clazz);
?? ??? ??? ?Schema<T> schema = getSchema(clazz);
?? ??? ??? ?ProtostuffIOUtil.mergeFrom(bytes, message, schema);
?? ??? ??? ?return message;
?? ??? ?} catch (Exception e) {
?? ??? ??? ?log.error("protobuf反序列化失敗");
?? ??? ??? ?throw new IllegalStateException(e.getMessage(), e);
?? ??? ?}
?? ?}
?
}3、新建實(shí)體類User.java
注:重點(diǎn)是@Tag標(biāo)簽需要按照順序往下排,如果需要新增字段,只能接著往下排,不能改變已存在的標(biāo)簽序號
package com.xrq.demo.bo;?
import java.io.Serializable;
import java.util.Date; ?
import io.protostuff.Tag;
?
/**
?* 用戶信息類
?*?
?* @ClassName: User
?* @author XRQ
?* @date 2019年4月30日
?*/
public class User implements Serializable
{
? ? private static final long serialVersionUID = 1L;
?
? ? // 用戶ID
? ? @Tag(1)
? ? private int userId;
?
? ? // 用戶類型
? ? @Tag(2)
? ? private int userTypeId;
?
? ? // 用戶名?
? ? @Tag(3)
? ? private String userName;
?
? ? // 創(chuàng)建時(shí)間
? ? @Tag(4)
? ? private Date createDateTime;?
? ? public int getUserId()
? ? {
?
? ? ? ? return userId;
? ? }
?
? ? public void setUserId(int userId)
? ? {
?
? ? ? ? this.userId = userId;
? ? }
?
? ? public int getUserTypeId()
? ? {
?
? ? ? ? return userTypeId;
? ? }
?
? ? public void setUserTypeId(int userTypeId)
? ? {
?
? ? ? ? this.userTypeId = userTypeId;
? ? }
?
? ? public String getUserName()
? ? {
?
? ? ? ? return userName;
? ? }
?
? ? public void setUserName(String userName)
? ? {
?
? ? ? ? this.userName = userName;
? ? }
?
? ? public Date getCreateDateTime()
? ? {
?
? ? ? ? return createDateTime;
? ? }
?
? ? public void setCreateDateTime(Date createDateTime)
? ? {
?
? ? ? ? this.createDateTime = createDateTime;
? ? }
}4、使用方式
User user = new User();
user.setUserId(1);
user.setUserTypeId(1);
user.setUserName("XRQ");
user.setCreateDateTime(new Date());
//序列化成ProtoBuf數(shù)據(jù)結(jié)構(gòu)
byte[] userProtoObj= ProtoBufUtil.serializer(userInfo)
?
//ProtoBuf數(shù)據(jù)結(jié)構(gòu)反序列化成User對象
User newUserObj = ProtoBufUtil.deserializer(userProtoObj, User.class))ProtoBuf+Java+Springboot+IDEA應(yīng)用
什么是Protobuf
1.Google Protocol Buffer( 簡稱 Protobuf) 是 Google 公司內(nèi)部的混合語言數(shù)據(jù)標(biāo)準(zhǔn);
2.Protocol Buffers 是一種輕便高效的結(jié)構(gòu)化數(shù)據(jù)存儲格式,可以用于結(jié)構(gòu)化數(shù)據(jù)串行化,或者說序列化。它很適合做數(shù)據(jù)存儲或 RPC 數(shù)據(jù)交換格式??捎糜谕ㄓ崊f(xié)議、數(shù)據(jù)存儲等領(lǐng)域的語言無關(guān)、平臺無關(guān)、可擴(kuò)展的序列化結(jié)構(gòu)數(shù)據(jù)格式;
3.形式為.proto結(jié)尾的文件;
應(yīng)用環(huán)境
近期接觸公司的網(wǎng)約車接口對接項(xiàng)目,第三方公司限定了接口的數(shù)據(jù)用protobuf格式序列化后,通過AES128加密后傳輸。
開發(fā)環(huán)境
Java+Spring boot+IDEA+Windows
新建Spring boot項(xiàng)目 在IDEA開發(fā)工具中安裝protobuf插件

添加配置maven依賴(可能一開始自己的項(xiàng)目中存在固有的配置,不要?jiǎng)h除,在對應(yīng)的地方添加下面的配置即可,不需要修改)
<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.4.0</version> </dependency>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
寫項(xiàng)目對應(yīng)的.proto文件(這里貼一部分代碼)
// 版本號
syntax = "proto2";
// 打包路徑
option java_package="XXX1";
// Java文件名
option java_outer_classname="XXX2";
// 屬性信息
message BaseInfo {
// 公司標(biāo)識
required string CompanyId = 1;
// 公司名稱
required string CompanyName = 2;
// 操作標(biāo)識
required uint32 Flag = 3;
// 更新時(shí)間
required uint64 UpdateTime = 4;
}
.proto文件存在項(xiàng)目路徑

生成.java文件方式(點(diǎn)擊項(xiàng)目中的.proto文件,找到對應(yīng)的maven依賴,雙擊標(biāo)識2對應(yīng)的protobuf:compile文件)

運(yùn)行成功之后在對應(yīng)路徑下查看生成的.java文件

生成的Java文件即可使用(如果在業(yè)務(wù)中.proto文件很大,生成的Java大小超出IDEA默認(rèn)的2.5M,生成的Java文件將被IDEA誤認(rèn)為不是Java文件,導(dǎo)致生成的Java文件不可使用,這時(shí)需要修改IDEA的配置文件,將默認(rèn)的大小修改,重啟IDEA即可) 進(jìn)行數(shù)據(jù)序列化
List<BaseInfo> baseInfoList = baseInfoMapper.selectAll();
XXX2.OTIpcList.Builder listBuilder = XXX2.OTIpcList.newBuilder();
XXX2.OTIpc.Builder otipcBuilder = XXX2.OTIpc.newBuilder();
otipcBuilder.setCompanyId(ProjectConstant.COMPANY_ID);
otipcBuilder.setSource(ProjectConstant.COMPANY_SOURCE);
otipcBuilder.setIPCType(OTIpcDef.IpcType.baseInfoCompany);
for(int i=0;i<baseInfoList .size();i++){
try{
XXX2.BaseInfo.Builder baseInfoBuilder = XXX2.BaseInfo.newBuilder();
baseInfoBuilder .setCompanyId(baseInfoList .get(i).getCompanyid());
baseInfoBuilder .setCompanyName(baseInfoList .get(i).getCompanyname());
baseInfoBuilder .setFlag(baseInfoList .get(i).getFlag());
baseInfoBuilder .setUpdateTime(baseInfoList .get(i).getUpdatetime());
for(int j=0;j<10;j++) {
otipcBuilder.addBaseInfo(baseInfoBuilder .build());
}
}
catch (Exception e){
LoggerUtils.info(getClass(),e.getMessage());
}
}
listBuilder.addOtpic(otipcBuilder);
進(jìn)行數(shù)據(jù)AES128位加密
public static String sendScreate(OTIpcDef.OTIpcList.Builder listBuilder) {
String screateKeyString = getSecretKey();
String screateKey = screateKeyString.split(";")[1];
String screateValue = screateKeyString.split(";")[0];
OTIpcDef.OTIpcList list = listBuilder.build();
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
list.writeTo(output);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = null;
try{
byte[] keyByte = new byte[screateValue.length()/2];
for(int j=0;j<screateValue.length()/2;j++) {
byte b = (byte) ((Integer.valueOf(String.valueOf(screateValue.charAt(j*2)), 16) << 4) |
Integer.valueOf(String.valueOf(screateValue.charAt(j*2+1)), 16));
keyByte[j] = b;
}
Key secretKey= new SecretKeySpec(keyByte,"AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher= Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
data = cipher.doFinal(output.toByteArray());
sendPostJSON(screateKey, data);
return "success";
} catch(Exception e){
e.printStackTrace();
}
return null;
}
小結(jié)一下:經(jīng)驗(yàn)證,Protobuf 序列化相比XML,JSON性能更好,在Protobuf 官網(wǎng)看到在創(chuàng)建對象,將對象序列化為內(nèi)存中的字節(jié)序列,然后再反序列化的整個(gè)過程中相比其他相似技術(shù)的性能測試結(jié)果圖,但是在通用性上會存在局限性,功能相對簡單,不適合用來描述數(shù)據(jù)結(jié)構(gòu)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)
這篇文章主要介紹了Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
SpringBoot使用FFmpeg實(shí)現(xiàn)M3U8切片轉(zhuǎn)碼播放
FFmpeg是一個(gè)開源跨平臺的多媒體處理工具套件,它支持音頻、視頻文件的編碼、解碼、流媒體傳輸以及轉(zhuǎn)換等多種操作,本文小編給大家介紹了SpringBoot使用FFmpeg實(shí)現(xiàn)M3U8切片轉(zhuǎn)碼播放的操作,需要的朋友可以參考下2024-08-08
MyBatisPlus條件構(gòu)造器圖文實(shí)例詳解
這篇文章主要介紹了MyBatisPlus條件構(gòu)造器,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的2023-01-01
Java Volatile關(guān)鍵字實(shí)現(xiàn)原理過程解析
這篇文章主要介紹了Java Volatile關(guān)鍵字實(shí)現(xiàn)原理過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

