baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.76k stars 1.2k forks source link

请问动态数据源,如何配置druid的web-stat-filter和stat-view-servlet? #548

Closed ldcsaa closed 1 year ago

ldcsaa commented 1 year ago

以下配置无效,访问druid后台页面返回404

spring.datasource.dynamic:
  primary: master
  strict: true
  lazy: false
  seata: false
  druid:
    max-active: 10
    min-idle: 1
    max-wait: 3000
    initial-size: 3
    keep-alive: true
    test-while-idle: true
    time-between-eviction-runs-millis: 60000
    validation-query: SELECT 1
    validation-query-timeout: 1
    filters: stat,wall,slf4j
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    public-key: "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJRhKGNr3IAJcs4+0ys/4zmwUJoBYqAdx6EcyiWIBt7cIFS5QdruakTmGk8XOVPMVz9jOLOvhPfjLzgPgwaKo6UCAwEAAQ=="
    web-stat-filter:
      enabled: true
      session-stat-enable: true
      url-pattern: /*
      exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/__druid/*"
    stat-view-servlet:
      enabled: true
      url-pattern: "/__druid/*"
      login-username: admin
      login-password: 123456
      allow: localhost,127.0.0.1,192.168.*
      reset-enable: false
huayanYu commented 1 year ago

原生配

ldcsaa commented 1 year ago

原生配

原生配?什么意思呀,能简略讲讲么?

alvinkwok1 commented 1 year ago

@ldcsaa 意思是通过spring来配置原生的servlet 和filter,以下是一个例子


/**
 * Description
 *
 * @author alvinkwok
 * @since 2023/10/17
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * @description 注册一个StatViewServlet,进行druid监控页面配置
     * @return servlet registration bean
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
        //先配置管理后台的servLet,访问的入口为/druid/
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(
                new StatViewServlet(), "/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "root");
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * @description 注册一个过滤器,允许页面正常浏览
     * @return filter registration bean
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidStatFilter(){
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(
                new WebStatFilter());
        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

如果你使用的是动态做数据源刷新的情况,看下面的代码做相关filter配置,否则就直接配置在配置文件当中就行了。

    @PostMapping("/add")
    public Set<String> add(@RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        // 配置druid的相关filter
        DruidConfig druidConfig = new DruidConfig();
        Map<String, Object> stat = new HashMap<>();
        stat.put("setSlowSqlMillis", 30);
        stat.put("setLogSlowSql", true);
        stat.put("setMergeSql", true);
        druidConfig.setStat(stat);
        druidConfig.setFilters("stat");
        dataSourceProperty.setDruid(druidConfig);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPoolName(), dataSource);
        return ds.getDataSources().keySet();
    }
alvinkwok1 commented 1 year ago

@ldcsaa 以上的代码仅供参考,具体情况参考源码配置