Spring Boot使用Druid和监控配置
完美与Spring Boot集成。
1、编写Spring Boot Druid配置类
DruidDataSourceProperties.java
package org.paascloud.ops.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Properties;
/*** Created by meyer on 2017/1/15.*/
@ConfigurationProperties("spring.datasource.druid")
public class DruidDataSourceProperties {private Boolean testWhileIdle = true;private Boolean testOnBorrow;private String validationQuery = "SELECT 1";private Boolean useGlobalDataSourceStat;private String filters;private Long timeBetweenLogStatsMillis;private Integer maxSize;private Boolean clearFiltersEnable;private Boolean resetStatEnable;private Integer notFullTimeoutRetryCount;private Integer maxWaitThreadCount;private Boolean failFast;private Boolean phyTimeoutMillis;private Long minEvictableIdleTimeMillis = 300000L;private Long maxEvictableIdleTimeMillis;private Integer initialSize = 5;private Integer minIdle = 5;private Integer maxActive = 20;private Long maxWait = 60000L;private Long timeBetweenEvictionRunsMillis = 60000L;private Boolean poolPreparedStatements = true;private Integer maxPoolPreparedStatementPerConnectionSize = 20;private Properties connectionProperties = new Properties() {{put("druid.stat.mergeSql", "true");put("druid.stat.slowSqlMillis", "5000");}};public Boolean getTestWhileIdle() {return testWhileIdle;}public void setTestWhileIdle(Boolean testWhileIdle) {this.testWhileIdle = testWhileIdle;}public Boolean getTestOnBorrow() {return testOnBorrow;}public void setTestOnBorrow(Boolean testOnBorrow) {this.testOnBorrow = testOnBorrow;}public String getValidationQuery() {return validationQuery;}public void setValidationQuery(String validationQuery) {this.validationQuery = validationQuery;}public Boolean getUseGlobalDataSourceStat() {return useGlobalDataSourceStat;}public void setUseGlobalDataSourceStat(Boolean useGlobalDataSourceStat) {this.useGlobalDataSourceStat = useGlobalDataSourceStat;}public String getFilters() {return filters;}public void setFilters(String filters) {this.filters = filters;}public Long getTimeBetweenLogStatsMillis() {return timeBetweenLogStatsMillis;}public void setTimeBetweenLogStatsMillis(Long timeBetweenLogStatsMillis) {this.timeBetweenLogStatsMillis = timeBetweenLogStatsMillis;}public Integer getMaxSize() {return maxSize;}public void setMaxSize(Integer maxSize) {this.maxSize = maxSize;}public Boolean getClearFiltersEnable() {return clearFiltersEnable;}public void setClearFiltersEnable(Boolean clearFiltersEnable) {this.clearFiltersEnable = clearFiltersEnable;}public Boolean getResetStatEnable() {return resetStatEnable;}public void setResetStatEnable(Boolean resetStatEnable) {this.resetStatEnable = resetStatEnable;}public Integer getNotFullTimeoutRetryCount() {return notFullTimeoutRetryCount;}public void setNotFullTimeoutRetryCount(Integer notFullTimeoutRetryCount) {this.notFullTimeoutRetryCount = notFullTimeoutRetryCount;}public Integer getMaxWaitThreadCount() {return maxWaitThreadCount;}public void setMaxWaitThreadCount(Integer maxWaitThreadCount) {this.maxWaitThreadCount = maxWaitThreadCount;}public Boolean getFailFast() {return failFast;}public void setFailFast(Boolean failFast) {this.failFast = failFast;}public Boolean getPhyTimeoutMillis() {return phyTimeoutMillis;}public void setPhyTimeoutMillis(Boolean phyTimeoutMillis) {this.phyTimeoutMillis = phyTimeoutMillis;}public Long getMinEvictableIdleTimeMillis() {return minEvictableIdleTimeMillis;}public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;}public Long getMaxEvictableIdleTimeMillis() {return maxEvictableIdleTimeMillis;}public void setMaxEvictableIdleTimeMillis(Long maxEvictableIdleTimeMillis) {this.maxEvictableIdleTimeMillis = maxEvictableIdleTimeMillis;}public Integer getInitialSize() {return initialSize;}public void setInitialSize(Integer initialSize) {this.initialSize = initialSize;}public Integer getMinIdle() {return minIdle;}public void setMinIdle(Integer minIdle) {this.minIdle = minIdle;}public Integer getMaxActive() {return maxActive;}public void setMaxActive(Integer maxActive) {this.maxActive = maxActive;}public Long getMaxWait() {return maxWait;}public void setMaxWait(Long maxWait) {this.maxWait = maxWait;}public Long getTimeBetweenEvictionRunsMillis() {return timeBetweenEvictionRunsMillis;}public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) {this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;}public Boolean getPoolPreparedStatements() {return poolPreparedStatements;}public void setPoolPreparedStatements(Boolean poolPreparedStatements) {this.poolPreparedStatements = poolPreparedStatements;}public Integer getMaxPoolPreparedStatementPerConnectionSize() {return maxPoolPreparedStatementPerConnectionSize;}public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;}public Properties getConnectionProperties() {return connectionProperties;}public void setConnectionProperties(Properties connectionProperties) {this.connectionProperties = connectionProperties;}public Properties toProperties() {Properties properties = new Properties();notNullAdd(properties, "testWhileIdle", this.testWhileIdle);notNullAdd(properties, "testOnBorrow", this.testOnBorrow);notNullAdd(properties, "validationQuery", this.validationQuery);notNullAdd(properties, "useGlobalDataSourceStat", this.useGlobalDataSourceStat);notNullAdd(properties, "filters", this.filters);notNullAdd(properties, "timeBetweenLogStatsMillis", this.timeBetweenLogStatsMillis);notNullAdd(properties, "stat.sql.MaxSize", this.maxSize);notNullAdd(properties, "clearFiltersEnable", this.clearFiltersEnable);notNullAdd(properties, "resetStatEnable", this.resetStatEnable);notNullAdd(properties, "notFullTimeoutRetryCount", this.notFullTimeoutRetryCount);notNullAdd(properties, "maxWaitThreadCount", this.maxWaitThreadCount);notNullAdd(properties, "failFast", this.failFast);notNullAdd(properties, "phyTimeoutMillis", this.phyTimeoutMillis);notNullAdd(properties, "minEvictableIdleTimeMillis", this.minEvictableIdleTimeMillis);notNullAdd(properties, "maxEvictableIdleTimeMillis", this.maxEvictableIdleTimeMillis);notNullAdd(properties, "initialSize", this.initialSize);notNullAdd(properties, "minIdle", this.minIdle);notNullAdd(properties, "maxActive", this.maxActive);notNullAdd(properties, "maxWait", this.maxWait);notNullAdd(properties, "timeBetweenEvictionRunsMillis", this.timeBetweenEvictionRunsMillis);notNullAdd(properties, "poolPreparedStatements", this.poolPreparedStatements);notNullAdd(properties, "maxPoolPreparedStatementPerConnectionSize", this.maxPoolPreparedStatementPerConnectionSize);return properties;}private void notNullAdd(Properties properties, String key, Object value) {if (value != null) {properties.setProperty("druid." + key, value.toString());}}
}
编写Spring Boot自动配置类:DruidDataSourceconfig.java
package org.paascloud.ops.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*** Created by meyer on 2017/1/15.*/
@Configuration
@ConditionalOnClass({DruidDataSource.class})
@ConditionalOnProperty(name = {"spring.datasource.type"},havingValue = "com.alibaba.druid.pool.DruidDataSource",matchIfMissing = true
)
@EnableConfigurationProperties(DruidDataSourceProperties.class)
public class DruidDataSourceConfig {@Beanpublic DruidDataSource dataSource(DataSourceProperties dataSourceProperties, DruidDataSourceProperties druidDataSourceProperties) {DruidDataSource druidDataSource = (DruidDataSource) dataSourceProperties.initializeDataSourceBuilder().type(DruidDataSource.class).build();druidDataSource.configFromPropety(druidDataSourceProperties.toProperties());druidDataSource.setInitialSize(druidDataSourceProperties.getInitialSize());druidDataSource.setMinIdle(druidDataSourceProperties.getMinIdle());druidDataSource.setMaxActive(druidDataSourceProperties.getMaxActive());druidDataSource.setMaxWait(druidDataSourceProperties.getMaxWait());druidDataSource.setConnectProperties(druidDataSourceProperties.getConnectionProperties());return druidDataSource;}@Beanpublic ServletRegistrationBean druidStatViewServlet(DruidDataSourceProperties druidDataSourceProperties) {if (StringUtils.isEmpty(druidDataSourceProperties.getServletPath())) {druidDataSourceProperties.setServletPath("/druid/*");}return new ServletRegistrationBean(new StatViewServlet(), druidDataSourceProperties.getServletPath());}@Beanpublic FilterRegistrationBean druidWebStatFilter() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());//添加过滤规则.filterRegistrationBean.addUrlPatterns("/*");//添加不需要忽略的格式信息.filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");return filterRegistrationBean;}
}
触发Druid配置:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xxxxx:3306/xxxxxx
spring.datasource.username=xxxxxxxx
spring.datasource.password=xxxxxxxxxxxx
文章转载:Spring Cloud :http://www.60kb.com/post/71.html
Spring Boot使用Druid和监控配置相关推荐
- spring boot +spring data jpa +druid 多数据源配置
一.首先pom.xml新增如下依赖 <dependencies><dependency><groupId>org.springframework.boot</ ...
- Spring Boot下Druid连接池的使用配置分析
引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连接池,在连接池之外,还提供了非常优秀的监控功能,这里讲解如何与Spring Boot实现集成. 1. ...
- Spring Boot 集成 Druid 监控数据源
关注"Java后端技术全栈" 回复"面试"获取全套大厂面试资料 Druid 介绍 Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和 ...
- druid监控页面_Spring boot学习(四)Spring boot整合Druid
前言 在上一篇博客中我们介绍了Spring boot配置Mybatis,但是并没有配置连接池,这在实际开发过程中肯定是不切实际的,多次的数据库连接会给程序和数据库都带来没必要的负担,这一篇博客我将介绍 ...
- spring boot整合druid以及druid监控
Druid是Java语言中最好的数据库连接池(我个人知识范围内),并且能够提供强大的监控和扩展功能. 下面来说明如何在 spring Boot 中配置使用druid 1:添加druid和spring ...
- 德鲁伊(Druid)后台监控配置详细操作。生产环境定位问题方法
前言 从写System.out.println("hello world!")开始,到现在我一直认为,一个项目的系统上线,完成CURD(增删改查)的代码远远不够.为便于项目快速定位 ...
- Spring Boot 使用 Druid 连接池详解
Spring Boot 使用 Druid 连接池详解 Alibaba Druid 是一个 JDBC 组件库,包含数据库连接池.SQL Parser 等组件,被大量业务和技术产品使用或集成,经历过严苛的 ...
- Spring Boot之程序性能监控
转载自 Spring Boot之程序性能监控 Spring Boot特别适合团队构建各种可快速迭代的微服务,同时为了减少程序本身监控系统的开发量,Spring Boot提供了actuator模块,可以 ...
- Spring Boot集成Druid异常discard long time none received connection.
Spring Boot集成Druid异常 在Spring Boot集成Druid项目中,发现错误日志中频繁的出现如下错误信息: discard long time none received conn ...
最新文章
- 2019数据安装勾选_宝象课堂丨如何正确安装SOLIDWORKS 2019?
- 真实临床“生态”下实效性研究的挑战和意义
- express模板引擎jade与ejs
- 华农java实验7_国家实验教学示范中心
- 作者:季统凯,男,博士,中国科学院云计算中心主任、研究员,国云科技股份有限公司董事长,中国云计算专家委员会委员。...
- 4-2MapReduce的运行流程
- debian9.8无法切换中文输入法
- 索引体积_米家温湿度计体积虽小,耗电不小,如果经常离线,换颗电池吧
- OpenGL基础38:数据存储
- 深入了解人工智能机器人的应用领域有哪些?
- 深入理解Linux网络技术内幕学习笔记第二章:一些重要的数据结构
- ToStringBuilder.reflectionToString用法
- 简单理解:类目、SPU、SKU
- 如何用计算机接收光纤网络电视,家里只有一根网络电缆. 电脑和电视如何共享互联网?如何在机顶盒和路由器之间建立连接?...
- 【OpenFOAM】——OpenFOAM入门算例学习
- Unity | 基础逻辑
- VS2019 无法登录 许可证已过期 无法下载许可证
- EasyRecovery15数据恢复注意事项及主要功能介绍
- ACM-ICPC 2018 南京赛区网络预赛 E.AC Challenge 状压dp
- ftp mac上传文件到服务器,mac ftp 如何上传文件到服务器