Springboot 中配置 druid 监控

2022-12-31 09:39:30

一、druid 的 maven 依赖

<!-- druid依赖 -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.9</version>
</dependency>

二、属性配置类

import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfiguration {
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        ServletRegistrationBean<StatViewServlet> srb = new ServletRegistrationBean
(new StatViewServlet(), "/druid/*");
        //IP白名单(没有配置或者为空,则允许所有访问)
        srb.addInitParameter("allow", "127.0.0.1");
        //IP黑名单(黑白均有时,deny优先于allow) : 
        //如果满足deny的即提示:Sorry, you are not permitted to view this page.
        srb.addInitParameter("deny", "192.168.1.100");
        //账号参数名必须为loginUsername
        srb.addInitParameter("loginUsername", "root");
        //密码参数名必须为loginPassword
        srb.addInitParameter("loginPassword", "root");
        //是否能够重置数据
        srb.addInitParameter("resetEnable", "false");
        return srb;
    }
}

三、访问监控页面

启动项目后,访问页面http://127.0.0.1:8080/druid/sql.html,就可以访问监控页面了,其中ip和端口号为项目的ip和端口号。页面如下:

  • 作者:JFS_Study
  • 原文链接:https://blog.csdn.net/ChineseSoftware/article/details/123112043
    更新时间:2022-12-31 09:39:30