Spring|Spring Boot 整合 Durid 数据库连接池
1.Druid简介
Druid是一个JDBC组件库,包括数据库连接池、SQL Parser等组件。DruidDataSource是最好的数据库连接池
2.maven引入druid
com.alibaba
druid
1.1.9
mysql
mysql-connector-java
3. application.properties 具体的配置文件详解 可以参照DruidDataSource配置属性列表
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#dataSource Pool configuration
spring.datasource.initialSize=5
#最小连接池数量
spring.datasource.minIdle=5
#最大连接池数量
spring.datasource.maxActive=20
#获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下#降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.druid.exceptionSorter=true
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.druid.filters=stat,wall
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 慢SQL
spring.datasource.connectionProperties=druid.stat.mergeSql=true;
druid.stat.slowSqlMillis=500
spring.datasource.useGlobalDataSourceStat=true
4.配置 Durid dataSource
/**
* dataSource 数据源配置
*/
@Configuration
@Data
public class DruidDataSourceConfiguration {@Value("${spring.datasource.druid.filters}")
private String filters ;
@Value("${spring.datasource.druid.exceptionSorter}")
private String exceptionSorter;
/**
* @see org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat 仿写的你可以去了解
* @return DruidDataSource
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();
try {
System.out.println(filters);
dataSource.setFilters(filters);
dataSource.setExceptionSorter(exceptionSorter);
} catch (SQLException e) {
e.printStackTrace();
}
return dataSource;
}
}
5. 配置DruidStatFilter Druid的监控统计功能是通过filter-chain扩展实现,如果你要打开监控统计功能,配置StatFilter
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
打开监控统计功能
*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="https://www.it610.com/article/*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源
}
)
public class DruidStatFilter extends WebStatFilter {
}
6. 配置DruidStatViewServlet 【Spring|Spring Boot 整合 Durid 数据库连接池】启用Druid的内置监控页面
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* @author hu
* Created by hu on 2018/2/27.
*/
//urlPatterns指定监控页面url
@WebServlet(urlPatterns="/druid/*",
initParams={
@WebInitParam(name="allow",value="https://www.it610.com/article/192.168.33.108,127.0.0.1"),// IP白名单(没有配置或者为空,则允许所有访问)
@WebInitParam(name="deny",value="https://www.it610.com/article/192.168.1.73"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="https://www.it610.com/article/admin"),// 用户名
@WebInitParam(name="loginPassword",value="https://www.it610.com/article/123456"),// 密码
@WebInitParam(name="resetEnable",value="https://www.it610.com/article/true")// 禁用HTML页面上的“Reset All”功能
}
)
public class DruidStatViewServlet extends StatViewServlet {
}
7. 添加@ServletComponentScan注解 需要在spring boot 启动类上面 添加@ServletComponentScan,否则监控页面的servlet 不会被加载。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
* @author hu
* @date 2018/5/9 17:25
*/
@ServletComponentScan
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.dream.repository")
public class DreamApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(DreamApplication.class, args);
}
}
8. 访问监控页面
- http://localhost:5000/druid/index.html
- DruidDataSource配置属性列表
- spring boot Starter配置方式
- Durid 常见问题
- 自定义存储
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- Spring集成|Spring集成 Mina
- springboot使用redis缓存
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了
- Spring|Spring Boot之ImportSelector