SpringBoot整合Druid

Druid是个十分强大的后端管理工具,具体的功能和用途请问阿里爸爸

1. 在pom.xml中导入包

<!-- alibaba 的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>

2. 配置Druid

  • 首先在application.yml中配置druid
spring:
datasource:
password: xxx
# 如果存在多个数据源,监控的时候可以通过名字来区分开来
name: mysql
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
# 驱动名字
driver-class-name: com.mysql.cj.jdbc.Driver
# 连接数据库的url
url: jdbc:mysql://localhost:3306/xxx?serverTimezone=Asia/Shanghai&allowMultiQueries=true
# 连接数据库的账号
username: xxx
# 连接数据库的密码
# 扩展插件
# 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
filters: stat, wall
# 最大连接池数量
maxActive: 20
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
initialSize: 5
# 获取连接时最大等待时间,单位毫秒
maxWait: 60000
# 最小连接池数量
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
# 连接保持空闲而不被驱逐的最长时间
minEvictableIdleTimeMillis: 300000
# 用来检测连接是否有效的sql,要求是一个查询语句
# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用
validationQuery: select count(1) from 'table'
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
testWhileIdle: true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnBorrow: false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn: false
# 是否缓存preparedStatement,即PSCache
poolPreparedStatements: false
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true
maxOpenPreparedStatements: -1
  • 其次编写Druid配置类
package xxx.xxx.xxx.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
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 javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map; /**
* 配置 Druid
*
* @author axiang
*/
@Configuration
public class DruidConfig { /* 设置Druid配置的默认前缀为 spring.datasource */
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid() {
return new DruidDataSource();
} /* 自定义配置Druid管理界面的访问账户名 */
@Value("${xxx.config.druid.name}")
private String name; /* 自定义配置Druid管理界面的访问密码 */
@Value("${xxx.config.druid.pwd}")
private String pwd; /**
* 配置Druid的监控
* 配置一个管理后台的servlet
*
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>(10);
//账号
initParams.put("loginUsername", name);
//密码
initParams.put("loginPassword", pwd);
//默认允许所有
initParams.put("allow", "");
//不允许的黑名单ip
initParams.put("deny", "");
bean.setInitParameters(initParams);
return bean;
} /**
* 配置一个监控的filter
*
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(10);
initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams);
bean.setUrlPatterns(Collections.singletonList("/*")); return bean;
}
}

3. 访问和测试

输入下面的IP,进入页面访问Druid内容

http://[IP地址]:[端口号]/[项目部署名]/druid/index.html

【SpringBoot | Druid】SpringBoot整合Druid的更多相关文章

  1. SpringBoot学习之整合Druid的简单应用

    一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...

  2. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  3. springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置

    一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...

  4. springBoot(13)---整合Druid实现多数据源和可视化监控

    SpringBoot整合Druid实现多数据源和可视化监控 先献上github代码地址:https://github.com/yudiandemingzi/springboot-manydatasou ...

  5. Springboot整合druid

    目录 Springboot整合druid application.yml DruidConfig 数据监控地址:http://localhost:8080/druid Springboot整合drui ...

  6. SpringBoot整合Druid数据连接池

    SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ...

  7. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  8. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  9. SpringBoot:整合Druid、MyBatis

    目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...

随机推荐

  1. Python斐波那契数列

    今天偶然看到这个题目,闲着没事练一下手 if __name__ == '__main__': """ 斐波那契数列(Fibonacci sequence), 又称黄金分割 ...

  2. 达孚电子(NDF)参加2019年印度电子元器件展圆满成功

    2019年9月27日-29日,达孚电子(NDF)参加2019年印度国际电子元器件及生产设备展览会在印度国际展览中心举得圆满成功,为期三天的展会中,打造了一场电子元器件行业交流的饕餮盛宴. 本次展会取得 ...

  3. Centos7 安装redis 并新建springboot工程使用Redis 做session

    Redis 安装 就是解压运行,根据自己的爱好,放到文件夹中 tar -zxvf redis-5.0.4.tar.gz yum install gcc cd redis-5.0.4 make MALL ...

  4. 【Go】高效截取字符串的一些思考

    原文链接:https://blog.thinkeridea.com/201910/go/efficient_string_truncation.html 最近我在 Go Forum 中发现了 [SOL ...

  5. 用FILE*指针对象读文件的方式。

      先直接上代码: ]; ]; char* ptr1; ]; FILE* fh; QString strpath = getenv("GCDIR"); QString str_in ...

  6. 整理了适合新手的20个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...

  7. 基于SEER的区块链版赛亚麻将游戏Pre alpha版本内测啦!

    游戏基于SEER测试网络文体平台模块(Culture and Sports Platform,CSP),正在进行数据调试等工作,大家可以尝鲜体验. 此游戏账户和资金等核心系统完全基于区块链,但目前运行 ...

  8. Redis(十五)Redis 的一些常用技术(Spring 环境下)

    一.Redis 事务与锁机制 1.Redis的基础事务 在Redis中开启事务的命令是 multi 命令, 而执行事务的命令是 exec 命令.multi 到 exec 命令之间的 Redis 命令将 ...

  9. 列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 以及join()在python中的用法简介

    列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 使用字符串链接的四种方法都可以创建 字符串拼接一共有四种方法,也可以应用到列表的拼接中 ...

  10. SYZOJ中文安装指南

    Made By:Spaceskynet Thanks to other developers. 测试系统 Ubuntu-17.04 PS(全局变量): [syzoj2 path] = 您git的syz ...