开发者社区> 问答> 正文

SpringBoot 1.5.9 和 Druid 1.1.6 多数据源下无法统计SQL,数据源和se

配置

Master datasource config

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*^@#

SlaveAlpha datasource config

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*^@#

SlaveBeta datasource config

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*^@#

SlaveGamma datasource config

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*^@#

Druid dataSource config

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

Druid stat filter config

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

Druid manage page config

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

Druid AOP config

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());
}

}

T53.png

T54.png

T55.png

原提问者GitHub用户helloworlde

展开
收起
山海行 2023-07-05 20:33:01 59 0
2 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    以下是可能的解决方法:

    检查Druid的配置:请检查Druid的配置是否正确。特别是在多数据源的场景下,需要确保每个数据源都正确配置了监控统计功能。您可以在Druid的配置文件中配置多个数据源,并为每个数据源配置独立的监控统计配置。

    检查Spring Boot的配置:请检查Spring Boot的配置是否正确。特别是在多数据源的场景下,需要确保每个数据源都正确配置了Druid的数据源。您可以通过配置多个数据源来实现多数据源的支持,并为每个数据源配置独立的Druid数据源。

    检查日志输出:您可以尝试在应用程序中输出SQL语句的日志,以便跟踪SQL语句的执行情况。您可以在Druid的配置文件中配置日志输出功能,并为每个数据源配置独立的日志输出配置。

    升级Druid版本:Druid是一个开源的Java数据库连接池和SQL解析库,最新的版本可能已经解决了您的问题。您可以尝试升级Druid的版本,并重新测试您的应用程序。

    2023-07-30 14:12:07
    赞同 展开评论 打赏
  • 找到错误了

    return DataSourceBuilder.create().build(); // 应该是 return DruidDataSourceBuilder.create().build();

    原回答者GitHub用户helloworlde

    2023-07-06 11:52:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载