spring boot 开启Druid监控功能

Posted Steven.Li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 开启Druid监控功能相关的知识,希望对你有一定的参考价值。

1.配置yml

 1 spring:
 2   datasource:
 3 #   数据源基本配置
 4     username: song
 5     password: 123456
 6     driver-class-name: com.mysql.jdbc.Driver
 7     url: jdbc:mysql://39.106.222.34:3306/mybatis
 8     type: com.alibaba.druid.pool.DruidDataSource
 9 #   数据源其他配置
10     initialSize: 5
11     minIdle: 5
12     maxActive: 20
13     maxWait: 60000
14     timeBetweenEvictionRunsMillis: 60000
15     minEvictableIdleTimeMillis: 300000
16     validationQuery: SELECT 1 FROM DUAL
17     testWhileIdle: true
18     testOnBorrow: false
19     testOnReturn: false
20     poolPreparedStatements: true
21 #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
22     filters: stat,wall,log4j
23     maxPoolPreparedStatementPerConnectionSize: 20
24     useGlobalDataSourceStat: true
25     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

2.java 代码 

@Configuration
public class DruidConfiguration {

    @Bean
    public ServletRegistrationBean statViewServle(){
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //IP白名单
        //servletRegistrationBean.addInitParameter("allow","192.168.1.12,127.0.0.1");
        //IP黑名单
        //servletRegistrationBean.addInitParameter("deny","192.168.4.23");
        //控制台用户
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        //是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

3。访问监控  http://localhost/druid/weburi.html  

输入java代码里配置的用户名和密码

 

以上是关于spring boot 开启Druid监控功能的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot:spring boot使用Druid和监控配置

Spring Boot 如何监控 SQL 运行情况?

Spring boot 数据库连接池

Spring boot 数据库连接池

Spring Boot使用Druid和监控配置

Druid监控Admin使用记录