当前位置: 首页 > 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;将全球顶尖的美容美发资…...

简易版抽奖活动的设计技术方案

1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)

文章目录 1.什么是Redis&#xff1f;2.为什么要使用redis作为mysql的缓存&#xff1f;3.什么是缓存雪崩、缓存穿透、缓存击穿&#xff1f;3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

【AI学习】三、AI算法中的向量

在人工智能&#xff08;AI&#xff09;算法中&#xff0c;向量&#xff08;Vector&#xff09;是一种将现实世界中的数据&#xff08;如图像、文本、音频等&#xff09;转化为计算机可处理的数值型特征表示的工具。它是连接人类认知&#xff08;如语义、视觉特征&#xff09;与…...

反射获取方法和属性

Java反射获取方法 在Java中&#xff0c;反射&#xff08;Reflection&#xff09;是一种强大的机制&#xff0c;允许程序在运行时访问和操作类的内部属性和方法。通过反射&#xff0c;可以动态地创建对象、调用方法、改变属性值&#xff0c;这在很多Java框架中如Spring和Hiberna…...

关键领域软件测试的突围之路:如何破解安全与效率的平衡难题

在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件&#xff0c;这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下&#xff0c;实现高效测试与快速迭代&#xff1f;这一命题正考验着…...

mac 安装homebrew (nvm 及git)

mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用&#xff1a; 方法一&#xff1a;使用 Homebrew 安装 Git&#xff08;推荐&#xff09; 步骤如下&#xff1a;打开终端&#xff08;Terminal.app&#xff09; 1.安装 Homebrew…...

MySQL 索引底层结构揭秘:B-Tree 与 B+Tree 的区别与应用

文章目录 一、背景知识&#xff1a;什么是 B-Tree 和 BTree&#xff1f; B-Tree&#xff08;平衡多路查找树&#xff09; BTree&#xff08;B-Tree 的变种&#xff09; 二、结构对比&#xff1a;一张图看懂 三、为什么 MySQL InnoDB 选择 BTree&#xff1f; 1. 范围查询更快 2…...

AI语音助手的Python实现

引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...

QT开发技术【ffmpeg + QAudioOutput】音乐播放器

一、 介绍 使用ffmpeg 4.2.2 在数字化浪潮席卷全球的当下&#xff0c;音视频内容犹如璀璨繁星&#xff0c;点亮了人们的生活与工作。从短视频平台上令人捧腹的搞笑视频&#xff0c;到在线课堂中知识渊博的专家授课&#xff0c;再到影视平台上扣人心弦的高清大片&#xff0c;音…...