SpringBoot + Druid monitors MySQL, slow SQL can be quickly located, really easy to use!

We have all used connection pools. For example C3P0,DBCP,hikari, Druid, although HikariCP is slightly faster, Druid can provide powerful monitoring and expansion functions, and it is also an open source project of Alibaba.

Druid is a database connection pool developed by Alibaba known as monitoring. It surpasses other database connection pools in terms of function, performance, and scalability, including, etc., killing everything in seconds DBCP、C3P0、BoneCP、Proxool、JBoss DataSource.

Druid can monitor the DB pool connection and SQL execution very well, and it is a DB connection pool for monitoring.

The default data source of Spring Boot HikariDataSourcehas JdbcTemplateintroduced that Spring Boot 2.x uses the Hikari data source by default. It can be said that Hikari and Driud are the best data sources on the Java Web.

And Druid has deployed more than 600 applications in Alibaba, after several years of severe tests of large-scale deployment in the production environment!

  • stat:  Druid provides one built-in StatFilterfor statistical monitoring information.

  • wall:  Druid's defense against SQL injection attacks WallFilteris through Druid's SQL Parser analysis. Druid SQL Parsercan intercept SQL at the JDBC layer for corresponding processing, such as sub-database sub-table, auditing, etc.

  • log4j2:  This is the logging function, which can print sql statements to log4j2 for troubleshooting.

1. Related configuration

1.1 Add dependencies

<properties>
    <java.version>1.8</java.version>
    <alibabaDruidStarter.version>1.2.11</alibabaDruidStarter.version>
</properties>

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

1.2 Configuration Properties

  • Configure Druid data source (connection pool):  Just like c3p0 and dbcp data sources can set data source connection initialization size, maximum number of connections, waiting time, minimum number of connections, etc., Druid data source can be set in the same way.

  • Configure Druid web monitoring filter (WebStatFilter):  The function of this filter is to count all database information in web application requests, such as the issued sql statement, sql execution time, number of requests, requested url address, and seesion monitoring, database table visits, etc.

  • Configure the Druid background management Servlet (StatViewServlet):  The Druid data source has a monitoring function and provides a web interface for users to view. Similar to when installing a router, they also provide a default web page; you need to set the Druid background management page. Attributes, such as login account, password, etc.

[Note]: The name of the configuration property follows Druid completely. You can configure the Druid database connection pool and monitoring through the Spring Boot configuration file. If there is no configuration, use the default value. Configure the relevant properties Druid Spring Boot Starteras follows:application.yml

