springboot项目创建笔记34 之《配置druid监控页面》

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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值