Druid连接池

简介

由阿里巴巴开源的druid连接池是目前综合实力最突出的数据库连接池,而且还提供了监控日志功能,能够分析SQL执行情况。

引入druid连接池

pom.xml中加入

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid</artifactId>
  4. <version>1.1.21</version>
  5. </dependency>

application.properties中加入

  1. # druid
  2. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  3. # 连接池初始化大小,最小,最大
  4. spring.datasource.initialSize=10
  5. spring.datasource.minIdle=10
  6. spring.datasource.maxActive=30
  7. # 连接等待超时时间
  8. spring.datasource.maxWait=60000
  9. # 多久检测需要关闭的空闲连接
  10. spring.datasource.timeBetweenEvictionRunsMillis=60000
  11. # 一个连接在池中最小生存的时间
  12. spring.datasource.minEvictableIdleTimeMillis=300000
  13. # 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用
  14. spring.datasource.validationQuery=SELECT 'x'
  15. spring.datasource.testWhileIdle=true
  16. spring.datasource.testOnBorrow=false
  17. spring.datasource.testOnReturn=false
  18. # 打开PSCache,并且指定每个连接上PSCache的大小
  19. spring.datasource.poolPreparedStatements=true
  20. spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
  21. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,wall用于防火墙
  22. spring.datasource.filters=stat,wall,log4j
  23. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  24. spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  25. # 合并多个DruidDataSource的监控数据
  26. spring.datasource.useGlobalDataSourceStat=true

注:之前已经配置好了mysql,mybatis,这里没有重复写了

代码实战

新增了一个DruidFilter.java来配置内置的监控



DruidFilter.java

  1. package com.example.config;
  2. import com.alibaba.druid.support.http.StatViewServlet;
  3. import com.alibaba.druid.support.http.WebStatFilter;
  4. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  5. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. public class DruidFilter {
  10. @Bean
  11. public ServletRegistrationBean druidStatView() {
  12. //指定路径进入内置监控页面
  13. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  14. //IP白名单:
  15. servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
  16. //IP黑名单
  17. servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
  18. //登录查看信息的账号密码.
  19. //servletRegistrationBean.addInitParameter("loginUsername", "admin");
  20. //servletRegistrationBean.addInitParameter("loginPassword", "123456");
  21. //是否能够重置数据.
  22. servletRegistrationBean.addInitParameter("resetEnable", "true");
  23. return servletRegistrationBean;
  24. }
  25. @Bean
  26. public FilterRegistrationBean druidWebStatFilter() {
  27. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
  28. //添加过滤规则.
  29. filterRegistrationBean.addUrlPatterns("/*");
  30. //添加不需要忽略的格式信息.
  31. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  32. return filterRegistrationBean;
  33. }
  34. }

注:我之前就已经配置好了spring security,如果在和其他教程一样配置druid的账号密码,会导致输入druid的账号密码后无法跳转进入内置监控页面,我知道有很多方法可以避免,但那都需要额外增加代码来判断绕过,既然spring security已经有了账号权限,何必额外非得给druid单独增加一套账号权限,把 /druid/* 路径配置在spring security中,公用已有的账号权限不更好嘛。所以我没有单独配置。

还有这只是常用的配置,如若需要进一步了解详细的配置

内置监控页面配置详情Web关联配置详情

效果图如下



恩恩,还自带阿里云的广告,果然是阿里的开源产品

SpringBoot学习(五)—— springboot快速整合Druid的更多相关文章

  1. SpringBoot第五集:整合Druid和MyBatis(2020最新最易懂)

    SpringBoot第五集:整合Druid和MyBatis(2020最新最易懂) 1.SpringBoot整合Druid Druid是阿里巴巴的一个开源项目,是一个数据库连接池的实现,结合了C3P0. ...

  2. SpringBoot学习(五)-->SpringBoot的核心

    SpringBoot的核心 1.入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的J ...

  3. SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂)

    SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂) 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏/高词汇.访问权限控制(URL级别 ...

  4. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  5. springboot 学习之路 8 (整合websocket(1))

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  6. SpringBoot学习(四)-->SpringBoot快速入门,开山篇

    Spring Boot简介 Spring Boot的目的在于创建和启动新的基于Spring框架的项目.Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合.大部分Spring ...

  7. springboot 学习之路 20 (整合RabbitMQ)

    整合RabbitMQ: 我的操作系统是window7 ,所以在整合ribbotMQ之前需要先安装rabbitMq服务:安装步骤请参考:window下安装RabbitMQ  这个详细介绍了安装步骤,请按 ...

  8. springboot 学习之路 14(整合mongodb的Api操作)

    springboot整合mongodb: mongodb的安装和权限配置  请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...

  9. springboot学习一:快速搭建springboot项目

    1.idea创建springboot工程 JDK选择1.8以上的版本 选择springboot的版本和添加配置项 新建一个HelloController,测试 访问 http://localhost: ...

随机推荐

  1. C++模板编程中只特化模板类的一个成员函数(花样特化一个成员函数)

    转自:https://www.cnblogs.com/zhoug2020/p/6581477.html 模板编程中如果要特化或偏特化(局部特化)一个类模板,需要特化该类模板的所有成员函数.类模板中大多 ...

  2. JS Array.apply会有内存泄漏问题

    报错内容: Maximum call stack size exceeded 参考:https://www.jianshu.com/p/b9ba0ddd3392 对象较多,前端JS内存溢出: 数组克隆 ...

  3. C#随机挑选某一个用户或者几个用户信息

    && u.EnabledMark == ).OrderBy(_=>Guid.NewGuid()).Take(); && u.EnabledMark == ).Or ...

  4. 006 认识BeanNameAware

    在看Spring的生命周期,就要注意到Spring Aware,Aware是什么,在这篇文章中,主要是说明三个部分,Spring的生命周期[这个在后面会继续说明,这里这是一个大纲],然后说明Sprin ...

  5. AI项目(CV方向)研发流程

  6. weui 可移动悬浮按钮

    @CHARSET "UTF-8"; /** 右下角跳转按钮 跳转到列表 */ #list_note_icon { position: fixed; bottom: 10%; rig ...

  7. sorry, unimplemented: non-trivial designated initializers not supported

    将C语言转换为C++代码时,发生如下错误 sorry, unimplemented: non-trivial designated initializers not supported. 查找原因,是 ...

  8. Linux记录-批量安装软件服务(转载)

    #!/bin/bash # 安装函数 install(){    for soft in $*    do         echo "$soft"安装中...         y ...

  9. python对 windows系统监控插件

    在python编程的windows系统监控中,需要监控监控硬件信息需要两个模块:WMI 和 pypiwin32 .

  10. div定位relative和absolute测试1

    div里的position定位也是比较常见的,relative是相对定位,absolute是绝对定位.如本文测试:body自带8px的margin,这里不对其进行清空.蓝色的div和红色的div分别设 ...