Spring Boot - 6

Spring Boot集成 Druid

以下是非常基础,非常简洁的配置,只是能让Druid生效,并且启动监控,详细的配置最好去官方Wiki查看

添加jar包

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

       <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

第一个必须加,第二个不加可能会没有自动提示,会有警告

配置数据源

spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbgril?characterEncoding=utf8&useSSL=true
    username: root
    password: pengfei

    druid:
      filter:
        stat:
          db-type: mysql
          log-slow-sql: true
          slow-sql-millis: 2000

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

至少要配置数据库的基础信息,如果是内嵌数据库,甚至用户名密码也可以没有

开启Druid监控

配置Filter

import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

@WebFilter(filterName = "druidStatFilter",urlPatterns = "/*",initParams = {
        @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")})
public class DruidStatFilter extends WebStatFilter {
}

继承WebStatFilter,并配置访问路径即可,exclusions是配置的额忽略资源

配置Servlet


import com.alibaba.druid.support.http.StatViewServlet;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/druid/*",
        initParams={
                @WebInitParam(name="allow",value="127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="admin"),// 用户名
@WebInitParam(name="loginPassword",value="123456"),// 密码
@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
        })
public class DruidStatViewServlet extends StatViewServlet {
}

继承StatViewServlet,需要在Application 启动类上添加注解@ServletComponentScan,否则无法启动Servlet

# 开发 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×