springboot 配置DRUID数据源的方法实例分析
本文实例讲述了springboot 配置DRUID数据源的方法。分享给大家供大家参考,具体如下:
druid 是阿里开源的数据库连接池。
开发时整合 druid 数据源过程。
1.修改pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
增加上面的配置。
2.编辑配置数据源代码
package com.neo.conf;
import javax.sql.DataSource;
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 com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
@ConfigurationProperties(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,192.168.31.77");
//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;
}
}
3.编写配置文件 druid.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:
spring.datasource.username=root
spring.datasource.password=root
#--------------------------
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=50
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1
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
spring.datasource.filters=stat
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
4.启动程序查看效果
更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
同类资源
- 基于Kubernetes与Helm的配置管理
基于Kubernetes与Helm的配置管理例子源代码,kubeadm与AWS的Kops等Kubernetes集群部署工具的出现,极大的简化...
- 窗口配置一句代码实现保存与读取
易语言窗口配置一句代码实现保存与读取例子源代码,窗口上有很多组件而且需要用到对软件的操作结果保存,再下...
- 文件读写文件操作类模块,支持配置文件操作API实现
文件操作类模块,支持配置文件操作API实现例子源代码,本模块属于以前封装自用的模块,开源给同样有需要的人。...
- 图形按钮一键配置
图形按钮一键配置例子源代码,这样就不用一个按钮做3个图片了。...
- Sigar文档及配置
Sigar文档及配置本文件感兴趣的可以参考一下,一个收集系统各项底层信息的工具集。...
- Exchange2010安装及详细配置
Exchange2010安装及详细配置本文件感兴趣的可以参考一下,非常详细,本人是exchange2010方面的菜鸟。...
- Java基于springboot教务管理系统
Java基于springboot教务管理系统本文件感兴趣的可以参考一下,帮助学校管理教务系统,用一个帐号解决学校教务...
- 一键生成redis集群配置文件
易语言一键生成redis集群配置文件例子源代码,只调用了模块window版本的redis集群搭建。...
- springBootNoMaven实现定时任务功能
springBootNoMaven本文件感兴趣的可以参考一下,不使用maven环境,创建springboot项目,实现了定时任务功能简单...
- DZ制作的网络验证,配合dz配置插件
易语言DZ制作的网络验证,配合dz配置插件例子源代码。...
- 环境变量配置工具,实时生效无需重启电脑
环境变量配置工具,实时生效无需重启电脑本文件感兴趣的可以参考一下,近期玩python,环境变量配置那稍显麻烦,配...
- 自定义快捷短语复制,输出,搜索,及读写配置的工具
易语言自定义快捷短语复制,输出,搜索,及读写配置的工具例子源代码,可以自定义输入一些常用语,移动鼠标快速复制...