springboot配置多数据源
springboot配置多数据源
学习新技术,争做新青年,欢迎围观,河南老乡在上海请,加,微,andyfau2022,
----获取数据源:null,数据源为null时默认使用主数据源的。
1-yml文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql:/306/andy?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password:
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: true
url: jdbc:mysql/demo_project?useUnicode=true&characterEncodhavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password:
slave-aaa:
# 从数据源开关/默认关闭
enabled: true
# driverClassName: org.postgresql.Driver
url: jdbc:mysql:/_databaseuseUnicode=true&characterEncoding=utf8&avior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置连接超时时间
connectTimeout: 30000
# 配置网络超时时间
socketTimeout: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username: Andy_Fan
login-password: 123ss456
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: tr
2-DruidConfig 配置
/**
* druid 配置多数据源
*
* @author Andy_Fan
*
* 在需要切换数据源的方法上加上@DataSource注解----加在serviceImpl方法上,或者mapper
*
* @DataSource(value = DataSourceType.SLAVE)
* @Select("select count(1) from public.\"LX_CJ_DK\"")
*/
@Slf4j
@Configuration
public class DruidConfig
{
/**
* 主库数据源
* @param druidProperties
* @return
*/
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource( DruidProperties druidProperties )
{
log.info( "------主库数据源加载完成--masterDataSource-" );
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource( dataSource );
}
/**
* 从库数据源 1
* @param druidProperties
* @return
*/
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource( DruidProperties druidProperties )
{
log.info( "------从库数据源加载完成--slaveDataSource-" );
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource( dataSource );
}
/**
* 从库数据源 2 aaa
* @param
* @return
*/
@Bean
@ConfigurationProperties("spring.datasource.druid.slave-aaa")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave-aaa", name = "enabled", havingValue = "true")
public DataSource slaveAAADataSource( DruidProperties druidProperties )
{
log.info( "------从库数据源加载完成--slaveAAADataSource-" );
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource( dataSource );
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource( DataSource masterDataSource, DataSource slaveDataSource, DataSource slaveAAADataSource )
{
log.info( "------主从数据源加载完成---" );
Map< Object, Object > targetDataSources = new HashMap<>();
targetDataSources.put( DataSourceType.MASTER.name(), masterDataSource );
targetDataSources.put( DataSourceType.SLAVE.name(), slaveDataSource );
targetDataSources.put( DataSourceType.SLAVE_AAA.name(), slaveAAADataSource );
// 在需要切换数据源的方法上加上@DataSource注解,@DataSource(value = DataSourceType.SLAVE)
return new DynamicDataSource( masterDataSource, targetDataSources );
}
/**
* 设置数据源
*
* @param targetDataSources 备选数据源集合
* @param sourceName 数据源名称
* @param beanName bean名称
*/
public void setDataSource( Map< Object, Object > targetDataSources, String sourceName, String beanName )
{
try
{
log.info( "------加载数据源: targetDataSources{}", targetDataSources + "------: sourceName {}", sourceName + "------: beanName{}", beanName );
DataSource dataSource = SpringUtils.getBean( beanName );
targetDataSources.put( sourceName, dataSource );
}
catch ( Exception e )
{
log.error( "------加载数据源失败, {}", e.getMessage() );
}
}
}
3- DynamicDataSource
```
/**
* 动态数据源
*
* @author Andy_Fan
* @date 2024/09/11
*/
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource
{
public DynamicDataSource( DataSource defaultTargetDataSource, Map< Object, Object > targetDataSources )
{
log.info( "-----------DynamicDataSource init" );
super.setDefaultTargetDataSource( defaultTargetDataSource );
super.setTargetDataSources( targetDataSources );
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey()
{
log.info( "------DynamicDataSource determineCurrentLookupKey" );
return DynamicDataSourceContextHolder.getDataSourceType();
}
}
4-DynamicDataSourceContext
/**
* 数据源切换处理
* @author Andy_Fan
* @date 2024/09/11
*/
public class DynamicDataSourceContextHolder
{
public static final Logger log = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
/**
* 使用ThreadLocal维护变量,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
*/
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
/**
* 设置数据源的变量
*
* 增加DataSourceType类里面的枚举类型
public enum DataSourceType{MASTER,SLAVE,SLAVE_XZDB}
*
*/
public static void setDataSourceType(String dsType)
{
log.info("----切换数据源:", dsType);
CONTEXT_HOLDER.set(dsType);
}
/**
* 获得数据源的变量
*/
public static String getDataSourceType()
{
log.info("-----获取数据源:{}", CONTEXT_HOLDER.get());
return CONTEXT_HOLDER.get();
}
/**
* 清空数据源变量
*/
public static void clearDataSourceType()
{
log.info("-----清除数据源");
CONTEXT_HOLDER.remove();
}
}
5-测试
/**
* <p>
* 用户表 服务实现类
* </p>
*
* @author Andy_Fan
* @since 2024-09-12 14:52:06
*/
@Service
public class DemoUserServiceImpl extends ServiceImpl< DemoUserMapper, DemoUser > implements IDemoUserService
{
@Resource
private DemoUserMapper demoUserMapper;
@Override
@DataSource(value = DataSourceType.SLAVE)
public List< DemoUser > selectList()
{
return demoUserMapper.selectAll( new DemoUser() );
}
}
----切换到SLAVE数据源
-----获取数据源:SLAVE
----获取数据源:null
.r.f.d.DynamicDataSourceContextHolder - [setDataSourceType,28] - ----切换到SLAVE数据源
15:52:06.280 [http-nio-8080-exec-1] INFO c.r.f.d.DynamicDataSourceContextHolder - [getDataSourceType,38] - -----获取数据源:SLAVE
15:52:06.283 [http-nio-8080-exec-1] DEBUG c.r.d.m.D.selectAll - [debug,135] - ==> Preparing: select id, username, password, nickName, third_party_id, avatar, avatar_thumbnail, gender, user_type, last_login_time, registration_ip, deleted, version, create_date, create_by, update_date, update_by from demo_user
15:52:06.289 [http-nio-8080-exec-1] DEBUG c.r.d.m.D.selectAll - [debug,135] - ==> Parameters:
15:52:06.429 [http-nio-8080-exec-1] DEBUG c.r.d.m.D.selectAll - [debug,135] - <== Total: 2
15:52:06.430 [http-nio-8080-exec-1] INFO c.r.f.d.DynamicDataSourceContextHolder - [clearDataSourceType,47] - -----清除数据源
一起加油,一起向前冲。。。