SpringBoot專案的Druid監控配置
阿新 • • 發佈:2018-12-18
1. 引入druid的Maven依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
2. application.yml配置
druid: resetEnable: false #管理員賬號 loginUsername: admin #管理員密碼 loginPassword: 123 #訪問IP限制 allow: '127.0.0.1,192.135.0.1/24' db: driver-class-name: com.mysql.cj.jdbc.Driver #連線數初始化大小,最小,最大 initialSize: 5 min-idle: 5 max-active: 20 # 配置獲取連線等待超時的時間 max-wait-millis: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 time-between-eviction-runs-millis: 3600000 # 配置一個連線在池中最小生存的時間,單位是毫秒 min-evictable-idle-time-millis: 3600000 # 配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆 filters: slf4j,wall,stat # 通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄 connection-properties: 'defaultRowPrefetch=5000;druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500' # 合併多個DruidDataSource的監控資料 use-global-data-source-stat: true #校驗SQL validation-query: SELECT 'x' test-while-idle: true test-on-borrow: true test-on-return: false # 開啟PSCache(快取遊標),並且指定每個連線上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 #其他 default-auto-commit: true connection-init-sqls: ["set names utf8"]
3. druid配置對應實體類
@Configuration
@ConfigurationProperties(prefix = "druid")
@Data
public class DruidProperties {
private String resetEnable;
private String loginUsername;
private String loginPassword;
private String allow;
}
4. druid監控配置
@Configuration public class DruidConfig { @Bean public StatFilter statFilter() { return new StatFilter(); } @Bean public WallConfig wallConfig() { return new WallConfig(); } @Bean public WallFilter wallFilter(WallConfig wallConfig) { WallFilter filter = new WallFilter(); filter.setConfig(wallConfig); return filter; } @Bean public Slf4jLogFilter slf4jLogFilter() { return new Slf4jLogFilter(); } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); filterRegistrationBean.setOrder(5); return filterRegistrationBean; } @Bean @Autowired public ServletRegistrationBean druidServlet(DruidProperties druidProperties) { ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); servletRegistrationBean.addInitParameter("resetEnable",druidProperties.getResetEnable()); servletRegistrationBean.addInitParameter("loginUsername",druidProperties.getLoginUsername()); servletRegistrationBean.addInitParameter("loginPassword",druidProperties.getLoginPassword()); servletRegistrationBean.addInitParameter("allow",druidProperties.getAllow()); return servletRegistrationBean; } @Bean public Advice advice() { return new DruidStatInterceptor(); } //patterns指定Mapper介面 @Bean public Advisor advisor() { String[] patterns = new String[]{"com.myproject.test.query.*.mapper.*", "com.myproject.test.*.domain.repository.*"}; return new RegexpMethodPointcutAdvisor(patterns, advice()); } @Bean public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); advisorAutoProxyCreator.setProxyTargetClass(true); return advisorAutoProxyCreator; } }