写在前面

  因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能是数据库连接的问题,所以我打算引入其他的数据池,引入数据池的时候找来找去,比较了当前两个最火的数据池,druid和HikariCP,比来比去选了阿里的druid,虽然spring boot默认不支持druid,而是支持HikariCP,而且HikariCP的性能更好,但是阿里功能多,界面友好方便,性价比更高

1 .maven配置

  1. <dependency>
  2.   <groupId>com.alibaba</groupId>
  3.   <artifactId>druid</artifactId>
  4.   <version>1.0.31</version>
  5. </dependency>
  6. <dependency>
  7.   <groupId>org.mybatis.spring.boot</groupId>
  8.   <artifactId>mybatis-spring-boot-starter</artifactId>
  9.   <version>1.3.0</version>
  10. </dependency>
  11. <dependency>
  12.   <groupId>mysql</groupId>
  13.   <artifactId>mysql-connector-java</artifactId>
  14.   <version>6.0.6</version>
  15. </dependency>
  16. <dependency>
  17.   <groupId>org.springframework.boot</groupId>
  18.   <artifactId>spring-boot-starter-web</artifactId>
  19.   <version>1.5.4.RELEASE</version>
  20. </dependency>

2.在application.yml中加入mybatis的配置,mysql配置

  1. ###datasource
  2. spring.datasource.url = jdbc:mysql://localhost:3306/eeee?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
  3. spring.datasource.username = root
  4. spring.datasource.password = 123
  5. spring.datasource.driverClassName =com.mysql.jdbc.Driver
  6.  
  7. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  8. spring.datasource.initialSize=1
  9. spring.datasource.minIdle=3
  10. spring.datasource.maxActive=20
  11. spring.datasource.maxWait=60000
  12. spring.datasource.timeBetweenEvictionRunsMillis=60000
  13. spring.datasource.minEvictableIdleTimeMillis=30000
  14. spring.datasource.validationQuery=select 'x'
  15. spring.datasource.testWhileIdle=true
  16. spring.datasource.testOnBorrow=false
  17. spring.datasource.testOnReturn=false
  18. spring.datasource.poolPreparedStatements=true
  19. spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
  20. spring.datasource.filters=stat,wall,slf4j
  21. spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  22. spring.datasource.useGlobalDataSourceStat=true
  23.  
  24. ###mybatis
  25. mybatis.mapper-locations=classpath:mapper/com/jiuying/small/*/*Mapper.xml
  26. mybatis.config-location=classpath:config/sqlMapConfig.xml

3 druid 的配置文件

  1. package com.tools.config;
  2.  
  3. import java.sql.SQLException;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.boot.context.properties.ConfigurationProperties;
  10. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  11. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.context.annotation.Primary;
  15.  
  16. import com.alibaba.druid.pool.DruidDataSource;
  17. import com.alibaba.druid.support.http.StatViewServlet;
  18. import com.alibaba.druid.support.http.WebStatFilter;
  19.  
  20. /**
  21. * @author
  22. *
  23. * druid 数据连接池配置
  24. */
  25. @Configuration
  26. @SuppressWarnings({ "rawtypes", "unchecked" })
  27. public class DruidConfiguration {
  28.  
  29. private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
  30. private static final String DB_PREFIX = "spring.datasource";
  31.  
  32. @Bean
  33. public ServletRegistrationBean druidServlet() {
  34. logger.info("init Druid Servlet Configuration ");
  35. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
  36. "/druid/*");
  37. // IP白名单
  38. servletRegistrationBean.addInitParameter("allow", "192.168.2.25,127.0.0.1");
  39. // IP黑名单(共同存在时,deny优先于allow)
  40. servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
  41. // 控制台管理用户
  42. servletRegistrationBean.addInitParameter("loginUsername", "admin");
  43. servletRegistrationBean.addInitParameter("loginPassword", "9527");
  44. // 是否能够重置数据 禁用HTML页面上的“Reset All”功能
  45. servletRegistrationBean.addInitParameter("resetEnable", "false");
  46. return servletRegistrationBean;
  47. }
  48.  
  49. @Bean
  50. public FilterRegistrationBean filterRegistrationBean() {
  51. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
  52. filterRegistrationBean.addUrlPatterns("/*");
  53. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  54. return filterRegistrationBean;
  55. }
  56.  
  57. // 解决 spring.datasource.filters=stat,wall,log4j 无法正常注册进去
  58. @ConfigurationProperties(prefix = DB_PREFIX)
  59. class IDataSourceProperties {
  60. private String url;
  61. private String username;
  62. private String password;
  63. private String driverClassName;
  64. private int initialSize;
  65. private int minIdle;
  66. private int maxActive;
  67. private int maxWait;
  68. private int timeBetweenEvictionRunsMillis;
  69. private int minEvictableIdleTimeMillis;
  70. private String validationQuery;
  71. private boolean testWhileIdle;
  72. private boolean testOnBorrow;
  73. private boolean testOnReturn;
  74. private boolean poolPreparedStatements;
  75. private int maxPoolPreparedStatementPerConnectionSize;
  76. private String filters;
  77. private String connectionProperties;
  78.  
  79. @Bean // 声明其为Bean实例
  80. @Primary // 在同样的DataSource中,首先使用被标注的DataSource
  81. public DataSource dataSource() {
  82. DruidDataSource datasource = new DruidDataSource();
  83. datasource.setUrl(url);
  84. datasource.setUsername(username);
  85. datasource.setPassword(password);
  86. datasource.setDriverClassName(driverClassName);
  87.  
  88. // configuration
  89. datasource.setInitialSize(initialSize);
  90. datasource.setMinIdle(minIdle);
  91. datasource.setMaxActive(maxActive);
  92. datasource.setMaxWait(maxWait);
  93. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  94. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  95. datasource.setValidationQuery(validationQuery);
  96. datasource.setTestWhileIdle(testWhileIdle);
  97. datasource.setTestOnBorrow(testOnBorrow);
  98. datasource.setTestOnReturn(testOnReturn);
  99. datasource.setPoolPreparedStatements(poolPreparedStatements);
  100. datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  101. try {
  102. datasource.setFilters(filters);
  103. } catch (SQLException e) {
  104. System.err.println("druid configuration initialization filter: " + e);
  105. }
  106. datasource.setConnectionProperties(connectionProperties);
  107. return datasource;
  108. }
  109.  
  110. public String getUrl() {
  111. return url;
  112. }
  113.  
  114. public void setUrl(String url) {
  115. this.url = url;
  116. }
  117.  
  118. public String getUsername() {
  119. return username;
  120. }
  121.  
  122. public void setUsername(String username) {
  123. this.username = username;
  124. }
  125.  
  126. public String getPassword() {
  127. return password;
  128. }
  129.  
  130. public void setPassword(String password) {
  131. this.password = password;
  132. }
  133.  
  134. public String getDriverClassName() {
  135. return driverClassName;
  136. }
  137.  
  138. public void setDriverClassName(String driverClassName) {
  139. this.driverClassName = driverClassName;
  140. }
  141.  
  142. public int getInitialSize() {
  143. return initialSize;
  144. }
  145.  
  146. public void setInitialSize(int initialSize) {
  147. this.initialSize = initialSize;
  148. }
  149.  
  150. public int getMinIdle() {
  151. return minIdle;
  152. }
  153.  
  154. public void setMinIdle(int minIdle) {
  155. this.minIdle = minIdle;
  156. }
  157.  
  158. public int getMaxActive() {
  159. return maxActive;
  160. }
  161.  
  162. public void setMaxActive(int maxActive) {
  163. this.maxActive = maxActive;
  164. }
  165.  
  166. public int getMaxWait() {
  167. return maxWait;
  168. }
  169.  
  170. public void setMaxWait(int maxWait) {
  171. this.maxWait = maxWait;
  172. }
  173.  
  174. public int getTimeBetweenEvictionRunsMillis() {
  175. return timeBetweenEvictionRunsMillis;
  176. }
  177.  
  178. public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
  179. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  180. }
  181.  
  182. public int getMinEvictableIdleTimeMillis() {
  183. return minEvictableIdleTimeMillis;
  184. }
  185.  
  186. public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
  187. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  188. }
  189.  
  190. public String getValidationQuery() {
  191. return validationQuery;
  192. }
  193.  
  194. public void setValidationQuery(String validationQuery) {
  195. this.validationQuery = validationQuery;
  196. }
  197.  
  198. public boolean isTestWhileIdle() {
  199. return testWhileIdle;
  200. }
  201.  
  202. public void setTestWhileIdle(boolean testWhileIdle) {
  203. this.testWhileIdle = testWhileIdle;
  204. }
  205.  
  206. public boolean isTestOnBorrow() {
  207. return testOnBorrow;
  208. }
  209.  
  210. public void setTestOnBorrow(boolean testOnBorrow) {
  211. this.testOnBorrow = testOnBorrow;
  212. }
  213.  
  214. public boolean isTestOnReturn() {
  215. return testOnReturn;
  216. }
  217.  
  218. public void setTestOnReturn(boolean testOnReturn) {
  219. this.testOnReturn = testOnReturn;
  220. }
  221.  
  222. public boolean isPoolPreparedStatements() {
  223. return poolPreparedStatements;
  224. }
  225.  
  226. public void setPoolPreparedStatements(boolean poolPreparedStatements) {
  227. this.poolPreparedStatements = poolPreparedStatements;
  228. }
  229.  
  230. public int getMaxPoolPreparedStatementPerConnectionSize() {
  231. return maxPoolPreparedStatementPerConnectionSize;
  232. }
  233.  
  234. public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
  235. this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
  236. }
  237.  
  238. public String getFilters() {
  239. return filters;
  240. }
  241.  
  242. public void setFilters(String filters) {
  243. this.filters = filters;
  244. }
  245.  
  246. public String getConnectionProperties() {
  247. return connectionProperties;
  248. }
  249.  
  250. public void setConnectionProperties(String connectionProperties) {
  251. this.connectionProperties = connectionProperties;
  252. }
  253. }
  254. }

