preface

In the article “MyBatis use and lack of thinking”, pointed out the use of MyBatis insufficiency. As follows:

  1. Each entity needs to write add, delete, change and check SQL, which feels like repetitive work
  2. Without paging plug-ins, paging queries are more troublesome
  3. Without unit testing, interface testing is a hassle
  4. Without SQL monitoring, the running status of SQL cannot be counted

The first three have been addressed in the previous articles. This article addresses the problem of monitoring.

Druid provides a database connection pool for monitoring.

The specific implementation

Maven rely on

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.14</version>
    </dependency>
</dependencies>
Copy the code

Druid monitoring page

Druid has its own monitoring page, if you want to visit only need to configure the parameters of the spring. The datasource. The Druid. Stat – view – servlet. Enabled = true.

Start the project, visit http://localhost:8080/druid, see the monitoring page.

Because the information in the monitoring page is sensitive, it is generally authenticated for login. To implement login authentication, set the following parameters:

# login account and password specified spring. The datasource. The druid. Stat - view - servlet. Login - username = root spring.datasource.druid.stat-view-servlet.login-password=123456Copy the code

After restarting the project, access will discover that login is required to access.

Then look at the TAB page of the monitoring page, you will find that the functions above include data source, SQL monitoring, SQL firewall, Web application, URL monitoring, Session monitoring and Spring monitoring.

By default, only the data source can be viewed. All other defaults are turned off. Configure one by one.

SQL monitoring and SQL firewall

By configuring spring. The datasource. The druid. Filters = stat, wall open firewall monitoring SQL and SQL.

After a database access operation is performed, the SQL execution times and time are displayed on the SQL monitoring page as follows:

On the SQL firewall page, you can view table access, whitelist statistics, and blacklist statistics as follows:

Web applications and URL monitoring

By configuring spring. The datasource. The druid. Web – stat – filter. Enabled = true open web application, the monitoring and controlling of the URL.

On the WEB application page, you can view the following information about the project:

On the URL monitoring page, you can view the request URI information, including the request time and maximum number of concurrent requests, as follows:

The Session monitoring

By configuring spring. The datasource. The druid. Web – stat – filter. The session – stat – enable = true open session monitoring. On the Session monitoring page, you can view the execution information about each Session as follows:

Spring monitoring

By configuring spring. The datasource. The druid. Aop – patterns = com. Zhuqc. Framework. * specified spring monitoring scope. On the Spring monitoring page, you can view the execution times and execution time of each method in the class as follows:

Complete configuration

spring:
  datasource:
    druid:
      stat-view-servlet:
        The Druid monitoring page is displayed
        enabled: true
        Access path
        url-pattern: /druid/*
        Set the account password
        login-username: root
        login-password: 123456
        # reset button
        reset-enable: true
      web-stat-filter:
        # Enable URI monitoring for Web applications
        enabled: true
        # Enable Session monitoring
        session-stat-enable: true
        # intercept path
        url-pattern: / *
      SQL monitor SQL firewall
      filters: stat,wall
      # Enable Spring monitoring
      aop-patterns: com.zhuqc.framework.*
Copy the code

conclusion

Druid’s monitoring capabilities are very powerful, not just for SQL monitoring. And Druid is so easy to use that you don’t have to write any code, you just add some configuration.

Above, thanks for reading, if you feel helpful, might as well click a like!

The source code

Github.com/zhuqianchan…

Review past

  • Build backend frameworks from scratch – keep updating