springboot 整合druid,监控页面看不到数据源信息?????

孤狼独闯天宇
  • 930


配置信息

#spring配置
spring:
   application:
      name: gxc-webapp  #应用名
   datasource: #【配置数据源】
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      druid:
         db1: # 数据源1
            jdbc-url: jdbc:mysql://localhost:3306/user-info?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
            username: root
            password: root
         db2: # 数据源2
            jdbc-url: jdbc:mysql://localhost:3306/colleges?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
            username: root
            password: root
         # 【*************数据库初始化配置**************】
         initialization-mode: always
         # 初始连接数
         initial-size: 5
         # 最大连接池数量
         max-active: 10
         # 最小连接池数量
         min-idle: 5
         # 配置获取连接等待超时的时间
         max-wait: 60000
         # 打开PSCache,并且指定每个连接上PSCache的大小
         pool-prepared-statements: true
         max-pool-prepared-statement-per-connection-size: 20
         # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
         timeBetweenEvictionRunsMillis: 60000
         # 配置一个连接在池中最小生存的时间,单位是毫秒
         min-evictable-idle-time-millis: 300000
         validation-query: SELECT 1 FROM DUAL
         #是否在连接空闲一段时间后检测其可用性
         test-while-idle: true
         #是否在获得连接后检测其可用性
         test-on-borrow: false
         #是否在连接放回连接池后检测其可用性
         test-on-return: false
         webStatFilter:
            enabled: true
         statViewServlet:
            enabled: true
            # 设置白名单,不填则允许所有访问
            allow:
            url-pattern: /druid/*
            # 控制台管理用户名和密码
            login-username: root
            login-password: root
         filter:
            stat:
               enabled: true
               # 慢SQL记录
               log-slow-sql: true
               slow-sql-millis: 1000
               merge-sql: true
            wall:
               config:
                  multi-statement-allow: true

多数据源配置

@Configuration
public class DataSourceConfig {
    //数据源1
    @Bean(name = "user-info")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db1") // application.properteis中对应属性的前缀
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    //数据源2
    @Bean(name = "colleges")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2") // application.properteis中对应属性的前缀
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 动态数据源: 通过AOP在不同数据源之间动态切换
     * @return
     */
    @Primary
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("user-info", dataSource1());
        dsMap.put("colleges", dataSource2());
        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    /**
     * 配置多数据源@Transactional注解事物[只对单数据源有效]
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }
}

去除druid底部广告

@Configuration
public class DruidConfig {
    /**
     * 去除监控页面底部的广告
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
    {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
        final String filePath = "support/http/resources/js/common.js";
        // 创建filter进行过滤
        Filter filter = new Filter()
        {
            @Override
            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
            {
            }
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException
            {
                chain.doFilter(request, response);
                // 重置缓冲区,响应头不会被重置
                response.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);
                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                response.getWriter().write(text);
            }
            @Override
            public void destroy()
            {
            }
        };
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);
        return registrationBean;
    }
}
回复
阅读 553
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