Springboot新增阿里巴巴 Druid監控
阿新 • • 發佈:2018-12-18
1.Maven載入jar包
<!--阿里巴巴durid https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency>
2.在application.properties配置檔案中寫相關配置資訊
server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/thirdteam?useSSL=false&characterEncoding=UTF-8&?useUnicode=true spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.filter=stat,wall,log4j spring.datasource.tomcat.initial-size=1 spring.datasource.maxWait = 60000 spring.datasource.minIdle = 1 spring.datasource.timeBetweenEvictionRunsMillis = 60000 spring.datasource.minEvictableIdleTimeMillis = 300000 spring.datasource.validationQuery = select 'x' spring.datasource.testWhileIdle = true spring.datasource.testOnBorrow = false spring.datasource.testOnReturn = false spring.datasource.poolPreparedStatements = true spring.datasource.maxOpenPreparedStatements = 20
3.新增Druid配置
package com.song.util.Durid; import com.alibaba.druid.support.http.StatViewServlet; import org.apache.catalina.filters.WebdavFixFilter; 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; import javax.servlet.ServletRegistration; @Configuration public class DuridConfiguration { @Bean//宣告bean例項 public ServletRegistrationBean stateViewServlet(){ //建立servlet註冊實體 ServletRegistrationBean servletRegistrationBean =new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //設定ip白名單 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //設定ip黑名單,如果allow與deny共同存在時,deny優於allow servletRegistrationBean.addInitParameter("denny","192.168.0.100"); //設定druid登入入口,1267.0.0.1/druid/login.html servletRegistrationBean.addInitParameter("loginUsername","druid"); servletRegistrationBean.addInitParameter("loginPassword","123456"); servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ //建立過濾器 FilterRegistrationBean filterRegistrationBean =new FilterRegistrationBean(new WebdavFixFilter()); //設定過濾路徑 filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,/druid/*"); return filterRegistrationBean; } }