In this chapter, we will replace the default database connection pool with the Druid connection pool. If you have any questions, please contact me at [email protected]. Ask for directions of various gods, thank you
Introduce Druid dependencies
Open the pom.xml mentioned in the previous article → Build your own SpringBoot backend framework from scratch (I)
Find the
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>Copy the code
1.0.29
Then right-click Maven→Reimport to download the dependency
After the download succeeds,
1.0.29
Configure the data source connection pool information
Add the following configuration to the application.properties file
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
Connection pool configuration
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
Connection wait timeout
spring.datasource.maxWait=60000
Configure how often to check (detect idle connections that can be closed)
spring.datasource.timeBetweenEvictionRunsMillis=60000
Set the minimum lifetime of the connection in the pool
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
Turn on PSCache and specify the size of PSCache on each connection
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# Configure the filters for monitoring statistics interception. After removing the filters, the MONITORING interface SQL cannot be counted. 'wall' is used for the firewall
spring.datasource.filters=stat,wall,slf4j
Enable mergeSql via connectProperties; Slow SQL record
spring.datasource.connectionProperties=druid.stat.mergeSql=true; druid.stat.slowSqlMillis=5000Copy the code
Three: Add a configuration file
Under the core – configurer create DruidDataSourceConfigurer. Java and DruidMonitorConfigurer. Java
As follows:
DruidDataSourceConfigurer
package com.example.demo.core.configurer; import com.alibaba.druid.pool.DruidDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; import java.sql.SQLException; /** * @author * @description: * @time 2018/4/18 15:16 */ @Configuration public class DruidDataSourceConfigurer { private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfigurer.class); @Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;
@Bean
public DataSource getDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
returndatasource; }}Copy the code
DruidMonitorConfigurer
package com.example.demo.core.configurer; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; /** * @author Zhang Yao@description :Druid monitor Configuration * @time 2018/4/18 15:30 */ @configuration public class DruidMonitorConfigurer {/** * register ServletRegistrationBean * @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); /** Whitelist bean.addinitParameter ()"allow"."127.0.0.1"); // Multiple IP addresses are separated by commas (,). //IP blacklist (deny takes precedence over allow) : // Bean.addinitParameter () :Sorry, you are not permitted to view this page."deny"."192.168.1.110"); // Password for logging in to view information. Bean.addinitparameter ("loginUsername"."admin");
bean.addInitParameter("loginPassword"."123456"); // Whether data can be reset. bean.addinitParameter ("resetEnable"."false");
returnbean; } /** * Register FilterRegistrationBean * @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter()); // Add filter rules. Bean.addurlpatterns ("/ *"); // Add format information that does not need to be ignored. Bean.addinitparameter ("exclusions"."*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
returnbean; }}Copy the code
Four: Start the server and access the data connection pool management page
Open a browser, type http://localhost:8080/druid in the following pages, for the configuration successfully
The account is admin, and the password is 123456
You can view data sources, SQL monitoring, URL monitoring and more
The project address
Code cloud address: gitee.com/beany/mySpr…
GitHub address: github.com/MyBeany/myS…
Writing articles is not easy, if it is helpful to you, please help click star
At the end
Springboot integration with Druid has been completed, and subsequent features will be updated in the future. If you have any questions, please contact me at [email protected]. Ask for directions from various gods, thank you.