欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 使用 Spring Boot 和 dynamic-datasource 实现多数据源集成

使用 Spring Boot 和 dynamic-datasource 实现多数据源集成

2025/6/19 20:59:24 来源:https://blog.csdn.net/hyc123123123123/article/details/148638002  浏览:    关键词:使用 Spring Boot 和 dynamic-datasource 实现多数据源集成
参考地址:

基础必读(免费) · dynamic-datasource · 看云

背景


在现代的微服务架构中,多数据源的使用场景越来越普遍。例如,一个系统可能需要同时访问多个数据库,或者在不同环境下切换数据源。Spring Boot 提供了强大的支持来简化多数据源的配置和管理。本文将介绍如何使用 Spring Boot 和 dynamic-datasource 库来实现多数据源的集成。


1. 为什么需要多数据源


多数据源的使用场景包括但不限于以下几种:
读写分离:通过主从数据库实现读写分离,提高系统的性能和可用性。
分库分表:在处理海量数据时,将数据分散到多个数据库中,以提高查询效率。
多租户架构:为不同的租户提供独立的数据存储,同时在逻辑上隔离数据。
动态切换数据源:根据不同的业务逻辑动态切换数据源,例如在不同环境下切换到不同的数据库。

2. 引入依赖


首先,我们需要在 Spring Boot 项目中引入 dynamic-datasource 的依赖。dynamic-datasource 是一个流行的开源库,用于动态切换数据源。

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

3. 配置多数据源

application.yml 文件中配置多个数据源。以下是一个示例配置,包含三个数据源:masterslave_1 和 slave_2

spring:application:name: spring-boot-dynamic-datasourcetask:scheduling:pool:# 系统定时任务线程池,同时可以有N个定时任务可以同时执行。size: 2jackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT+8main:allow-bean-definition-overriding: truedatasource:dynamic:# 设置默认的数据源或者数据源组,默认值即为masterprimary: master# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源strict: falsedatasource:master:url: jdbc:mysql://127.0.0.1:3306/xxxx2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&useOldAliasMetadataBehavior=true&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数(建议设为maxActive的1/6)minIdle: 2# 初始连接数建议与minIdle一致(原值3过低)initialSize: 2# 等待超时时间(合理)maxWait: 30000slave_1:url: jdbc:mysql://127.0.0.1:3306/xxxx2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&useOldAliasMetadataBehavior=true&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数(建议设为maxActive的1/6)minIdle: 2# 初始连接数建议与minIdle一致(原值3过低)initialSize: 2# 等待超时时间(合理)maxWait: 30000slave_2:url: jdbc:mysql://127.0.0.1:3306/xxxx3?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&useOldAliasMetadataBehavior=true&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=trueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数(建议设为maxActive的1/6)minIdle: 2# 初始连接数建议与minIdle一致(原值3过低)initialSize: 2# 等待超时时间(合理)maxWait: 30000

    4. 使用 @DS 切换数据源。

    /*** @DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解。** 注解	结果* 没有@DS	默认数据源* @DS("dsName") dsName可以为组名也可以为具体某个库的名称* @Service* @DS("slave")*/
    public class UserServiceImpl implements UserService {@Autowiredprivate JdbcTemplate jdbcTemplate;public List selectAll() {return  jdbcTemplate.queryForList("select * from user");}@Override@DS("slave_1")public List selectByCondition() {return  jdbcTemplate.queryForList("select * from user where age >10");}
    }

    上面的不重要,直接从Gitee仓库拉下来具体项目.

    https://gitee.com/hanyunchuan/spring-boot-dynamic-datasource

    https://gitee.com/hanyunchuan/spring-boot-dynamic-datasource.git

    版权声明:

    本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

    我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

    热搜词