SpringBoot使用Druid数据源的配置方法

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

一、依赖

为了测试,使用jdbcTemplate

<!-- jdbcTemplate -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.26</version>
</dependency>
<!-- mysql connector -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

二、Druid配置

druid.properties
#数据库设置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/uu_core?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
#--------------------------
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=1
spring.datasource.maxActive=50
# 配置获取连接等待超时的时间
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.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=false
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true

三、实例化Druid Datasource

package cn.aduu.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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.PropertySource;
import javax.sql.DataSource;
/**
 * @author zh
 * @ClassName cn.aduu.config.DruidConfiguration
 * @Description
 */
@Configuration
@PropertySource(value = "classpath:druid.properties")
public class DruidConfiguration {
  @Bean(destroyMethod = "close", initMethod = "init")
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource druidDataSource() {
    DruidDataSource druidDataSource = new DruidDataSource();
    return druidDataSource;
  }
  /**
   * 注册一个StatViewServlet
   * @return
   */
  @Bean
  public ServletRegistrationBean druidStatViewServlet(){
    //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
    //添加初始化参数:initParams
    //白名单:
    servletRegistrationBean.addInitParameter("allow","127.0.0.1");
    //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
    servletRegistrationBean.addInitParameter("deny","192.168.1.73");
    //登录查看信息的账号密码.
    servletRegistrationBean.addInitParameter("loginUsername","admin");
    servletRegistrationBean.addInitParameter("loginPassword","123456");
    //是否能够重置数据.
    servletRegistrationBean.addInitParameter("resetEnable","false");
    return servletRegistrationBean;
  }
  /**
   * 注册一个:filterRegistrationBean
   * @return
   */
  @Bean
  public FilterRegistrationBean druidStatFilter(){
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
    //添加过滤规则.
    filterRegistrationBean.addUrlPatterns("/*");
    //添加不需要忽略的格式信息.
    filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
  }
}

四、监控

访问 http://localhost:8080/druid, 使用上面配置的账号密码。

五、测试

@RestController
public class HelloController{
  private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
  @Autowired
  private JdbcTemplate jdbcTemplate;
  @RequestMapping("hello")
  public List<Map<String, Object>> hello() {
    List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT user,password FROM mysql.user ", new Object[]{});
    return list;
  }
}

访问 localhost:8080/hello

[
  {
    "user": "root",
    "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B"
  },
  {
    "user": "root",
    "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B"
  }
]

总结

