配置
spring.datasource.druid.master.name=master spring.datasource.druid.master.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.master.url=jdbc:mysql://localhost/redisapi?useSSL=false spring.datasource.druid.master.port=3306 spring.datasource.druid.master.username=root spring.datasource.druid.master.password=ihaveapen*^@#
spring.datasource.druid.slave-alpha.name=SlaveAlpha spring.datasource.druid.slave-alpha.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.slave-alpha.url=jdbc:mysql://localhost/redisapi2?useSSL=false spring.datasource.druid.slave-alpha.port=3306 spring.datasource.druid.slave-alpha.username=root spring.datasource.druid.slave-alpha.password=ihaveapen*^@#
spring.datasource.druid.slave-beta.name=SlaveBeta spring.datasource.druid.slave-beta.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.slave-beta.url=jdbc:mysql://localhost/redisapi3?useSSL=false spring.datasource.druid.slave-beta.port=3306 spring.datasource.druid.slave-beta.username=root spring.datasource.druid.slave-beta.password=ihaveapen*^@#
spring.datasource.druid.slave-gamma.name=SlaveGamma spring.datasource.druid.slave-gamma.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.slave-gamma.url=jdbc:mysql://localhost/redisapi4?useSSL=false spring.datasource.druid.slave-gamma.port=3306 spring.datasource.druid.slave-gamma.username=root spring.datasource.druid.slave-gamma.password=ihaveapen*^@#
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-wait=60000 spring.datasource.druid.pool-prepared-statements=false spring.datasource.druid.validation-query=SELECT 1 spring.datasource.druid.validation-query-timeout=30000 spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.test-while-idle=true #spring.datasource.druid.time-between-eviction-runs-millis= #spring.datasource.druid.min-evictable-idle-time-millis= #spring.datasource.druid.max-evictable-idle-time-millis=10000
spring.datasource.druid.filters=stat,wall,log4j,config,slf4j spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=.js,.gif,.jpg,.png,.css,.ico,/druid/* spring.datasource.druid.web-stat-filter.session-stat-enable=true spring.datasource.druid.web-stat-filter.session-stat-max-count=10 spring.datasource.druid.web-stat-filter.principal-session-name=user #spring.datasource.druid.web-stat-filter.principal-cookie-name= spring.datasource.druid.web-stat-filter.profile-enable=true spring.datasource.druid.use-global-data-source-stat=true spring.datasource.druid.filter.stat.db-type=mysql spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=1000 spring.datasource.druid.filter.stat.merge-sql=true spring.datasource.druid.filter.wall.enabled=true spring.datasource.druid.filter.wall.config.delete-allow=true spring.datasource.druid.filter.wall.config.drop-table-allow=false
spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.reset-enable=true spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=admin #spring.datasource.druid.stat-view-servlet.allow= #spring.datasource.druid.stat-view-servlet.deny= spring.datasource.druid.filter.slf4j.enabled=true
spring.datasource.druid.aop-patterns=cn.com.hellowood.dynamicdatasource.service.* spring.aop.proxy-target-class=true
数据源配置:
@Configuration public class DataSourceConfigurer {
@Bean("master")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.druid.master")
public DataSource master() {
return DataSourceBuilder.create().build();
}
@Bean("slaveAlpha")
@ConfigurationProperties(prefix = "spring.datasource.druid.slave-alpha")
public DataSource slaveAlpha() {
return DataSourceBuilder.create().build();
}
@Bean("slaveBeta")
@ConfigurationProperties(prefix = "spring.datasource.druid.slave-beta")
public DataSource slaveBeta() {
return DataSourceBuilder.create().build();
}
@Bean("slaveGamma")
@ConfigurationProperties(prefix = "spring.datasource.druid.slave-gamma")
public DataSource slaveGamma() {
return DataSourceBuilder.create().build();
}
@Bean("dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>(4);
dataSourceMap.put(DataSourceKey.master.name(), master());
dataSourceMap.put(DataSourceKey.slaveAlpha.name(), slaveAlpha());
dataSourceMap.put(DataSourceKey.slaveBeta.name(), slaveBeta());
dataSourceMap.put(DataSourceKey.slaveGamma.name(), slaveGamma());
dynamicRoutingDataSource.setDefaultTargetDataSource(master());
dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
DynamicDataSourceContextHolder.dataSourceKeys.addAll(dataSourceMap.keySet());
DynamicDataSourceContextHolder.slaveDataSourceKeys.addAll(dataSourceMap.keySet());
DynamicDataSourceContextHolder.slaveDataSourceKeys.remove(DataSourceKey.master.name());
return dynamicRoutingDataSource;
}
@Bean
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactoryBean() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dynamicDataSource());
return sqlSessionFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
}
原提问者GitHub用户helloworlde
以下是可能的解决方法:
检查Druid的配置:请检查Druid的配置是否正确。特别是在多数据源的场景下,需要确保每个数据源都正确配置了监控统计功能。您可以在Druid的配置文件中配置多个数据源,并为每个数据源配置独立的监控统计配置。
检查Spring Boot的配置:请检查Spring Boot的配置是否正确。特别是在多数据源的场景下,需要确保每个数据源都正确配置了Druid的数据源。您可以通过配置多个数据源来实现多数据源的支持,并为每个数据源配置独立的Druid数据源。
检查日志输出:您可以尝试在应用程序中输出SQL语句的日志,以便跟踪SQL语句的执行情况。您可以在Druid的配置文件中配置日志输出功能,并为每个数据源配置独立的日志输出配置。
升级Druid版本:Druid是一个开源的Java数据库连接池和SQL解析库,最新的版本可能已经解决了您的问题。您可以尝试升级Druid的版本,并重新测试您的应用程序。
找到错误了
return DataSourceBuilder.create().build(); // 应该是 return DruidDataSourceBuilder.create().build();
原回答者GitHub用户helloworlde
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。