Spring Boot集成protobuf快速入门Demo

news/2024/9/23 1:43:47/

1.什么是protobuf

Protobuf(Protocol Buffers)是由 Google 开发的一种轻量级、高效的数据交换格式,它被用于结构化数据的序列化、反序列化和传输。相比于 XML 和 JSON 等文本格式,Protobuf 具有更小的数据体积、更快的解析速度和更强的可扩展性。 Protobuf 的核心思想是使用协议(Protocol)来定义数据的结构和编码方式。使用 Protobuf,可以先定义数据的结构和各字段的类型、字段等信息,然后使用 Protobuf 提供的编译器生成对应的代码用于序列化和反序列化数据。由于 Protobuf 是基于二进制编码的,因此可以在数据传输和存储中实现更高效的数据交换,同时也可以跨语言使用。

google-protocol-buffers-xenonstack

Protobuf 有以下几个优势

  • 更小的数据量:Protobuf 的二进制编码通常只有 XML 和 JSON 的 1/3 到 1/10 左右,因此在网络传输和存储数据时可以节省带宽和存储空间。
  • 更快的序列化和反序列化速度:由于 Protobuf 使用二进制格式,所以序列化和反序列化速度比 XML 和 JSON 快得多。
  • 跨语言:Protobuf 支持多种编程语言,可以使用不同的编程语言来编写客户端和服务端。这种跨语言的特性使得 Protobuf 受到很多开发者的欢迎(JSON 也是如此)。
  • 易于维护可扩展:Protobuf 使用 .proto 文件定义数据模型和数据格式,这种文件比 XML 和 JSON 更容易阅读和维护,且可以在不破坏原有协议的基础上,轻松添加或删除字段,实现版本升级和兼容性。

2.代码工程

 实验目标

rest api实现基于Protobuf 协议通信

pom.xml

<?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"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.1</version></parent><modelVersion>4.0.0</modelVersion><artifactId>protobuf</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><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.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>3.19.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency></dependencies>
<build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.6.1</version></extension></extensions><plugins><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protoSourceRoot>${basedir}/src/main/resources</protoSourceRoot><protocArtifact>com.google.protobuf:protoc:3.19.1:exe:${os.detected.classifier}</protocArtifact><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.43.1:exe:${os.detected.classifier}</pluginArtifact><outputDirectory>src/main/java</outputDirectory><clearOutputDirectory>false</clearOutputDirectory><pluginId>grpc-java</pluginId></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build></project>

controller

package com.et.protobuf.controller;import com.et.protobuf.PhoneNumJson;
import com.et.protobuf.ProtobufMessage;
import com.et.protobuf.StudentJson;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@RestController
public class HelloWorldController {@RequestMapping("/json/{id}")public StudentJson showHelloWorld(@PathVariable Integer id){StudentJson  studentJson = new StudentJson();studentJson.setId(id);studentJson.setFirstName("maxsm");studentJson.setLastName("sdfsdfsdfsdfsdfsdsdfsdfsdfsdfsdfsdfsdfsdf");studentJson.setEmail("1224sdfsfsdf344552@163.com");PhoneNumJson phoneNumJson =  new PhoneNumJson();phoneNumJson.setNumber("12345sdfsdfsd6566666");phoneNumJson.setType(1);List<PhoneNumJson> list = new ArrayList<>();list.add(phoneNumJson);studentJson.setPhoneNumList(list);return studentJson;}@RequestMapping("/protobuf/{id}")ProtobufMessage.Student protobuf(@PathVariable Integer id) {return ProtobufMessage.Student.newBuilder().setId(id).setFirstName("maxsm").setLastName("sdfsdfsdfsdfsdfsdsdfsdfsdfsdfsdfsdfsdfsdf").setEmail("1224sdfsfsdf344552@163.com").addPhone(ProtobufMessage.Student.PhoneNumber.newBuilder().setNumber("12345sdfsdfsd6566666").setType(ProtobufMessage.Student.PhoneType.MOBILE).build()).build();}}

entity