以上所述是小编给大家介绍的SpringBoot使用Druid数据源,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • 详解Spring Boot下Druid连接池的使用配置分析

    引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连接池,在连接池之外,还提供了非常优秀的监控功能,这里讲解如何与Spring Boot实现集成. 1.  环境描述 spring Boot 1.4.0.RELEASE,  JDK 1.8 2.   Druid介绍 Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可管理的数据

  • SpringBoot入门之集成Druid的方法示例

    Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置. 主要参考:https://github.com/alibaba/druid/wiki/ 常见问题https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter 一.引入依赖 这里看其他博客都是引用的Druid,由于是使用springboot集成,这里参考druid官方文档,用的是druid-spring-boot-starte

  • springboot 动态数据源的实现方法(Mybatis+Druid)

    Spring多数据源实现的方式大概有2中,一种是新建多个MapperScan扫描不同包,另外一种则是通过继承AbstractRoutingDataSource实现动态路由.今天作者主要基于后者做的实现,且方式1的实现比较简单这里不做过多探讨. 实现方式 方式1的实现(核心代码): @Configuration @MapperScan(basePackages = "com.goofly.test1", sqlSessionTemplateRef = "test1SqlSess

  • 通过springboot+mybatis+druid配置动态数据源

    一.建数据库和表 1.数据库demo1放一张user表 SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NU

  • SpringBoot开发案例之配置Druid数据库连接池的示例

    前言 好久没有更新Spring Boot系列文章,你说忙么?也可能是,前段时间的关注点也许在其他方面了,最近项目中需要开发小程序,正好采用Spring Boot实现一个后端服务,后面会把相关的代码案例分享出来,不至于大家做小程序后端服务的时候一头雾水. 在Spring Boot下默认提供了若干种可用的连接池(dbcp,dbcp2, tomcat, hikari),当然并不支持Druid,Druid来自于阿里系的一个开源连接池,它提供了非常优秀的监控功能,下面跟大家分享一下如何与Spring Bo

  • Spring Boot使用Druid连接池的示例代码

    Druid是Java语言中最好的数据库连接池.Druid相比于其他的数据库连接池,有两大特性: 监控数据库,有利于分析线上数据库问题 更容易扩展,同时也很高效. 今天演示一下Spring Boot集成Druid. 实战 1.添加Maven依赖. Spring Boot版本使用的是1.x的,2.x的版本druid starter还不支持.不过自定义也是没问题的. <!--starter-web 方便我们查看效果--> <dependency> <groupId>org.s

  • Spring Boot 自定义数据源DruidDataSource代码

    这篇文章主要介绍了Spring Boot 自定义数据源DruidDataSource代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.添加依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </depend

  • Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了,这里就不过多说明了.重点是讲述在多数据源下的如何配置使用Druid和PageHelper . Druid介绍和使用 在使用Druid之前,先来简单的了解下Druid. Druid是一个数据库连接池.Druid可以说是目前最好的数据库连接池!因其优秀的功能.性能和扩展性方面,深受开发人员的青睐. D

  • SpringBoot使用Druid数据源的配置方法

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池(据说是目前最好的连接池) 一.依赖 为了测试,使用jdbcTemplate <!-- jdbcTemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifa

  • SpringBoot整合Druid数据源的方法实现

    目录 1.在创建SpringBoot项目的时候,在pom.xml maven中添加依赖: 2.在 application.yml(或aproperties)中添加相应的配置: 3. log4j.properties 配置文件: 4.在运行测试方法,查看数据源 5.运行测试方法 SprintBoot 默认使用的是 HikariDataSource数据源,这次整合一个第三方的数据源 Druid ,它是阿里开发的一款开源的数据源,被很多人认为是Java语言中最好的数据库连接池,因为 Druid 能够提

  • springboot集成druid连接池配置的方法

    在开发项目中如果数据库选型为mysql,很大概率下连接池会使用druid 这里介绍springboot集成durid springboot : 2.1.9 druid : 1.1.10 案例地址 github地址 springboot集成druid配置 需要引入的pom <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactI

  • Spring Boot使用Druid和监控配置方法

    Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource Druid是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能. 下面来说明如何在 Spring Boot 中配置使用Druid (1)添加Maven依赖 (或jar包)\ <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId&g

  • SpringBoot整合Druid数据源过程详解

    这篇文章主要介绍了SpringBoot整合Druid数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.数据库结构 2.项目结构 3.pom.xml文件 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</ar

  • SpringBoot整合Druid数据库连接池的方法

    一,Druid是什么? Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. 二, 在哪里下载druid maven中央仓库: http://central.maven.org/maven2/com/alibaba/druid/ 三, 怎么获取Druid的源码 Druid是一个开源项目,源码托管在github上,源代码仓库地址是 https://github.com/alibaba/druid.同时每次Druid发布正式版本和快照的时候,都会把源码打包,你可以从

  • SpringBoot环境Druid数据源使用及特点

    1.springboot默认的数据源是: org.apache.tomcat.jdbc.pool.DataSource 2.简单的Druid介绍: Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池 Druid 是目前比较流行的高性能的,分布式列存储的OLAP框架(具体来说是MOLAP).它有如下几个特点: 2-1:亚秒级查询: druid提

  • SpringBoot搭建多数据源的实现方法

    首先我们建立两个数据库(可以不在同一台电脑上): multiple_order: DROP DATABASE IF EXISTS `multiple_order`; CREATE DATABASE `multiple_order`; USE `multiple_order`; CREATE TABLE `order` ( `order_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '订单id', `user_id` BIGINT U

  • 在SpringBoot中添加Redis及配置方法

    在实际的开发中,会有这样的场景.有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长. 此时就可以考虑在项目中加入缓存. 引入依赖 在maven项目中引入如下依赖.并且需要在本地安装redis. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifac

  • springboot数据库密码加密的配置方法

    前言 由于系统安全的考虑,配置文件中不能出现明文密码的问题,本文就给大家详细介绍下springboot配置数据库密码加密的方法,下面话不多说了,来一起看看详细的介绍吧 1.导入依赖 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.2</

随机推荐