Druid is a database connection pool component developed by Alibaba middleware team. It includes powerful connection pool monitoring features.

Add Dependencies

Add Druid dependency into pom.xml

1
2
3
4
5
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.16</version>
</dependency>

Add Guava dependency into pom.xml

1
2
3
4
5
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>27.0-jre</version>
</dependency>

Comment and Add Properties

Comment Previous Data Source Properties

Comment out previous data source properties in the application.properties file:

1
2
3
4
#spring.datasource.url=jdbc:mysql://localhost:3307/shop?serverTimezone=UTC
#spring.datasource.username=root
#spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Add Druid Data Source Properties

Add Druid data source properties into application.properties file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
spring.druid.url=jdbc:mysql://localhost:3307/shop?characterEncoding=UTF-8&serverTimezone=UTC
spring.druid.username=root
spring.druid.password=
spring.druid.driverClassName=com.mysql.cj.jdbc.Driver

# Idle connections minimum idled time: at least 4 minutes
spring.druid.minEvictableIdleTimeMillis=240000
# Idle connections checking interval: every 9 seconds
spring.druid.timeBetweenEvictionRunsMillis=54000
# Prevent MySQL's eight hours problem
spring.druid.validationQuery=SELECT 'x'
# Maximum connections: 50 connections
spring.druid.maxActive=50
# Maximum wait time: for 10 seconds
spring.druid.maxWait=10000
# Minimum idle connections: 5 idle connections
spring.druid.minIdle=5

Create Druid Config Class

  • Right click java directory: New > Package
  • Fill in: co.dongchen.shop.config
  • Click “OK” button
img
  • Right click co.dongchen.shop.config directory: New > Java Class
  • Fill in: DruidConfig
  • Click “OK” button
img

Copy and paste the following content into DruidConfig.java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package co.dongchen.shop.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.google.common.collect.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {

    // Read properties which start with a spring.druid prefix from the application.properties file
    @ConfigurationProperties(prefix = "spring.druid")

    // initMethod: initialize druid through its internal method called init
    // destroyMethod: release druid connections through its internal method called close
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        return dataSource;
    }

    @Bean
    public Filter statFilter() {
        StatFilter filter = new StatFilter();
        filter.setSlowSqlMillis(1000);    // Any SQL that exceeds 1 second is a slow SQL
        filter.setLogSlowSql(true);    // Logging slow SQL
        filter.setMergeSql(true);    // Merge logs
        return filter;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }
}

Verify

Run the App

1
2
// Short Cut for "Run the Program"
Alt + Shift + F10
img

Choose the correspondent option and press enter.

View Druid Monitoring Dashboard in the Browser

1
http://localhost:8080/druid/datasource.html
img

It might be a Chinese Simplified version, if so, please click the “English” linkage at the top right of the page:

img

References Alibaba Druid, Google Guava

Buy me a coffeeBuy me a coffee