Druid SpringBoot series of integrated source monitoring configuration data
Following previous blog JDBC data access SpringBoot series later, this blog will introduce the use of database connection pooling framework Druid
Test environment preparation:
- Maven
- IntelliJ IDEA
Springboot Initializer first create a project, details refer SpringBoot series quickly create Initializer project , must pay attention to the introduction of JDBC, web-dependent and so on, because the Druid did not provide a default, so go https://mvnrepository.com/artifact/com.alibaba/ druid configuration information, after the project is created, pom configuration should have the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency>
Application.yml a new configuration file, add the following configuration: type: com.alibaba.druid.pool.DruidDataSource must be specified
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf8&useSSL=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
initialization-mode: always
type: com.alibaba.druid.pool.DruidDataSource
# 连接池设置
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 90000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
# Oracle请使用select 1 from dual
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
use-global-data-source-stat: true
Write a junit test class for testing:
package com.example.springboot.jdbc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class SppringbootJdbcApplicationTests {
@Autowired
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
After commissioning, the property did not take effect because Springboot class does not automatically configure the following attributes, so the idea was to identify all colors shown below:
So let the onset of these configurations, you need to write a configuration class, attribute by ConfigurationProperties mapping
@Configuration
public class DriudConfig {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new DruidDataSource();
}
}
Start again junit debugging, found the onset of attributes, ok, test by
providing a platform for monitoring for the data source, and so Druid, it needs to be configured to work properly
package com.example.springboot.jdbc.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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 org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* <pre>
* Druid配置类
* </pre>
*
* @author nicky
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2019年12月15日 修改内容:
* </pre>
*/
@Configuration
public class DriudConfig {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new DruidDataSource();
}
/**
* 注册ServletRegistrationBean
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
/** 初始化参数配置,initParams**/
//白名单
bean.addInitParameter("allow", "127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
bean.addInitParameter("deny", "192.168.1.73");
//登录查看信息的账号密码.
bean.addInitParameter("loginUsername", "admin");
bean.addInitParameter("loginPassword", "admin");
//是否能够重置数据.
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 注册FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
bean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return bean;
}
}
ok, access http://127.0.0.1:8080/, enter the code in the configuration of the account password to log in to access, I set up the account password is admin, enter the account password sign-on platform, query the data source, sql monitoring, etc. Features
Download code examples: GitHub download link