springboot 整合 alibaba druid

in Java with 0 comment

阿里巴巴数据库事业部出品,为监控而生的数据库连接池 https://github.com/alibaba/druid/wiki

maven 安装

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.16</version>
</dependency>

设置配置

druid 属性详情 可参考 :https://www.cnblogs.com/halberd-lee/p/11304790.html

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: 
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      clear-filters-enable: true
      initial-size: 1
      max-active: 20
      max-wait: 60000
      filters: stat,wall,slf4j

启动配置类

@Configuration
public class DruidConfiguration {
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//设置ip白名单
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
        servletRegistrationBean.addInitParameter("deny", "192.168.0.19");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean<WebStatFilter> statFilter() {
        //创建过滤器
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

然后就可以通过 http://localhost:8080/druid/ 登录可视化界面了

Responses