package com.et.protobuf;import java.util.List;/*** @author liuhaihua* @version 1.0* @ClassName StudentJson* @Description todo* @date 2024/08/05/ 16:32*/
public class StudentJson {private int id;private String firstName;private String lastName;private String email;private List<PhoneNumJson> phoneNumList;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public List<PhoneNumJson> getPhoneNumList() {return phoneNumList;}public void setPhoneNumList(List<PhoneNumJson> phoneNumList) {this.phoneNumList = phoneNumList;}
}
package com.et.protobuf;/*** @author liuhaihua* @version 1.0* @ClassName PhoneNum* @Description todo* @date 2024/08/05/ 16:35*/public class PhoneNumJson {private int type;private String number;public int getType() {return type;}public void setType(int type) {this.type = type;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}
}

mxsm.proto

使用 Protobuf 的语言定义文件(.proto)可以定义要传输的信息的数据结构,可以包括各个字段的名称、类型等信息。同时也可以相互嵌套组合,构造出更加复杂的消息结构。

syntax = "proto3";
package mxsm;
option java_package = "com.et.protobuf";
option java_outer_classname = "ProtobufMessage";message Course {int32 id = 1;string course_name = 2;repeated Student student = 3;
}
message Student {int32 id = 1;string first_name = 2;string last_name = 3;string email = 4;repeated PhoneNumber phone = 5;message PhoneNumber {string number = 1;PhoneType type = 2;}enum PhoneType {MOBILE = 0;LANDLINE = 1;}
}
头部全局定义
  • syntax = "proto3"; 指定 Protobuf 版本为版本 3(最新版本)
  • package com.wdbyte.protobuf; 指定 Protobuf 包名,防止有相同类名的 message 定义,这个包名是生成的类中所用到的一些信息的前缀,并非类所在包。
  • option java_multiple_files = true; 是否生成多个文件。若 false,则只会生成一个类,其他类以内部类形式提供。
  • option java_package = 生成的类所在包。
  • option java_outer_classname 生成的类名,若无,自动使用文件名进行驼峰转换来为类命名。
消息结构具体定义

message Person 定一个了一个 Person 类。 Person 类中的字段被 optional 修饰,被 optional 修饰说明字段可以不赋值。

  • 修饰符 optional 表示可选字段,可以不赋值。
  • 修饰符 repeated 表示数据重复多个,如数组,如 List。
  • 修饰符 required 表示必要字段,必须给值,否则会报错 RuntimeException,但是在 Protobuf 版本 3 中被移除。即使在版本 2 中也应该慎用,因为一旦定义,很难更改。
字段类型定义

修饰符后面紧跟的是字段类型,如 int32 、string。常用的类型如下:

  • int32、int64、uint32、uint64:整数类型,包括有符号和无符号类型。
  • float、double:浮点数类型。
  • bool:布尔类型,只有两个值,true 和 false。
  • string:字符串类型。
  • bytes:二进制数据类型。
  • enum:枚举类型,枚举值可以是整数或字符串。
  • message:消息类型,可以嵌套其他消息类型,类似于结构体。

字段后面的 =1,=2 是作为序列化后的二进制编码中的字段的对应标签,因为 Protobuf 消息在序列化后是不包含字段信息的,只有对应的字段序号,所以节省了空间。也因此,1-15 比 16 会少一个字节,所以尽量使用 1-15 来指定常用字段。且一旦定义,不要随意更改,否则可能会对不上序列化信息

config

