Spring|SpringBoot 配置Druid监控页面

首先创建druid配置类:用户名、密码需要自行修改。

package org.demo; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /**** * 配置好sruid之后,数据源是从默认的tomcat.pool切换到了druid, * 可是druid的属性比如initialSize、minIdle还未生效, * 因为在DataSourceProperties.class下没有对应的匹配项, * 还需要我们手动配置同时配置监控界面: * */ public class DruidConfig { // 将所有前缀为spring.datasource下的配置项都加载到DataSource中@ConfigurationProperties(prefix ="spring.datasource") @Bean public DruidDataSource druidDataSource() {return new DruidDataSource(); }@Bean public ServletRegistrationBean druidStatViewServlet() {ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); registrationBean.addInitParameter("allow", "127.0.0.1"); // IP白名单 (没有配置或者为空,则允许所有访问) registrationBean.addInitParameter("deny", ""); // IP黑名单 (存在共同时,deny优先于allow) registrationBean.addInitParameter("loginUsername", "admin"); registrationBean.addInitParameter("loginPassword", "admin"); registrationBean.addInitParameter("resetEnable", "false"); return registrationBean; }@Bean public FilterRegistrationBean druidWebStatFilter() { FilterRegistrationBean filterRegistrationBean =new FilterRegistrationBean(new WebStatFilter()); Map initParams =new HashMap<>(); initParams.put("exclusions", "*.js,*.css,/druid/*"); filterRegistrationBean.setInitParameters(initParams); filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); return filterRegistrationBean; } }

【Spring|SpringBoot 配置Druid监控页面】由于需要验证账户密码,所以需要在application.properties配置原始账户密码:
spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=admin

起动项目:浏览器输入:http://localhost:6000/druid/login.html
登录账户密码,可以看到后台界面,操作项目业务之后,查看各项日志记录:
Spring|SpringBoot 配置Druid监控页面
文章图片

    推荐阅读