当前位置:   article > 正文

springboot项目创建笔记34 之《配置druid监控页面》_filters: stat,wall,slf4j

filters: stat,wall,slf4j

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文件配置:

  1. spring:
  2. datasource:
  3. druid:
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. type: com.alibaba.druid.pool.DruidDataSource
  6. url: jdbc:mysql://localhost:3306/webapp2?serverTimezone=UTC
  7. username: user2
  8. #654321
  9. password: ENC(TSNRe9Ou+hxupLlQ3me15Q==)
  10. #配置监控统计拦截的filters
  11. filters: stat,wall,slf4j
  12. max-active: 20
  13. initial-size: 1
  14. max-wait: 60000
  15. min-idle: 1
  16. time-between-eviction-runs-millis: 60000
  17. min-evictable-idle-time-millis: 300000
  18. test-while-idle: true
  19. test-on-borrow: false
  20. test-on-return: false
  21. pool-prepared-statements: true
  22. max-open-prepared-statements: 20
  23. async-init: true
  24. filter:
  25. slf4j:
  26. enabled: true
  27. statement-create-after-log-enabled: false
  28. statement-close-after-log-enabled: false
  29. result-set-open-after-log-enabled: false
  30. result-set-close-after-log-enabled: false
  31. stat:
  32. enabled: true
  33. #合并多个DruidDataSource的监控数据(多数据源时用)
  34. #use-global-data-source-stat: true
  35. #配置监控用户和密码以及访问ip
  36. stat-view-servlet:
  37. #默认关闭了监控页面
  38. enabled: true
  39. login-username: admin
  40. login-password: admin
  41. allow: 127.0.0.1

2、拦截器添加排除

  1. package com.example.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  8. /**
  9. * 拦截器配置类
  10. *
  11. * @author User
  12. *
  13. */
  14. @Configuration
  15. public class WebMvcConfig implements WebMvcConfigurer {
  16. // 在此处,将拦截器注册为一个Bean,才能使用@Autowired注解注入对象
  17. @Bean
  18. public RateLimitInterceptor rateLimitInterceptor() {
  19. return new RateLimitInterceptor();
  20. }
  21. @Override
  22. public void addInterceptors(InterceptorRegistry registry) {
  23. // 注册RateLimitInterceptor拦截器
  24. // 要注意这里使用this.rateLimitInterceptor()
  25. InterceptorRegistration registration = registry.addInterceptor(this.rateLimitInterceptor());
  26. // 所有路径都被拦截
  27. registration.addPathPatterns("/**")
  28. // 排除拦截swagger页面
  29. .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
  30. "doc.html", "/error")
  31. // 排除拦截webapp目录下的jsp和html页面
  32. .excludePathPatterns("/*.jsp", "/*.html")
  33. // 排除拦截druid
  34. .excludePathPatterns("/druid/**");
  35. WebMvcConfigurer.super.addInterceptors(registry);
  36. }
  37. @Override
  38. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  39. registry.addResourceHandler("swagger-ui.html", "doc.html")
  40. .addResourceLocations("classpath:/META-INF/resources/");
  41. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  42. }
  43. }

3、启动应用,访问监控页面
http://127.0.0.1:8088/druid/index.html

参考资料:
https://github.com/alibaba/druid/issues/3076

注:最新代码上传至https://github.com/csj50/myboot
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/844160
推荐阅读
相关标签
  

闽ICP备14008679号