Preface
I haven't updated the Spring Boot series articles for a long time. Do you think you are busy? It is also possible that the focus of some time ago may be on other aspects. Recently, the project needs to develop a mini program, which is just using Spring Boot to implement a back-end service. The relevant code cases will be shared later so that everyone will not be confused when doing mini program back-end services.
Under Spring Boot, several available connection pools (dbcp, dbcp2, tomcat, hikari) are provided by default. Of course, Druid does not support Druid. Druid comes from an open source connection pool of Alibaba. It provides very excellent monitoring functions. Let me share with you how to integrate with Spring Boot.
Version environment
Spring Boot 1.5.2.RELEASE, Druid 1.1.6, JDK1.7
System integration
Add pom.xml dependency:
<!-- Jpa --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MySql --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency><!-- druid --><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version></dependency>
Configure application.properties:
#Data source spring.datasource.url=jdbc:mysql://192.168.1.66:3306/spring_boot?characterEncoding=utf-8&useSSL=falsspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.DruidDataSource# Initialize size, minimum, maximum spring.datasource.initialSize=1spring.datasource.minIdle=3spring.datasource.maxActive=20# Configure the time to get the connection waiting timeout spring.datasource.maxWait=60000# Configure how long it takes to detect once, detect the idle connection that needs to be closed, in milliseconds spring.datasource.timeBetweenEvictionRunsMillis=60000# Configure the minimum time for a connection to survive in the pool, in milliseconds spring.datasource.minEvictableIdleTimeMillis=30000spring.datasource.validationQuery=select 'x'spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falspring.datasource.testOnReturn=false# Open PSCache and specify the size of PSCache on each connection spring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20# Configure filters for monitoring statistics intercepted. After removing it, the monitoring interface SQL cannot be counted. 'wall' is used for firewall spring.datasource.filters=stat,wall,slf4j# Open mergeSql function through the connectProperties property; slow SQL records spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
Configure yml file (choose one of the two above)
spring: datasource: url: jdbc:mysql://192.168.1.66:3306/spring-boot?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver platform: mysql type: com.alibaba.druid.pool.DruidDataSource # The following is a supplementary setting for the connection pool, which is applied to all data sources above# Initialize size, minimum, maximum initialSize: 1 minIdle: 3 maxActive: 20 # Configure the time to get the connection waiting timeout maxWait: 60000 # Configure how long it takes to perform a detection interval to detect idle connections that need to be closed, in millisecond timeBetweenEvictionRunsMillis: 60000 # Configure the minimum time to survive in the pool minEvictableIdleTimeMillis: 30000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false # Open PSCache and specify the size of PSCache on each connection pool poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # Configure filters for monitoring statistics intercepting. After removing the monitoring interface SQL, the monitoring interface cannot be counted. 'wall' is used for firewall filters: stat,wall,slf4j # Open mergeSql function through the connectProperties property; slow SQL record connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
Configure Druid's monitoring and statistics function
import java.sql.SQLException;import javax.sql.DataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.context.properties.ConfigurationProperties;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.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;/** * Alibaba Database Connection Pool Druid Configuration* Creator* Created by 7* Created on March 15, 2018*/@Configurationpublic class DruidConfiguration { private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class); private static final String DB_PREFIX = "spring.datasource"; @Bean public ServletRegistrationBean druidServlet() { logger.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP whitelist (no configuration or empty, all access is allowed) servletRegistrationBean.addInitParameter("allow", ""); // IP blacklist (deny takes precedence over allow when coexisting) //servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //Console management user servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "admin"); //Is it possible to reset the data and disable the "Reset on the HTML page All" function servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } @ConfigurationProperties(prefix = DB_PREFIX) class IDataSourceProperties { private String url; private String username; private String password; private String driverClassName; private int initialSize; private int minIdle; private int maxActive; private int maxWait; private int timeBetweenEvictionRunsMillis; private int minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String connectionProperties; @Bean public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(url); 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) { System.err.println("druid configuration initialization filter: " + e); } datasource.setConnectionProperties(connectionProperties); return datasource; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public int getInitialSize() { return initialSize; } public void setInitialSize(int initialSize) { this.initialSize = initialSize; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxActive() { return maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public int getMaxWait() { return maxWait; } public void setMaxWait(int maxWait) { this.maxWait = maxWait; } public int getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public int getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public boolean isTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public boolean isTestOnReturn() { return testOnReturn; } public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } public boolean isPoolPreparedStatements() { return poolPreparedStatements; } public void setPoolPreparedStatements(boolean poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public int getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public String getFilters() { return filters; } public void setFilters(String filters) { this.filters = filters; } public String getConnectionProperties() { return connectionProperties; } public void setConnectionProperties(String connectionProperties) { this.connectionProperties = connectionProperties; } }}Start the application, access address: http://localhost:8080/druid/, enter the configured account password and log in, and you can view the data source and SQL statistics and other monitoring. The renderings are as follows:
Of course, Alibaba also provides Druid's SpringBoot integrated version (druid-spring-boot-starter), you can refer to the following link.
refer to:
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
https://github.com/alibaba/druid/wiki
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.