package com.et.protobuf.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.Arrays;@Configuration
public class Config {@BeanRestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {return new RestTemplate(Arrays.asList(hmc));}@BeanProtobufHttpMessageConverter protobufHttpMessageConverter() {return new ProtobufHttpMessageConverter();}
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.(Protobuf

3.测试

启动Spring Boot应用

编写测试类

package com.et.protobuf;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.client.RestTemplate;import java.util.ArrayList;
import java.util.List;@SpringBootTest(classes = DemoApplication.class)
public class ApplicationTest {// Other declarationsprivate static final String COURSE1_URL = "http://localhost:8088/protobuf/1";@Autowiredprivate RestTemplate restTemplate ;@Testpublic void whenUsingRestTemplate_thenSucceed() {ResponseEntity<ProtobufMessage.Student> student = restTemplate.getForEntity(COURSE1_URL, ProtobufMessage.Student.class);System.out.println(student.toString());}
}

结果如下

<200 OK OK,id: 1
first_name: "maxsm"
last_name: "sdfsdfsdfsdfsdfsdsdfsdfsdfsdfsdfsdfsdfsdf"
email: "1224sdfsfsdf344552@163.com"
phone {number: "12345sdfsdfsd6566666"
}
,[X-Protobuf-Schema:"mxsm.proto", X-Protobuf-Message:"mxsm.Student", Content-Type:"application/x-protobuf;charset=UTF-8", Transfer-Encoding:"chunked", Date:"Mon, 05 Aug 2024 09:10:55 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

Json和protobuf性能比较

单个线程,循环1分钟

json

Protobuf

测试结果发现json性能甚至优于protobuf,并不符合预期结果!是我哪一步整错了吗?有大神知道怎么回事吗?

4.引用

  • Overview | Protocol Buffers Documentation

http://www.ppmy.cn/news/1508866.html

相关文章

Spring MVC 扩展和SSM框架整合

Spring MVC框架处理JSON数据 JSON格式数据在现阶段的web项目开发中扮演着非常重要的角色。在前端页面和后台交互的过程中&#xff0c;需要一种格式清晰、高效且两端都可以轻松使用的数据格式做交互的媒介&#xff0c;JSON正可以满足这一需求 JSON&#xff08;JavaScript Object…

【随笔】Java 连接操作FTP与SFTP 详细指南

引言 在Java开发中&#xff0c;文件传输协议&#xff08;FTP&#xff09;和安全文件传输协议&#xff08;SFTP&#xff09;是处理文件传输的两种常见方式。FTP是标准的网络文件传输协议&#xff0c;而SFTP则在FTP基础上增加了安全层&#xff08;SSH&#xff09;&#xff0c;提…

4章8节:用R做数据重塑,行列命名和数据类型转换

在R语言中,行列命名和数据类型转换是数据处理中的两个基础性操作。它们不仅对数据的可读性和组织性至关重要,而且在执行数据分析、模型构建和结果解释时也扮演着重要的角色。 一、行和列命名 在数据科学和统计分析中,命名是组织和管理数据的一个重要部分。尤其是在处理复杂…

LVS的12种调度算法详解

1.lvs调度算法类型 1.1静态方法 仅根据算法本身进行调度&#xff0c;不考虑RS的负载情况 1.2动态方法 主要根据每RS当前的负载状态及调度算法进行调度Overheadvalue较小的RS将被调度 1.1lvs静态调度算法 1.1.1RR&#xff08;轮询算法&#xff09;&#xff1a; roundrobin 轮…

媒体优质翻译对中国产品在美国推广的影响

随着中国公司越来越多地瞄准美国市场&#xff0c;媒体内容的高质量翻译的重要性怎么强调都不为过。有效地翻译不仅仅是将单词从一种语言转换为另一种语言&#xff1b;这是关于传达正确的信息、语气和文化细微差别。高质量的翻译通过提高品牌知名度、增加客户参与度和推动销售&a…

cAdvisor+prometheus+grafana搭建监控页面并嵌入自定义页面中

三者关系 一般公司会有很多docker主机&#xff0c;那么就需要对docker进行监控了&#xff0c;docker监控可以采用docker stats配合shell命令来取值做监控&#xff0c;但是无法传递给prometheus进行采集&#xff0c;zabbix监控docker又比较麻烦&#xff0c;因此就有了谷歌的cad…

鸿蒙图形开发【AR引擎服务】 XR

AREngine 介绍 本示例展示了AREngine提供的平面检测&#xff0c;运动跟踪&#xff0c;环境跟踪和碰撞检测能力。 效果预览 在手机的主屏幕&#xff0c;点击“ArSample”&#xff0c;启动应用&#xff0c;在主界面可见“ArWorld”按钮。点击“ArWorld”按钮&#xff0c;拉起A…

软件项目质量管理体系,软件评审,资质认证,安全建设及项目管理匹配资料(原件参考)

软件项目质量管理体系是指一套系统化的管理方法、流程、工具和文档&#xff0c;旨在确保软件项目从需求分析、设计、开发、测试到部署和维护的整个生命周期中&#xff0c;都能达到预定的质量标准和客户期望。该体系通过明确的角色和责任、标准化的工作流程、有效的质量控制和持…