4.然后我们启动项目,访问localhost:8080/druid,输入admin ,9527(配置文件里面写的用户名和密码),就可以看得到我们的监控界面了

spring boot 集成 druid的更多相关文章

  1. Spring boot 集成 Druid 数据源

    Druid是阿里开源的一个JDBC应用组件,其中包括三部分: DruidDriver:代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource:高效可管理 ...

  2. Spring Boot集成Druid数据库连接池

    1. 前言 Druid数据库连接池由阿里巴巴开源,号称是java语言中最好的数据库连接池,是为监控而生的.Druid的官方地址是:https://github.com/alibaba/druid 通过 ...

  3. spring boot 集成Druid

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...

  4. Spring Boot [使用 Druid 数据库连接池]

    导读 最近一段时间比较忙,以至于很久没有更新Spring Boot系列文章,恰好最近用到Druid, 就将Spring Boot 使用 Druid作为数据源做一个简单的介绍. Druid介绍: Dru ...

  5. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  6. Quartz与Spring Boot集成使用

    上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...

  7. Spring Boot下Druid连接池+mybatis

      目前Spring Boot中默认支持的连接池有dbcp,dbcp2, hikari三种连接池.  引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连 ...

  8. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  9. (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

    在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...

随机推荐

  1. postman提取接口的返回值及动态设置变量(一)

    一.提取接口返回值   1.当返回值是返回JSON时 let json = JSON.parse(responseBody); // responseBody是包含整个返回内容的字符串 let foo ...

  2. ASP.NET MVC学习(一)之路由篇Route

    什么是路由 通过[路由]配置,路由可以规定URL的特殊格式,使其达到特殊效果. 在ASP.NET MVC框架中,通过路由配置URL,使用户的URL请求可以映射到Controller下的action方法 ...

  3. Sublime Text 2 绿色汉化版 x64

    前天介绍了<Sublime Text 2 绿化与汉化 [Windows篇]>,应大家要求,我特地做了汉化版分享给大家. 很清新吧,但我没安装多余的插件,只是安装了几个必备的插件,如 输入法 ...

  4. javascript构造函数模块

    var Person = (function(){ var Constr; Constr = function(){ this.name = 'carl'; } Constr.prototype = ...

  5. js深复制

    一般来讲深复制主要是为了复制js对象中的引用类型,引用类型在普通的赋值操作下相当于是引用,修改复制对象也会影响原对象,简单的方法的话可以使用JSON.parse(JSON.stringify(obj) ...

  6. python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__

    __getattr__:     属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...

  7. http和socket之长连接和短连接区别【转】

    转自:https://blog.csdn.net/mengyafei43/article/details/25195445 TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层 ...

  8. git 入门常用命令(转)

    Git工作流程:D:\projects\Setup2\Setup2\Setup2\Express\SingleImage\DiskImages\DISK1 git clone工作开始之初,可通过git ...

  9. 使用maven命令终端构建一个web项目及发布该项目

    构建环境: maven版本:3.3.9 系统平台:Windows7 x64 JDK版本:1.7 构建步骤: 1.打开maven安装目录,在地址栏输入cmd进入命令窗口 2.输入命令mvn archet ...

  10. tar命令的实用详解(C参数和排除文件 --exclude)

    一.tar:从压缩包中解压出指定文件 [root@d176 test]# tar ztf nrpe-2.12.tar.gz |grep srcnrpe-2.12/src/nrpe-2.12/src/. ...