SpringBoot数据访问------Druid数据源(添加监控)

DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池。

SpringBoot整合Druid

1.添加Maven依赖
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.8version>
dependency>
2.添加配置

application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.10.173:3306/jdbc
    driver-class-name: com.mysql.jdbc.Driver

    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
#   配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
#   配置一个连接在池中最小生存的时间,单位是毫秒  
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
#   打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
#   合并多个DruidDataSource的监控数据
    useGlobalDataSourceStat: true
#   通过connectProperties属性来打开mergeSql功能;慢SQL记录  
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3.开启Druid监控功能
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix="spring.datasource")
    @Bean
    public DataSource durid(){
        return new DruidDataSource();
    }

    //配置Druid的监控

    //1.配置一个管理后台的sevlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap();

        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "123456");
        //设置ip白名单
        initParams.put("allow", "");
        //设置ip黑名单。deny优先级高于allow
        initParams.put("deny", "192.168.10.125");

        bean.setInitParameters(initParams);
        return bean;
    }

    //2.配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map initParams = new HashMap();
        //忽略过滤的形式
        initParams.put("exclusions", "*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);
        //设置过滤器过滤路径
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
4.检验配置是否生效
  • 访问http://localhost:8080/druid/login.html
  • 以上述配置的账号密码登录(admin,123456)
    SpringBoot数据访问------Druid数据源(添加监控)_第1张图片
    SpringBoot数据访问------Druid数据源(添加监控)_第2张图片
5.可能出现错误

错误描述:

Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:

    Property: spring.datasource.filters
    Value: stat,wall,log4j
    Origin: class path resource [application.yml]:21:14
    Reason: Unable to set value for property filters

解决办法:

<dependency>
   <groupId>log4jgroupId>
   <artifactId>log4jartifactId>
   <version>1.2.16version>
   <scope>compilescope>
dependency>

你可能感兴趣的:(springboot,springboot,springboot,druid,druid监控)