当前位置: 首页 > news >正文

Spring Boot集成protobuf快速入门Demo

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

相关文章:

Spring Boot集成protobuf快速入门Demo

1.什么是protobuf&#xff1f; Protobuf&#xff08;Protocol Buffers&#xff09;是由 Google 开发的一种轻量级、高效的数据交换格式&#xff0c;它被用于结构化数据的序列化、反序列化和传输。相比于 XML 和 JSON 等文本格式&#xff0c;Protobuf 具有更小的数据体积、更快…...

SpringBoot+Vue 简单小文章项目开发全过程

文章目录 一、项目介绍二、需求设计三、数据库设计四、项目构建项目技术选型:构建项目说明:项目架构mavenMySQLRedis 五、项目开发&#xff1a;项目开发思路&#xff1a;项目开发过程&#xff1a;1. 导入文件包/新建项目2. 新建子模块&#xff1a;common模块pojo模块server模块…...

如何将发明原理应用于产品设计的概念阶段?

众所周知&#xff0c;产品设计的概念阶段是创意孵化的关键时期&#xff0c;它决定了产品的方向、定位及核心卖点。在这一阶段&#xff0c;将发明原理融入其中&#xff0c;能够极大地拓宽思维边界&#xff0c;激发前所未有的设计灵感。具体步骤如深圳天行健企业管理咨询公司下文…...

【wsl】wsl + vscode 中使用 typora 打开 markdown 文件

vscode 连接好wsl 使用Open in External App 一个五星好评的插件Open in External App则可以在vscode中用typora打开md文件&#xff0c;不仅如此&#xff0c;还有设定其他应用打开相应的文件&#xff0c;比如chrome打开html。插件食用方法也比较简单&#xff0c;安装后&#…...

AutoDL下huggingface下载模型位置问题

AutoDL系统盘只有30G&#xff0c;数据盘有50G且可扩容&#xff0c;模型及数据集空间通常较大&#xff0c;为节省系统盘空间&#xff0c;我们将文件都存储于数据盘&#xff0c;在运行的代码最前端&#xff08;一定要在最前面&#xff09;添加 import os os.environ[HF_HOME] /…...

SpringBoot基础(一):快速入门

SpringBoot基础系列文章 SpringBoot基础(一)&#xff1a;快速入门 目录 一、SpringBoot简介二、快速入门三、SpringBoot核心组件1、parent1.1、spring-boot-starter-parent1.2、spring-boot-dependencies 2、starter2.1、spring-boot-starter-web2.2、spring-boot-starter2.3、…...

使用Weka进行数据挖掘与机器学习

在当前大数据时代&#xff0c;数据挖掘与机器学习已经成为了不可或缺的技术。而Weka是一个非常流行的机器学习软件&#xff0c;它提供了一整套的机器学习算法和数据处理工具。Weka不仅支持命令行操作和GUI&#xff0c;还提供了Java API&#xff0c;非常适合Java开发者进行数据挖…...

定时器知识点

#视频教程&#xff1a; 11.TIM定时中断 CSDN教程 知识点&#xff1a; 1.时钟源选择图 ![[Pasted Image 20240802103525_114.png]] 基本定时器 2个功能 &#xff1a;只能定时中断和主模式触发DAC的功能 知识点 1.时基单元&#xff1a;预分配器&#xff08;PSC&#xff09;、…...

桌面日历还能这样玩?这个日历太酷了吧!秒变桌面记事本!

大家应该有经常看日历的习惯&#xff0c;每个人都有不同的日历需求。特别是一些节假日&#xff0c;重要节日时候&#xff0c;大家看日历的频次就比较高了&#xff0c;如何选一款好用的日历&#xff1f;我们给大家展示一款非常不错的桌面日历&#xff0c;看下你喜不喜欢&#xf…...

基于深度学习的太阳暗条检测(2020年以来)

A universal method for solar filament detection from Hα observations using semi-supervised deep learning A&A, 686, A213 (2024) A universal method for solar filament detection from Hα observations using semi-supervised deep learning (aanda.org) ABS…...

【吊打面试官系列-Elasticsearch面试题】Elasticsearch 在部署时,对 Linux 的设置有哪些优化方法?

大家好&#xff0c;我是锋哥。今天分享关于 【Elasticsearch 在部署时&#xff0c;对 Linux 的设置有哪些优化方法?】面试题&#xff0c;希望对大家有帮助&#xff1b; Elasticsearch 在部署时&#xff0c;对 Linux 的设置有哪些优化方法? 面试官 &#xff1a;想了解对 ES 集…...

MySQL·C/C++访问数据库

目录 准备工作 测试是否安装成功 C/C语言访问 官方文档 接口介绍使用 mysql_init() mysql_close() 补充1&#xff1a;makefile编写 mysql_real_connect() 测试1&#xff1a;编译链接 mysql_query() 测试2&#xff1a;SQL语句测试 改 增 删 查 错误1&#x…...

python.tkinter设计标记语言(渲染2-渲染器)

