<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency>
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource username: root password: 123456 url: jdbc:mysql://localhost:3308/spring_boot?characterEncoding=UTF-8 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,log4j connectionProperties: ruid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 driver-class-name: com.mysql.jdbc.Driver jpa: database-platform: org.hibernate.dialect.MySQLDialect show-sql: true
# 數據庫訪問配置 # 主數據源,默認的 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 # 下面爲鏈接池的補充設置,應用到上面全部數據源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取鏈接等待超時的時間 spring.datasource.maxWait=60000 # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個鏈接在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打開PSCache,而且指定每一個鏈接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆 spring.datasource.filters=stat,wall,log4j # 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合併多個DruidDataSource的監控數據
@WebServlet(urlPatterns = "/druid/*", initParams = { @WebInitParam(name = "allow", value = "127.0.0.1"), // IP白名單 (沒有配置或者爲空,則容許全部訪問) @WebInitParam(name = "deny", value = ""), // IP黑名單 (存在共同時,deny優先於allow) @WebInitParam(name = "loginUsername", value = "admin"), // 用戶名 @WebInitParam(name = "loginPassword", value = "123456"), // 密碼 @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML頁面上的「Reset All」功能 }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = -7635469867140166016L; }
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源 }) public class DruidStatFilter extends WebStatFilter { }
最後在App.java類上加上註解:@ServletComponentScan是的spring可以掃描到咱們本身編寫的servlet和filter。css
@Configuration public class DuridConfiguration { /** * 註冊一個StatViewServlet * @return */ @Bean public ServletRegistrationBean DruidStatViewServle2() { //org.springframework.boot.context.embedded.ServletRegistrationBean提供類的進行註冊. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid2/*"); //添加初始化參數:initParams //白名單: servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); //IP黑名單 (存在共同時,deny優先於allow) : 若是知足deny的話提示:Sorry, you are not permitted to view this page. servletRegistrationBean.addInitParameter("deny", "192.168.1.73"); //登陸查看信息的帳號密碼. servletRegistrationBean.addInitParameter("loginUsername", "admin2"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); //是否可以重置數據. servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } /** * 註冊一個:filterRegistrationBean * @return */ @Bean public FilterRegistrationBean druidStatFilter2() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加過濾規則. filterRegistrationBean.addUrlPatterns("/*"); //添加不須要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"); return filterRegistrationBean; } }