# spring 配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: 123456
    username: root
    url: jdbc:mysql://localhost:3306/superjson?useUnicode=true&characterEncoding=utf8&useSSL=false
    # 连接池配置
    druid:
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM user
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      # 打开 PSCache,并且指定每个连接上 PSCache 的大小
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置监控统计拦截的 Filter,去掉后监控界面 SQL 无法统计,wall 用于防火墙
      filters: stat,wall,slf4j
      # 通过 connection-properties 属性打开 mergeSql 功能;慢 SQL 记录
      connection-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      # 配置 DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
      # 配置 DruidStatViewServlet
      stat-view-servlet:
        url-pattern: /druid/*
        # IP 白名单,没有配置或者为空,则允许所有访问
        allow: 127.0.0.1
        # IP 黑名单,若白名单也存在,则优先使用
        deny: 192.168.31.253
        # 禁用 HTML 中 Reset All 按钮
        reset-enable: false
        # 登录用户名/密码
        login-username: root
        login-password: 123456
        # 需要设置enabled=true,否则会报出There was an unexpected error (type=Not Found, status=404).错误,或者将druid-spring-boot-starter的版本降低到1.1.10及以下
        # 是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全)
        enabled: true

The parameters for the above configuration files can be found in com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatPropertiesand  org.springframework.boot.autoconfigure.jdbc.DataSourcePropertie.

1.3 Configuring Filters

spring.datasource.druid.filters=stat,wall,log4j ...The corresponding built-in Filter can be enabled through the method, but these Filters are all default configurations. If the default configuration cannot meet the requirements, you can abandon this method and configure Filter through the configuration file, as follows:

# 配置StatFilter 
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.db-type=h2
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# 配置WallFilter 
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=h2
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false

Currently, configuration support is provided for the following Filters, which spring.datasource.druid.filter.*are configured according to ( ).

  • StatFilter

  • WallFilter

  • ConfigFilter

  • EncodingConvertFilter

  • Slf4jLogFilter

  • Log4jFilter

  • Log4j2Filter

  • CommonsLogFilter

If you don't want to use the built-in Filters, you need to set the corresponding Filter's enabled to true to make the custom Filter configuration effective. Druid Spring Boot StarterStatFilter is disabled by default, and you can set its enabled to true to enable it.

2. Monitoring page

2.1 After starting the project, access http://localhost:8081/druid/login.htmlto the login page, enter the user name and password to log in, as shown below:

2.2 The data source page is the basic information of the current DataSource configuration. The Filter configured above can be found there. If no Filter is configured (some information will not be counted, for example, SQL monitoring will not be able to obtain JDBC-related SQL execution information)

2.3 SQL monitoring page, which counts the execution of all SQL statements

2.4 URL monitoring page, which counts the access and execution of all Controller interfaces

2.5 Spring monitoring page, using aop to record the execution time and jdbc number of the specified interface

2.6 SQL firewall page

Druid provides access to the black and white lists, and you can clearly see the sql protection situation.

2.7 Session monitoring page

You can see the current session status, detailed parameters such as creation time, last active time, number of requests, and request time.

2.8 JSON API page

Access Druid's monitoring interface in the form of api, and the api interface returns data in the form of Json.

3. SQL monitoring

Configure the Druid web monitoring filter ( WebStatFilter), which is used to count all database information in web application requests, such as issued sql statements, sql execution time, number of requests, requested url address, and seesion monitoring, database table access Times, configured as follows:

spring:
  datasource:
    druid:
      ########## 配置WebStatFilter,用于采集web关联监控的数据 ##########
      web-stat-filter:
        enabled: true                   # 启动 StatFilter
        url-pattern: /*                 # 过滤所有url
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # 排除一些不必要的url
        session-stat-enable: true       # 开启session统计功能
        session-stat-max-count: 1000    # session的最大个数,默认100

4. Slow sql records

Sometimes, some SQL in the system executes very slowly, and we want to use logs to record them. You can enable Druid's slow SQL recording function, and configure it as follows:

spring:
  datasource:
    druid:
      filter:
        stat:
          enabled: true         # 开启DruidDataSource状态监控
          db-type: mysql        # 数据库的类型
          log-slow-sql: true    # 开启慢SQL记录功能
          slow-sql-millis: 2000 # 默认3000毫秒,这里超过2s,就是慢,记录到日志

After startup, if you encounter slow SQL, it will be output to the log

5. spring monitoring

After access, spring monitoring has no data by default, but it needs to import the AOP Starter of SprngBoot, as follows:

<!--SpringBoot 的aop 模块-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

At the same time, it needs to be configured in application.yml as follows:

Spring monitors AOP entry points, such as com.springboot.template.dao.*configuring multiple English comma-separated

spring.datasource.druid.aop-patterns="com.springboot.template.dao.*"

6. Remove advertising (Ad)

When visiting the monitoring page, you may see Alibaba's advertisement at the bottom of the page (footer), as shown below:

Reason: common.js in the imported druid jar package (there is a piece of js code in it to add advertisements to the footer of the page)

If you want to remove it, there are two ways:

6.1 Manually comment this code directly

If you use Maven, go directly to the local warehouse, find the jar package, and comment the following code:

// this.buildFooter();

The location of common.js:

com/alibaba/druid/1.1.23/druid-1.1.23.jar!/support/http/resources/js/common.js

6.2 Filtering with filters

Register a filter, filter common.jsthe request, and use regular expressions to replace relevant advertisement content, as shown in the following code:

@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled",
havingValue = "true", matchIfMissing = true)
public class RemoveDruidAdConfig {

    /**
    * 方法名: removeDruidAdFilterRegistrationBean
    * 方法描述 除去页面底部的广告
    * @param properties com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties
    * @return org.springframework.boot.web.servlet.FilterRegistrationBean
    */
    @Bean
    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {

        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");

        final String filePath = "support/http/resources/js/common.js";

        //创建filter进行过滤
        Filter filter = new Filter() {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException {}

            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                chain.doFilter(request, response);
                // 重置缓冲区,响应头不会被重置
                response.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);
                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                response.getWriter().write(text);
            }

            @Override
            public void destroy() {}
        };

        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);
        return registrationBean;
    }
}

Both methods are available, and it is recommended to use the first method to solve the problem from the root cause.

7. Obtain Druid monitoring data

The monitoring data of Druid can be obtained StatFilterby DruidStatManagerFacadeusing;

DruidStatManagerFacade#getDataSourceStatDataListThis method can obtain monitoring data from all data sources. In addition, DruidStatManagerFacadesome other methods are provided, which can be selected and used as needed.

@RestController
@RequestMapping(value = "/druid")
public class DruidStatController {

    @GetMapping("/stat")
    public Object druidStat(){
        // 获取数据源的监控数据
        return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
    }
}

 

Guess you like

Origin blog.csdn.net/2301_77463738/article/details/131484742