TOC 前言 本文仅作为笔记记录。 在前文中&#xff0c;我们通过标记意义解释生成了带有明确渲染要求的参数组&#xff0c;以<title>为例&#xff0c;我们获取了title, level两个明确的渲染标记&#xff0c;这一部分由Tin标记解释器完成&#xff0c;不需要编写者花费过多…...

Cadence学习笔记 Day0 Cadence17.4环境安装

当然是选择“吴法安装” 直接跟着吴川斌博客的方法来就可以了&#xff0c;这里大致记录一下我的安装步骤&#xff1a; 安装许可证管理器破解许可证管理器安装软件以及补丁破解软件 获取 直接放出链接&#xff1a;吴川斌的博客 下载得到&#xff1a; 一、安装许可证管理器&am…...

k8s创建secret并在container中获取secret

k8s创建secret并在container中获取secret 本文使用的deployment和service与我的上一篇文章一样。link也放在下面了&#xff0c;如果不懂什么事deployment和service&#xff0c;可以先看我的上一篇文章。 k8s使用kustomize来部署应用 下面我们将通过创建secret开始。secret是我…...

Leetcode每日一题之仅仅反转字母(C++)

在学习之余对于知识的巩固也尤为重要&#xff0c;不论难度高低&#xff0c;都会对代码的理解有所加深&#xff0c;下面我们开始练习 思路解析 关于本题的核心思路就是如何判断字符串中元素是否为字母以及如何遍历字符串以达到仅反转的目的&#xff0c;这里用到的知识就是关于 s…...

PDF预览:利用vue3-pdf-app实现前端PDF在线展示

目录 PDF预览&#xff1a;利用vue3-pdf-app实现前端PDF在线展示 一、vue3-pdf-app组件介绍及其优点 1、vue3-pdf-app是什么 2、作用与场景 3、类似的插件 二、项目初始化与依赖安装 1、初始化Vue3项目 2、安装依赖 三、集成vue3-pdf-app插件 1、引入插件 2、配置组件…...

【OpenCV C++20 学习笔记】拉普拉斯(Laplace)二阶求导-边缘检测

拉普拉斯二阶求导 原理拉普拉斯算子(Laplacian Operator) API实例 原理 在OpenCV中&#xff0c;Sobel算法可以对图片中的值求一阶导数&#xff0c;从而计算出图片中的边缘线。其原理如下面的示意图&#xff1a; 那么&#xff0c;如果再求一次导数的&#xff0c;即求二阶导数&…...

MySQL的下载和安装步骤

一、数据库概述 我们先来了解三个概念&#xff1a;数据库、数据库管理系统、SQL。 名称全称简称数据库存储数据的仓库&#xff0c;数据是有组织的进行存储DataBase&#xff08;DB&#xff09;数据库管理系统操纵和管理数据库的大型软件DataBase Management System (DBMS)SQL操…...

Java国际版同城服务美容美发到店服务上门服务系统

&#x1f30d;全球美妆新风尚&#xff01;国际版同城服务&#xff0c;美容美发一键享 &#x1f3d9;️【国际视野&#xff0c;同城便捷】&#x1f3d9;️ 在这个全球化的时代&#xff0c;美丽不再受地域限制&#xff01;国际版同城服务系统&#xff0c;将全球顶尖的美容美发资…...

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…...

椭圆曲线密码学(ECC)

一、ECC算法概述 椭圆曲线密码学&#xff08;Elliptic Curve Cryptography&#xff09;是基于椭圆曲线数学理论的公钥密码系统&#xff0c;由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA&#xff0c;ECC在相同安全强度下密钥更短&#xff08;256位ECC ≈ 3072位RSA…...

三维GIS开发cesium智慧地铁教程(5)Cesium相机控制

一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点&#xff1a; 路径验证&#xff1a;确保相对路径.…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

汽车生产虚拟实训中的技能提升与生产优化​

在制造业蓬勃发展的大背景下&#xff0c;虚拟教学实训宛如一颗璀璨的新星&#xff0c;正发挥着不可或缺且日益凸显的关键作用&#xff0c;源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例&#xff0c;汽车生产线上各类…...

什么是库存周转?如何用进销存系统提高库存周转率?

你可能听说过这样一句话&#xff1a; “利润不是赚出来的&#xff0c;是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业&#xff0c;很多企业看着销售不错&#xff0c;账上却没钱、利润也不见了&#xff0c;一翻库存才发现&#xff1a; 一堆卖不动的旧货…...

《通信之道——从微积分到 5G》读书总结

第1章 绪 论 1.1 这是一本什么样的书 通信技术&#xff0c;说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号&#xff08;调制&#xff09; 把信息从信号中抽取出来&am…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南

精益数据分析&#xff08;97/126&#xff09;&#xff1a;邮件营销与用户参与度的关键指标优化指南 在数字化营销时代&#xff0c;邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天&#xff0c;我们将深入解析邮件打开率、网站可用性、页面参与时…...