spring boot配置druid数据源和监控配置

直接上代码:

一.pom.xml中添加依赖

 <dependency>
<groupId>com.github.drtrang</groupId>
<artifactId>druid-spring-boot2-starter</artifactId>
<version>1.1.9</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>

二.配置数据源与监控

第一种方法通过properties配置

 #配置数据源
spring.datasource.url=jdbc:mysql://106.14.xx.xx:3306/test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.username=123456
spring.datasource.druid.password=123456
 1 # 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#spring.datasource.druid.filter-class-names=com.alibaba.druid.filter.stat.StatFilter
#spring.datasource.druid.filter-class-names=com.alibaba.druid.wall.WallFilter
#spring.datasource.druid.filter-class-names=com.alibaba.druid.filter.logging.Log4jFilter,com.alibaba.druid.filter.stat.StatFilter,com.alibaba.druid.wall.WallFilter
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
#spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#spring.datasource.druid.connect-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true # WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
#是否开启web-jdbc监控 默认是false
spring.datasource.druid.web-stat.enabled=true
#是否开启session统计 默认false
spring.datasource.druid.web-stat.session-stat-enable=true
#设置session统计的最大值 默认是1000
spring.datasource.druid.web-stat.session-stat-max-count=1000
#是否开启单个监控url调用的sql列表 默认开启
spring.datasource.druid.web-stat.profile-enable=true
#过滤器url的映射规则
spring.datasource.druid.web-stat.url-patterns=/*
#过滤器url的排除规则
spring.datasource.druid.web-stat.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* # StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
#是否开启druid的数据统计界面 默认false
spring.datasource.druid.stat-view-servlet.enabled= true
#servlet映射规则,默认访问http:/**xxx/druid/
spring.datasource.druid.stat-view-servlet.url-mappings=/druid/*
#是否允许清空统计数据 默认false
spring.datasource.druid.stat-view-servlet.reset-enable= false
#设置登录名
spring.datasource.druid.stat-view-servlet.login-username= admin
#设置密码
spring.datasource.druid.stat-view-servlet.login-password= admin
#设置白名单
spring.datasource.druid.stat-view-servlet.allow= 127.0.0.1
#访问黑名单
#spring.datasource.druid.stat-view-servlet.deny= 192.168.32.139

第二种方法通过java配置类设置:

DruidConfiguration该类与主程序xxApplication同级
 package com.ieou.capsule;

 import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class DruidConfiguration { @Bean
public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); servletRegistrationBean.addInitParameter("loginUsername","admin");
servletRegistrationBean.addInitParameter("loginPassword","admin");
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
} @Bean
public FilterRegistrationBean statFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean;
}
}

三.效果图

spring boot配置druid数据源和监控配置

spring boot配置druid数据源和监控配置

spring boot配置druid数据源和监控配置

上一篇:ux.form.field.Year 只能选年的时间扩展


下一篇:Java中关键字this、super的含义及使用