网站建设丶金手指专业,哪些网站可以做微信,世界500强企业市值排名,建筑设计公司经营范围参考地址: 
基础必读#xff08;免费#xff09;  dynamic-datasource  看云 
背景 在现代的微服务架构中#xff0c;多数据源的使用场景越来越普遍。例如#xff0c;一个系统可能需要同时访问多个数据库#xff0c;或者在不同环境下切换数据源。Spring Boot 提供了强大的…参考地址: 
基础必读免费 · dynamic-datasource · 看云 
背景 在现代的微服务架构中多数据源的使用场景越来越普遍。例如一个系统可能需要同时访问多个数据库或者在不同环境下切换数据源。Spring Boot 提供了强大的支持来简化多数据源的配置和管理。本文将介绍如何使用 Spring Boot 和 dynamic-datasource 库来实现多数据源的集成。 1. 为什么需要多数据源 多数据源的使用场景包括但不限于以下几种 读写分离通过主从数据库实现读写分离提高系统的性能和可用性。 分库分表在处理海量数据时将数据分散到多个数据库中以提高查询效率。 多租户架构为不同的租户提供独立的数据存储同时在逻辑上隔离数据。 动态切换数据源根据不同的业务逻辑动态切换数据源例如在不同环境下切换到不同的数据库。 
2. 引入依赖 首先我们需要在 Spring Boot 项目中引入 dynamic-datasource 的依赖。dynamic-datasource 是一个流行的开源库用于动态切换数据源。 dependencygroupIdcom.baomidou/groupIdartifactIddynamic-datasource-spring-boot-starter/artifactIdversion4.3.1/version/dependency 
3. 配置多数据源 
在 application.yml 文件中配置多个数据源。以下是一个示例配置包含三个数据源master和 slave_1 和 slave_2。 
spring:application:name: spring-boot-dynamic-datasourcetask:scheduling:pool:# 系统定时任务线程池,同时可以有N个定时任务可以同时执行。size: 2jackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT8main:allow-bean-definition-overriding: truedatasource:dynamic:# 设置默认的数据源或者数据源组,默认值即为masterprimary: master# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源strict: falsedatasource:master:url: jdbc:mysql://127.0.0.1:3306/xxxx2?useUnicodetruecharacterEncodingutf8autoReconnecttruefailOverReadOnlyfalseserverTimezoneAsia/ShanghaiuseOldAliasMetadataBehaviortrueuseSSLfalseallowMultiQueriestruerewriteBatchedStatementstrueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数建议设为maxActive的1/6minIdle: 2# 初始连接数建议与minIdle一致原值3过低initialSize: 2# 等待超时时间合理maxWait: 30000slave_1:url: jdbc:mysql://127.0.0.1:3306/xxxx2?useUnicodetruecharacterEncodingutf8autoReconnecttruefailOverReadOnlyfalseserverTimezoneAsia/ShanghaiuseOldAliasMetadataBehaviortrueuseSSLfalseallowMultiQueriestruerewriteBatchedStatementstrueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数建议设为maxActive的1/6minIdle: 2# 初始连接数建议与minIdle一致原值3过低initialSize: 2# 等待超时时间合理maxWait: 30000slave_2:url: jdbc:mysql://127.0.0.1:3306/xxxx3?useUnicodetruecharacterEncodingutf8autoReconnecttruefailOverReadOnlyfalseserverTimezoneAsia/ShanghaiuseOldAliasMetadataBehaviortrueuseSSLfalseallowMultiQueriestruerewriteBatchedStatementstrueusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverdruid:# 最大空闲连接数必须 ≥ minIdlemaxActive: 10# 最小空闲连接数建议设为maxActive的1/6minIdle: 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);}OverrideDS(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