druid连接池配置监控界面

druid连接池配置监控界面

  1. 引入druid连接池依赖

    <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.2.15</version>
            </dependency>
    
  2. 在application.yml配置文件中添加数据库配置

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/botou?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
        username: root
        password: sdhs2020
        #连接池的配置信息
        initialSize: 10
        minIdle: 10
        maxActive: 100
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        type: com.alibaba.druid.pool.DruidDataSource
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat,wall,log4j
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        driver-class-name: com.alibaba.druid.proxy.DruidDriver
    
  3. 编写配置类

    @Configuration
    public class DruidConfig {
        //登陆账号
        private final String USERNAME = "sc";
        //登陆密码
        private final String PASSWORD = "12345678";
        @Value("${spring.datasource.url:#{null}}")
        private String dbUrl;
        @Value("${spring.datasource.username: #{null}}")
        private String username;
        @Value("${spring.datasource.password:#{null}}")
        private String password;
        @Value("${spring.datasource.driverClassName:#{null}}")
        private String driverClassName;
        @Value("${spring.datasource.initialSize:#{null}}")
        private Integer initialSize;
        @Value("${spring.datasource.minIdle:#{null}}")
        private Integer minIdle;
        @Value("${spring.datasource.maxActive:#{null}}")
        private Integer maxActive;
        @Value("${spring.datasource.maxWait:#{null}}")
        private Integer maxWait;
        @Value("${spring.datasource.timeBetweenEvictionRunsMillis:#{null}}")
        private Integer timeBetweenEvictionRunsMillis;
        @Value("${spring.datasource.minEvictableIdleTimeMillis:#{null}}")
        private Integer minEvictableIdleTimeMillis;
        @Value("${spring.datasource.validationQuery:#{null}}")
        private String validationQuery;
        @Value("${spring.datasource.testWhileIdle:#{null}}")
        private Boolean testWhileIdle;
        @Value("${spring.datasource.testOnBorrow:#{null}}")
        private Boolean testOnBorrow;
        @Value("${spring.datasource.testOnReturn:#{null}}")
        private Boolean testOnReturn;
        @Value("${spring.datasource.poolPreparedStatements:#{null}}")
        private Boolean poolPreparedStatements;
        @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:#{null}}")
        private Integer maxPoolPreparedStatementPerConnectionSize;
        @Value("${spring.datasource.filters:#{null}}")
        private String filters;
        @Value("{spring.datasource.connectionProperties:#{null}}")
        private String connectionProperties;
        @Bean
        @Primary
        public DruidDataSource dataSource() {
            DruidDataSource datasource = new DruidDataSource();
            datasource.setUrl(this.dbUrl);
            datasource.setUsername(username);
            datasource.setPassword(password);
            datasource.setDriverClassName(driverClassName);
            //configuration
            if (initialSize != null) {
                datasource.setInitialSize(initialSize);
            }
            if (minIdle != null) {
                datasource.setMinIdle(minIdle);
            }
            if (maxActive != null) {
                datasource.setMaxActive(maxActive);
            }
            if (maxWait != null) {
                datasource.setMaxWait(maxWait);
            }
            if (timeBetweenEvictionRunsMillis != null) {
                datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            }
            if (minEvictableIdleTimeMillis != null) {
          datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            }
            if (validationQuery != null) {
                datasource.setValidationQuery(validationQuery);
            }
            if (testWhileIdle != null) {
                datasource.setTestWhileIdle(testWhileIdle);
            }
            if (testOnBorrow != null) {
                datasource.setTestOnBorrow(testOnBorrow);
            }
            if (testOnReturn != null) {
                datasource.setTestOnReturn(testOnReturn);
            }
            if (poolPreparedStatements != null) {
                datasource.setPoolPreparedStatements(poolPreparedStatements);
            }
            if (maxPoolPreparedStatementPerConnectionSize != null) {        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            }
            if (connectionProperties != null) {
                datasource.setConnectionProperties(connectionProperties);
            }
            List<Filter> filters = new ArrayList<>();
            filters.add(statFilter());
            filters.add(wallFilter());
            datasource.setProxyFilters(filters);
            return datasource;
        }
        @Bean
        public ServletRegistrationBean druidServlet() {
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
            servletRegistrationBean.setServlet(new StatViewServlet());
            servletRegistrationBean.addUrlMappings("/druid/*");
            Map<String, String> initParameters = new HashMap<>();
            initParameters.put("resetEnable", "false"); //禁用HTML页面上的“Rest All”功能
            //initParameters.put("allow", "10.8.9.115");  //ip白名单(没有配置或者为空,则允许所有访问)
            initParameters.put("loginUsername", USERNAME);  //++监控页面登录用户名
            initParameters.put("loginPassword", PASSWORD);  //++监控页面登录用户密码
            //initParameters.put("deny", ""); //ip黑名单
            //如果某个ip同时存在,deny优先于allow
            servletRegistrationBean.setInitParameters(initParameters);
            return servletRegistrationBean;
        }
        @Bean
        public StatFilter statFilter() {
            StatFilter statFilter = new StatFilter();
            statFilter.setLogSlowSql(true);
            statFilter.setMergeSql(true);
            statFilter.setSlowSqlMillis(3000);
            return statFilter;
        }
        @Bean
        public WallFilter wallFilter() {
            WallFilter wallFilter = new WallFilter();
            //允许执行多条SQL
            WallConfig config = new WallConfig();
            config.setMultiStatementAllow(true);
            wallFilter.setConfig(config);
            return wallFilter;
        }
    }
    
  4. 运行项目,访问http://localhost:8051/druid

    image-20230206172543360

    image-20230206172613524

  5. 登陆账号和密码

    image-20230206172652084

posted @ 2023-02-06 17:29  striver-sc  阅读(624)  评论(0编辑  收藏  举报