Spring Boot集成Druid数据库连接池
1. 前言
Druid数据库连接池由阿里巴巴开源,号称是java语言中最好的数据库连接池,是为监控而生的。Druid的官方地址是:https://github.com/alibaba/druid
通过本文,我们可以看到
- Spring Boot 如何配置数据源
- Spring Boot 如何集成Druid数据库连接池
- 如何打开并访问Druid数据库连接池的监控功能
- Spring Boot 使用JdbcTemplate操作数据库
2. 配置pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.20</version> </dependency> </dependencies>
3. 在application.yml中配置数据源
mybatis: type-aliases-package: com.haige.entity # 起别名 mapper-locations: /com/haige/repository/mapper/*.xml # mapper映射文件的位置 server: port: 8080 spring: application: name: haige datasource: haige: # 数据库访问配置,mysql。 # 主数据源,默认的 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test username: root password: root # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 initialSize: 5 minIdle: 5 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false #exceptionSorter: false testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 useGlobalDataSourceStat: true # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 #spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 #spring.datasource.useGlobalDataSourceStat=true pagehelper: auto-dialect: "true" reasonable: "true" support-methods-arguments: "true" params: "count=countSql" logging: config: classpath:log4j2.yml
4. 打开Druid的监控统计功能
Druid的监控统计功能是通过filter-chain扩展实现,如果你要打开监控统计功能,需要配置StatFilter,相关代码如下。
@Configuration public class DruidConfiguration { private static final Logger log = LoggerFactory.getLogger(DruidConfiguration.class); @Bean public ServletRegistrationBean druidServlet() { log.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("loginUsername", "admin");// 用户名 initParameters.put("loginPassword", "admin");// 密码 initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能 initParameters.put("allow", ""); // IP白名单 (没有配置或者为空,则允许所有访问) //initParameters.put("deny", "192.168.20.38");// IP黑名单 (存在共同时,deny优先于allow) servletRegistrationBean.setInitParameters(initParameters); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
注意:在spring-webflux项目中会出现
ServletRegistrationBean空指针,即servlet注册不进去,原因是webflux中没有引入servlet的包,解决方案如下,在启动类注册进去
@SpringBootApplication public class HaigeApplication { public static void main(String[] args) { SpringApplication.run(HaigeApplication.class, args); } @Bean public RequestContextListener requestContextListener(){ return new RequestContextListener(); } }
等应用启动后,可以访问地址:http://localhost:8080/druid/,用户名和密码见上述代码中的设置,即admin/admin。
作者:诗和远方
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.