赞
踩
druid数据库连接池自带监控,添加配置打开。
1、监控需要的配置项
#配置监控统计拦截的filters
spring.datasource.druid.filters= stat,wall,slf4j
#默认关闭了监控页面
spring.datasource.druid.filter.stat.enabled= true
#默认关闭了监控页面
spring.datasource.druid.stat-view-servlet.enabled= true
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
完整yaml文件配置:
- spring:
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- type: com.alibaba.druid.pool.DruidDataSource
- url: jdbc:mysql://localhost:3306/webapp2?serverTimezone=UTC
- username: user2
- #654321
- password: ENC(TSNRe9Ou+hxupLlQ3me15Q==)
- #配置监控统计拦截的filters
- filters: stat,wall,slf4j
- max-active: 20
- initial-size: 1
- max-wait: 60000
- min-idle: 1
- time-between-eviction-runs-millis: 60000
- min-evictable-idle-time-millis: 300000
- test-while-idle: true
- test-on-borrow: false
- test-on-return: false
- pool-prepared-statements: true
- max-open-prepared-statements: 20
- async-init: true
- filter:
- slf4j:
- enabled: true
- statement-create-after-log-enabled: false
- statement-close-after-log-enabled: false
- result-set-open-after-log-enabled: false
- result-set-close-after-log-enabled: false
- stat:
- enabled: true
- #合并多个DruidDataSource的监控数据(多数据源时用)
- #use-global-data-source-stat: true
- #配置监控用户和密码以及访问ip
- stat-view-servlet:
- #默认关闭了监控页面
- enabled: true
- login-username: admin
- login-password: admin
- allow: 127.0.0.1

2、拦截器添加排除
- package com.example.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- /**
- * 拦截器配置类
- *
- * @author User
- *
- */
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
-
- // 在此处,将拦截器注册为一个Bean,才能使用@Autowired注解注入对象
- @Bean
- public RateLimitInterceptor rateLimitInterceptor() {
- return new RateLimitInterceptor();
- }
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- // 注册RateLimitInterceptor拦截器
- // 要注意这里使用this.rateLimitInterceptor()
- InterceptorRegistration registration = registry.addInterceptor(this.rateLimitInterceptor());
- // 所有路径都被拦截
- registration.addPathPatterns("/**")
- // 排除拦截swagger页面
- .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
- "doc.html", "/error")
- // 排除拦截webapp目录下的jsp和html页面
- .excludePathPatterns("/*.jsp", "/*.html")
- // 排除拦截druid
- .excludePathPatterns("/druid/**");
- WebMvcConfigurer.super.addInterceptors(registry);
- }
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("swagger-ui.html", "doc.html")
- .addResourceLocations("classpath:/META-INF/resources/");
- registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
-
- }
-
- }

3、启动应用,访问监控页面
http://127.0.0.1:8088/druid/index.html
参考资料:
https://github.com/alibaba/druid/issues/3076
注:最新代码上传至https://github.com/csj50/myboot
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。