spring boot 集成 druid
写在前面
因为在用到spring boot + mybatis的项目时候,经常发生访问接口卡,服务器项目用了几天就很卡的甚至不能访问的情况,而我们的项目和数据库都是好了,考虑到可能是数据库连接的问题,所以我打算引入其他的数据池,引入数据池的时候找来找去,比较了当前两个最火的数据池,druid和HikariCP,比来比去选了阿里的druid,虽然spring boot默认不支持druid,而是支持HikariCP,而且HikariCP的性能更好,但是阿里功能多,界面友好方便,性价比更高
1 .maven配置
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.31</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>6.0.6</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>1.5.4.RELEASE</version>
- </dependency>
2.在application.yml中加入mybatis的配置,mysql配置
- ###datasource
- spring.datasource.url = jdbc:mysql://localhost:3306/eeee?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
- spring.datasource.username = root
- spring.datasource.password = 123
- spring.datasource.driverClassName =com.mysql.jdbc.Driver
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
- spring.datasource.initialSize=1
- spring.datasource.minIdle=3
- spring.datasource.maxActive=20
- spring.datasource.maxWait=60000
- spring.datasource.timeBetweenEvictionRunsMillis=60000
- spring.datasource.minEvictableIdleTimeMillis=30000
- spring.datasource.validationQuery=select 'x'
- spring.datasource.testWhileIdle=true
- spring.datasource.testOnBorrow=false
- spring.datasource.testOnReturn=false
- spring.datasource.poolPreparedStatements=true
- spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
- spring.datasource.filters=stat,wall,slf4j
- spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
- spring.datasource.useGlobalDataSourceStat=true
- ###mybatis
- mybatis.mapper-locations=classpath:mapper/com/jiuying/small/*/*Mapper.xml
- mybatis.config-location=classpath:config/sqlMapConfig.xml
3 druid 的配置文件
- package com.tools.config;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- 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 com.alibaba.druid.pool.DruidDataSource;
- import com.alibaba.druid.support.http.StatViewServlet;
- import com.alibaba.druid.support.http.WebStatFilter;
- /**
- * @author
- *
- * druid 数据连接池配置
- */
- @Configuration
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public class DruidConfiguration {
- private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
- private static final String DB_PREFIX = "spring.datasource";
- @Bean
- public ServletRegistrationBean druidServlet() {
- logger.info("init Druid Servlet Configuration ");
- ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
- "/druid/*");
- // IP白名单
- servletRegistrationBean.addInitParameter("allow", "192.168.2.25,127.0.0.1");
- // IP黑名单(共同存在时,deny优先于allow)
- servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
- // 控制台管理用户
- servletRegistrationBean.addInitParameter("loginUsername", "admin");
- servletRegistrationBean.addInitParameter("loginPassword", "9527");
- // 是否能够重置数据 禁用HTML页面上的“Reset All”功能
- servletRegistrationBean.addInitParameter("resetEnable", "false");
- return servletRegistrationBean;
- }
- @Bean
- public FilterRegistrationBean filterRegistrationBean() {
- FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
- filterRegistrationBean.addUrlPatterns("/*");
- filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
- return filterRegistrationBean;
- }
- // 解决 spring.datasource.filters=stat,wall,log4j 无法正常注册进去
- @ConfigurationProperties(prefix = DB_PREFIX)
- class IDataSourceProperties {
- private String url;
- private String username;
- private String password;
- private String driverClassName;
- private int initialSize;
- private int minIdle;
- private int maxActive;
- private int maxWait;
- private int timeBetweenEvictionRunsMillis;
- private int minEvictableIdleTimeMillis;
- private String validationQuery;
- private boolean testWhileIdle;
- private boolean testOnBorrow;
- private boolean testOnReturn;
- private boolean poolPreparedStatements;
- private int maxPoolPreparedStatementPerConnectionSize;
- private String filters;
- private String connectionProperties;
- @Bean // 声明其为Bean实例
- @Primary // 在同样的DataSource中,首先使用被标注的DataSource
- public DataSource dataSource() {
- DruidDataSource datasource = new DruidDataSource();
- datasource.setUrl(url);
- datasource.setUsername(username);
- datasource.setPassword(password);
- datasource.setDriverClassName(driverClassName);
- // configuration
- datasource.setInitialSize(initialSize);
- datasource.setMinIdle(minIdle);
- datasource.setMaxActive(maxActive);
- datasource.setMaxWait(maxWait);
- datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
- datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
- datasource.setValidationQuery(validationQuery);
- datasource.setTestWhileIdle(testWhileIdle);
- datasource.setTestOnBorrow(testOnBorrow);
- datasource.setTestOnReturn(testOnReturn);
- datasource.setPoolPreparedStatements(poolPreparedStatements);
- datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
- try {
- datasource.setFilters(filters);
- } catch (SQLException e) {
- System.err.println("druid configuration initialization filter: " + e);
- }
- datasource.setConnectionProperties(connectionProperties);
- return datasource;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getDriverClassName() {
- return driverClassName;
- }
- public void setDriverClassName(String driverClassName) {
- this.driverClassName = driverClassName;
- }
- public int getInitialSize() {
- return initialSize;
- }
- public void setInitialSize(int initialSize) {
- this.initialSize = initialSize;
- }
- public int getMinIdle() {
- return minIdle;
- }
- public void setMinIdle(int minIdle) {
- this.minIdle = minIdle;
- }
- public int getMaxActive() {
- return maxActive;
- }
- public void setMaxActive(int maxActive) {
- this.maxActive = maxActive;
- }
- public int getMaxWait() {
- return maxWait;
- }
- public void setMaxWait(int maxWait) {
- this.maxWait = maxWait;
- }
- public int getTimeBetweenEvictionRunsMillis() {
- return timeBetweenEvictionRunsMillis;
- }
- public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
- this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
- }
- public int getMinEvictableIdleTimeMillis() {
- return minEvictableIdleTimeMillis;
- }
- public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
- this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
- }
- public String getValidationQuery() {
- return validationQuery;
- }
- public void setValidationQuery(String validationQuery) {
- this.validationQuery = validationQuery;
- }
- public boolean isTestWhileIdle() {
- return testWhileIdle;
- }
- public void setTestWhileIdle(boolean testWhileIdle) {
- this.testWhileIdle = testWhileIdle;
- }
- public boolean isTestOnBorrow() {
- return testOnBorrow;
- }
- public void setTestOnBorrow(boolean testOnBorrow) {
- this.testOnBorrow = testOnBorrow;
- }
- public boolean isTestOnReturn() {
- return testOnReturn;
- }
- public void setTestOnReturn(boolean testOnReturn) {
- this.testOnReturn = testOnReturn;
- }
- public boolean isPoolPreparedStatements() {
- return poolPreparedStatements;
- }
- public void setPoolPreparedStatements(boolean poolPreparedStatements) {
- this.poolPreparedStatements = poolPreparedStatements;
- }
- public int getMaxPoolPreparedStatementPerConnectionSize() {
- return maxPoolPreparedStatementPerConnectionSize;
- }
- public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
- this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
- }
- public String getFilters() {
- return filters;
- }
- public void setFilters(String filters) {
- this.filters = filters;
- }
- public String getConnectionProperties() {
- return connectionProperties;
- }
- public void setConnectionProperties(String connectionProperties) {
- this.connectionProperties = connectionProperties;
- }
- }
- }
4.然后我们启动项目,访问localhost:8080/druid,输入admin ,9527(配置文件里面写的用户名和密码),就可以看得到我们的监控界面了
spring boot 集成 druid的更多相关文章
- Spring boot 集成 Druid 数据源
Druid是阿里开源的一个JDBC应用组件,其中包括三部分: DruidDriver:代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource:高效可管理 ...
- Spring Boot集成Druid数据库连接池
1. 前言 Druid数据库连接池由阿里巴巴开源,号称是java语言中最好的数据库连接池,是为监控而生的.Druid的官方地址是:https://github.com/alibaba/druid 通过 ...
- spring boot 集成Druid
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...
- Spring Boot [使用 Druid 数据库连接池]
导读 最近一段时间比较忙,以至于很久没有更新Spring Boot系列文章,恰好最近用到Druid, 就将Spring Boot 使用 Druid作为数据源做一个简单的介绍. Druid介绍: Dru ...
- Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件
上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...
- Quartz与Spring Boot集成使用
上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...
- Spring Boot下Druid连接池+mybatis
目前Spring Boot中默认支持的连接池有dbcp,dbcp2, hikari三种连接池. 引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连 ...
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...
随机推荐
- postman提取接口的返回值及动态设置变量(一)
一.提取接口返回值 1.当返回值是返回JSON时 let json = JSON.parse(responseBody); // responseBody是包含整个返回内容的字符串 let foo ...
- ASP.NET MVC学习(一)之路由篇Route
什么是路由 通过[路由]配置,路由可以规定URL的特殊格式,使其达到特殊效果. 在ASP.NET MVC框架中,通过路由配置URL,使用户的URL请求可以映射到Controller下的action方法 ...
- Sublime Text 2 绿色汉化版 x64
前天介绍了<Sublime Text 2 绿化与汉化 [Windows篇]>,应大家要求,我特地做了汉化版分享给大家. 很清新吧,但我没安装多余的插件,只是安装了几个必备的插件,如 输入法 ...
- javascript构造函数模块
var Person = (function(){ var Constr; Constr = function(){ this.name = 'carl'; } Constr.prototype = ...
- js深复制
一般来讲深复制主要是为了复制js对象中的引用类型,引用类型在普通的赋值操作下相当于是引用,修改复制对象也会影响原对象,简单的方法的话可以使用JSON.parse(JSON.stringify(obj) ...
- python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__
__getattr__: 属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...
- http和socket之长连接和短连接区别【转】
转自:https://blog.csdn.net/mengyafei43/article/details/25195445 TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层 ...
- git 入门常用命令(转)
Git工作流程:D:\projects\Setup2\Setup2\Setup2\Express\SingleImage\DiskImages\DISK1 git clone工作开始之初,可通过git ...
- 使用maven命令终端构建一个web项目及发布该项目
构建环境: maven版本:3.3.9 系统平台:Windows7 x64 JDK版本:1.7 构建步骤: 1.打开maven安装目录,在地址栏输入cmd进入命令窗口 2.输入命令mvn archet ...
- tar命令的实用详解(C参数和排除文件 --exclude)
一.tar:从压缩包中解压出指定文件 [root@d176 test]# tar ztf nrpe-2.12.tar.gz |grep srcnrpe-2.12/src/nrpe-2.12/src/. ...