preface

In the last blog we introduced the Spring Boot configuration of Mybatis, but did not configure the connection pool, which is certainly unrealistic in the actual development process, multiple database connections will bring unnecessary burden to the program and database, this blog I will introduce the best in Java at present, Druid is the most popular database connection pool.

First published on personal blog: [www.xiongfrblog.cn]

Druid is introduced

Druid is an open source project from Alibaba that bills itself as a database connection pool for monitoring, Druid outperforms other connection pools such as DBCP, C3P0, BoneCP, Proxool, and JBoss DataSource in terms of functionality, performance, and scalability. Druid has already been deployed in Over 600 applications in Alibhabha.

Spring Boot Configures Druid

So without further ado, let’s start configuring Druid in our project. In this blog, you can configure Druid on the basis of Spring Boot’s integration with Mybatis.

Add the dependent

Add the following dependencies to our project’s POM.xml file:

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

The important thing to note here (why so many people get it wrong) is thatDruidThere is another dependency as follows:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.3</version>
</dependency>
Copy the code

The Druid dependency can be configured in a different way than the Druid dependency. The Druid dependency can be configured in a different way than the Druid dependency. The Druid dependency can be configured in a different way.

Add the configuration

I read online tutorials that are very vague, they just come up with a bunch of configurations, and they don’t tell you what a configuration is. It’s nice for people who like to copy and paste, but it’s really bad for us to understand, so I’m going to try to make it a little bit clearer.

  1. Start in the project configuration fileapplication.propertiesIf the following information is added to the file, yes is displayedDruidThe connection pool:
Druid connection pool is used
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
Copy the code
  1. Configure the data source information (already mentioned in integrating Mybatis, again for consistency)
Configure the location of the entity class
mybatis.type-aliases-package=com.web.springbootmybatis.entity
# XML file location
mybatis.mapper-locations=classpath:mapper/*.xml

Mysql database connection information
# mysql driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# Database connection informationspring.datasource.url=jdbc:mysql://localhost:3306/eran? useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
Database user name
spring.datasource.username=root
# database password
spring.datasource.password=root
Copy the code
  1. Then configure the connection pool properties. You can flexibly configure these parameters as required:
Number of physical connections established during initialization.
spring.datasource.druid.initial-size=5
# Maximum number of connection pools
spring.datasource.druid.max-active=20
Minimum number of connection pools
spring.datasource.druid.min-idle=5
Maximum wait time to get a connection in milliseconds
spring.datasource.druid.max-wait=3000
PreparedStatement PSCache improves the performance of databases that support cursor functions, such as Oracle, and is recommended to be disabled in mysql.
spring.datasource.druid.pool-prepared-statements=false
To enable PSCache, the configuration must be greater than 0. When greater than 0, poolPreparedStatements automatically triggers a change to true. In Druid, there is no problem with Oracle PSCache taking up too much memory. You can set this value to a higher value, such as 100
spring.datasource.druid.max-open-prepared-statements= -1
Configure the detection of idle connection intervals that can be closed
spring.datasource.druid.time-between-eviction-runs-millis=60000
Set the minimum lifetime of the connection in the pool
spring.datasource.druid.min-evictable-idle-time-millis= 300000
spring.datasource.druid.max-evictable-idle-time-millis= 400000
Copy the code
  1. Configure an alias for an extension plug-in. Common plug-ins include:

    The alias meaning
    stat Monitor the statistical
    log4j The log
    wall Defending against SQL Injection

    Stat and wall are separated by commas as follows:

    Stat to monitor statistics, and wall to prevent SQL injection
    spring.datasource.druid.filters= stat,wall
    #Spring monitors AOP pointcuts, such as x.y.z.severvice.*, with multiple English comma separated configurations
    spring.datasource.druid.aop-patterns= com.web.springbootdruid.service.*
    Copy the code

Configuration is important here. For example, if we don’t configure stat, we won’t get the information we want from Druid’s monitoring page.

  1. Up here we turn onstatMonitoring statistics plug-in, monitoring configuration below:
# Whether to enable StatFilter The default value is true
spring.datasource.druid.web-stat-filter.enabled= true
# Add filter rules
spring.datasource.druid.web-stat-filter.url-pattern=/*
Ignore the filter format
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
Copy the code
  1. StatViewServlet configuration: Druid provides a StatViewServlet to display Druid statistics. StatViewServlet can be used to:
  • Provides an HTML page for displaying monitoring information
  • Provides a JSON API for monitoring information

The configuration is as follows. Note the user name and password:

The default value is true
spring.datasource.druid.stat-view-servlet.enabled= true
If the access path is /druid, jump to StatViewServlet
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
Whether the data can be reset
spring.datasource.druid.stat-view-servlet.reset-enable=false
You need a password to access the console. Default is root
spring.datasource.druid.stat-view-servlet.login-username=druid
spring.datasource.druid.stat-view-servlet.login-password=druid
# IP white listSpring. The datasource. The druid. Stat - view - servlet. Allow = 127.0.0.1#  IP blacklist (deny takes precedence over allow when both exist)
spring.datasource.druid.stat-view-servlet.deny=
Copy the code

If you want to know more about Druid, you can go to the official documentation.

Verify the Druid

Localhost :8080/druid / 127.0.0.1:8080/druid / 127.0.0.1:8080/druid

Go to the Druid login page, enter the username and password we configured in the configuration file to log in, and go to the following page:

You can see some basic configuration information on the home page. Click the data source in the menu bar to view our data source configuration information and connection pool configuration information:

Click SQL Monitor to view SQL execution information:

Mysql > query localhost:8080/user; mysql > query localhost:8080/user;

Druid is a very powerful tool that you can use if you have successfully configured it.

conclusion

Spring Boot integration with Druid is done. I hope it will help you. See you in the next blog post. bye~