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

Spring Boot 项目多数据源配置【dynamic datasource】

前言:

随着互联网的发展,数据库的读写分离、数据迁移、多系统数据访问等多数据源的需求越来越多,我们在日常项目开发中,也不可避免的为了解决这个问题,本篇来分享一下在 Spring Boot 项目中使用多数据源访问不通的数据库。

业务场景:

当前开发的一个系统,因为数据量以及报表层面的需求要求,需要把 MySQL 的数据同步到大数据数据库 StarRocks 中,这里就涉及到了两个数据源:MySQL 和 StarRocks,想要完成这个功能的前置就是需要项目支持多数据源配置。

方案选型:

项目中使用了 MyBatis-Plus 插件,因此不在选择自己去实现多数据源的配置,直接使用 baomidou 的 dynamic-datasource-spring-boot-starter 来实现多数据源切换的功能。

引入依赖:

使用 baomidou 的 dynamic-datasource-spring-boot-starter 需要引入如下依赖:

<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version>
</dependency>

配置多数据源:

我这里配置了 MySQL 和 StarRocks 两个数据源,StarRocks 的驱动也是使用 MySQL 的驱动包,多数据源可以配置很多个,根据自己的需求来进行配置。

#mysql
spring.datasource.dynamic.primary = master
spring.datasource.dynamic.strict = false
spring.datasource.dynamic.datasource.master.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.master.url = jdbc:mysql://xx.xxx.xx.xxx:3306/xxxxx?useUnicode=true&characterEncoding=utf8&useSSL=true&rewriteBatchedStatements=true&allowMultiQueries=true
spring.datasource.dynamic.datasource.master.username = xxxxx
spring.datasource.dynamic.datasource.master.password = xxxxx
#starrocks
spring.datasource.dynamic.datasource.starrocks.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.starrocks.url = jdbc:mysql://xx.xxx.xx.xxx:3306/xxxxx?useUnicode=true&characterEncoding=utf8&useSSL=true&rewriteBatchedStatements=true&allowMultiQueries=true
spring.datasource.dynamic.datasource.starrocks.username = xxxxx
spring.datasource.dynamic.datasource.starrocks.password = xxxxx

多数据源测试

Service 层代码如下:

public interface DynamicDemoService {/** * @return java.lang.Long* @author author* @date 2025/3/24 14:27* @description 查询系统总数*/Long queryCount();}/*** @ClassName: DynamicDemoServiceImpl* @Author: Author* @Date: 2025/3/14 15:06* @Description:*/
@Service
@Slf4j
@DS("starrocks")
public class DynamicDemoServiceImpl implements DynamicDemoService {@Autowiredprivate DynamicDemoMapper dynDemoMapper;@Overridepublic Long queryCount() {return dynDemoMapper.queryCount();}}

上面代码我们可以看到和普通的 Service 代码区别不大,唯一需要注意的是,在 Service 类上加上 @DS 注解,并在指定数据源名称,(也可以在方法上加上 @DS 注解)。

Controller 层代码如下:

/*** @ClassName: DynamicDemoController* @Author: Author* @Date: 2025/3/14 15:12* @Description:*/
@RestController
@RequestMapping("/dynamic")
@Api(tags = {"【测试 Controller】"})
@Slf4j
public class DynamicDemoController {@Autowiredprivate DynamicDemoService dynamicDemoService;@RequestMapping("/test")public RetVo<Long> test() {Long count =  dynamicDemoService.queryCount();return RetVo.success(count);}}

测试结果如下:

在这里插入图片描述

结果符合预期。

多数据源的使用场景:

  • 读写分离:在高并发的系统中,数据库的读操作和写操作的频率和性能要求往往会不同,使用读写分离可以极大的提升系统的性能。
  • 数据迁移和系统升级:在系统升级或者数据迁移的过程中,往往涉及到新库和旧库同时使用,这时候就需要使用到多数据源。
  • 分库分表:业务量上升到一定的量后,单库单表的读写性能会成为业务的瓶颈,这个时候我们就会想到使用分库分表的方案,此时就可以使用多数据源来实现多库多表的访问了。
  • 不同数据系统的访问:在同一个系统里,可能会涉及到 MySQL、Doris、StarRocks 等数据库,系统想要在不同的数据库之间切换访问,这时候就需要使用到多数据源了。

总结:本篇简单分享了如何在 Spring Boot 项目中使用多数据源的配置,并附上了简单的代码演示,希望帮助到正需要配置多数据源的你。

如有不正确的地方欢迎各位指出纠正。

http://www.lryc.cn/news/2384567.html

相关文章:

  • JAVA查漏补缺(2)
  • 【Web前端】JavaScript入门与基础(二)
  • 取消 Conda 默认进入 Base 环境
  • Electron+vite+vue3 从0到1搭建项目,开发Win、Mac客户端
  • 《深度揭秘:解锁智能体大模型自我知识盲区探测》
  • 打卡Day33
  • 计算机组成原理-基本运算部件定点数的运算
  • python打卡day34@浙大疏锦行
  • SOC-ESP32S3部分:8-GPIO输出LED控制
  • 05算法学习_59. 螺旋矩阵 II
  • 绘制音频信号的各种频谱图,包括Mel频谱图、STFT频谱图等。它不仅能够绘制频谱图librosa.display.specshow
  • Linux `>`/`>>` 重定向操作符深度解析与高阶应用指南
  • 【自定义类型-联合和枚举】--联合体类型,联合体大小的计算,枚举类型,枚举类型的使用
  • 李宏毅《深度学习》:Self-attention 自注意力机制
  • C++初阶-list的使用1
  • Linux中的tty与login之间的关系
  • Python web 开发 Flask HTTP 服务
  • 分享|16个含源码和数据集的计算机视觉实战项目
  • 二十三、面向对象底层逻辑-BeanDefinitionParser接口设计哲学
  • [Vue]路由基础使用和路径传参
  • 使用VGG-16模型来对海贼王中的角色进行图像分类
  • OSI 网络七层模型中的物理层、数据链路层、网络层
  • WooCommerce缓存教程 – 如何防止缓存破坏你的WooCommerce网站?
  • AtCoder Beginner Contest 406(ABCD)
  • 第J2周:ResNet50V2 算法实战与解析
  • Live Search API :给大模型装了一个“实时搜索引擎”的插件
  • 每天分钟级别时间维度在数据仓库的作用与实现——以Doris和Hive为例(开箱即用)
  • 虚拟机Centos7:Cannot find a valid baseurl for repo: base/7/x86_64问题解决
  • IP风险度自检,多维度守护网络安全
  • NV066NV074美光固态颗粒NV084NV085