Springboot Druid +mybatis 多数据源监控,只能监控到主库SQL,从库不行 10C

配置如下
spring:
circle-datasource: #从库
#123.207.23.166:3306
name: db
url: jdbc:mysql://119.29.208.67:3307/ws?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: *****
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
minIdle: 5
maxActive: 100
initialSize: 10
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 50
removeAbandoned: true

spring:
datasource: #主库
#123.207.23.166:3306
name: db
url: jdbc:mysql://119.29.208.67:3306/ws?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: momo
password: *****
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
minIdle: 5
maxActive: 100
initialSize: 10
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 50
removeAbandoned: true
filters: stat
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
useGlobalDataSourceStat: true # 合并多个DruidDataSource的监控数据
druidLoginName: momo # 登录druid的账号
druidPassword: momo # 登录druid的密码

----------------------------------------------
   @Bean(name="circleDataSource",destroyMethod = "close", initMethod="init")
public DataSource dataSource(){
    DruidDataSource datasource =  new DruidDataSource();
    try {  
        datasource.setUrl(this.url);
       datasource.setDbType(type);
        datasource.setUsername(username);  
        datasource.setPassword(password);  
        datasource.setDriverClassName(driverClassName);  
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnBorrow);  
        datasource.setTestOnReturn(testOnReturn);  
        datasource.setPoolPreparedStatements(poolPreparedStatements);  
        datasource.setFilters(filters);
        datasource.setUseGlobalDataSourceStat(true);
    } catch (Exception e) {
        logger.error("druid configuration initialization filter", e);  
    }  
    return datasource;  
}

    ---------------------主库-------
      @Bean(name="dataSource",destroyMethod = "close", initMethod="init")
@Primary //不要漏了这
public DataSource dataSource(){
    DruidDataSource datasource = new DruidDataSource();
    try {  
        datasource.setUrl(this.dbUrl);  
        datasource.setDbType(dbType);
        datasource.setUsername(username);  
        datasource.setPassword(password);  
        datasource.setDriverClassName(driverClassName);  
        datasource.setInitialSize(initialSize);  
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnBorrow);  
        datasource.setTestOnReturn(testOnReturn);  
        datasource.setPoolPreparedStatements(poolPreparedStatements);  
        datasource.setFilters(filters);
        datasource.setUseGlobalDataSourceStat(true);
    } catch (SQLException e) {  
        logger.error("druid configuration initialization filter", e);  
    }  
    return datasource;  
}
/////////  下面是druid 监控访问的设置  /////////////////  
@Bean  
public ServletRegistrationBean druidServlet() {  
    ServletRegistrationBean reg = new ServletRegistrationBean();  
    reg.setServlet(new StatViewServlet());  
    reg.addUrlMappings("/druid/*");  //url 匹配  
  //  reg.addInitParameter("allow", "192.168.16.110,127.0.0.1"); // IP白名单 (没有配置或者为空,则允许所有访问)  
  //  reg.addInitParameter("deny", "192.168.16.111"); //IP黑名单 (存在共同时,deny优先于allow)  
    reg.addInitParameter("loginUsername", this.druidLoginName);//登录名  
    reg.addInitParameter("loginPassword", this.druidPassword);//登录密码  
    reg.addInitParameter("resetEnable", "false"); // 禁用HTML页面上的“Reset All”功能  
    return reg;  
}  

@Bean(name="druidWebStatFilter")  
public FilterRegistrationBean filterRegistrationBean() {  
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  
    filterRegistrationBean.setFilter(new WebStatFilter());  
    filterRegistrationBean.addUrlPatterns("/*");  
    filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); //忽略资源  
    filterRegistrationBean.addInitParameter("profileEnable", "true");  
    filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");  
    filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");  
    return filterRegistrationBean;  
}

    ----------------------------------------------------
    ![图片说明](https://img-ask.csdn.net/upload/201807/21/1532162386_693657.png)
    从库在数据源是监控了,但是sql没有
    ![图片说明](https://img-ask.csdn.net/upload/201807/21/1532162432_671619.png)

4个回答

图片说明

图片说明

原来是从库yml配置漏了
filters: stat

我的两个数据源都配置了,但是两个库同事调用sql时,sql监控就清空了,只有单独一个数据源时可以监控,不知道为什么。
yml配置没有生效,只能在代码里set拦截器

public DataSource dbOneDataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("stat,wall,slf4j");
        return druidDataSource;
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