SpringBoot essay - SpringBoot integrated Druid

Posted by Nik on Fri, 29 May 2020 16:55:46 +0200

1. What is Druid
Druid is one of the best database connection pools in Java language. Druid can provide powerful monitoring and extension functions.
2. Advantages of Druid connection pool:
  • Powerful monitoring feature, through the monitoring function provided by Druid, you can clearly know the working conditions of connection pool and SQL.
  • Easy to expand. Druid provides the extended API of Filter chain mode. You can write Filter to intercept any method in JDBC and do anything on it, such as performance monitoring, SQL audit, user name password encryption, log, etc.
  • Druid combines the excellent features of open source and commercial database connection pool, and optimizes it based on Alibaba's experience in large-scale and harsh production environment
3. Spring boot integrated Druid
1. Add Maven dependency
 1 <!-- Ali Druid Dependency package -->
 2 <dependency>
 3     <groupId>com.alibaba</groupId>
 4     <artifactId>druid-spring-boot-starter</artifactId>
 5     <version>1.1.21</version>
 6 </dependency>
 7 
 8 
 9 <!-- Druid rely on log4j package -->
10 <dependency>
11     <groupId>org.slf4j</groupId>
12     <artifactId>slf4j-log4j12</artifactId>
13 </dependency>

2. Configuration application.yml

 

 1 spring:
 2   datasource:
 3     username: root
 4     password: root
 5     url: jdbc:mysql://localhost:3306/security_authority?characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
 6     driver-class-name: com.mysql.jdbc.Driver
 7     type: com.alibaba.druid.pool.DruidDataSource
 8     # The following is the supplementary settings of the connection pool, which are applied to all data sources above
 9     # Initialization size, min, Max
10     druid.initial-size: 5
11     druid.min-idle: 5
12     druid.max-active: 20
13     # Configure the timeout time for getting connection waiting
14     druid.max-wait: 60000
15     # Configure how often to check the interval. Check the idle connections that need to be closed, in milliseconds
16     druid.time-between-eviction-runs-millis: 60000
17     # Configure the minimum lifetime of a connection in the pool, in milliseconds
18     druid.min-evictable-idle-time-millis: 300000
19     druid.validation-query: SELECT 1 FROM DUAL
20     druid.test-while-idle: true
21     druid.test-on-borrow: false
22     druid.test-on-return: false
23     # Open PSCache and specify the size of PSCache on each connection
24     druid.pool-prepared-statements: true
25     #   Configure the filters intercepted by monitoring statistics. After the filters are removed, the monitoring interface sql cannot be counted. The 'wall' is used for the firewall
26     druid.max-pool-prepared-statement-per-connection-size: 20
27     druid.filters: stat,wall
28     druid.use-global-data-source-stat: true
29     # Open mergeSql function through connectProperties property; slow SQL record
30     druid.connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

 

3. Create configuration class

 

 1 /**
 2  * @author jiz-a
 3  * @version 1.0
 4  * @date 2020/5/28 16:10
 5  */
 6 @Configuration
 7 public class DruidConfig {
 8     /**
 9      * Inject the self configured data source value in the configuration file into druid
10      *
11      * @return Database resources
12      */
13     @Bean
14     @ConfigurationProperties(prefix = "spring.datasource")
15     public DataSource druid() {
16         return new DruidDataSource();
17     }
18 
19     /**
20      * Configure Druid monitoring. If you do not configure this class, you will not be able to connect to Druid background.http://localhost:8080/druid/
21      * Configure a Servlet to manage the background
22      *
23      * @return Servlet
24      */
25     @Bean
26     public ServletRegistrationBean<StatViewServlet> statViewServlet() {
27         ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
28         Map<String, String> params = new HashMap<>(4);
29         //Set background login name and password
30         params.put("loginUsername", "admin");
31         params.put("loginPassword", "666");
32         //By default, all access and blacklist are allowed
33         params.put("allow", "");
34         // params.put("deny", "");
35         bean.setInitParameters(params);
36         return bean;
37     }
38 
39     /**
40      * Configure a web monitoring filter. If this class is not configured, the web module function on the page will not be enabled
41      *
42      * @return filter
43      */
44     @Bean
45     public FilterRegistrationBean<WebStatFilter> webStatFilter() {
46         FilterRegistrationBean<WebStatFilter> frb = new FilterRegistrationBean<>();
47         frb.setFilter(new WebStatFilter());
48         Map<String, String> params = new HashMap<>();
49         //Set not to block requests
50         params.put("exclusions", "*.js,*.css,/druid/*");
51         frb.setInitParameters(params);
52         //Intercept request
53         frb.setUrlPatterns(Arrays.asList("/*"));
54         return frb;
55     }
56 }

4. Start up and test;

http://localhost:8090/druid/login.html

 

 

 

At this point, spring boot integrates Druid.

 

 

Topics: MySQL Druid Spring SQL Database