Spring Boot :Druid Monitor
Posted 爱吃醋的兔子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot :Druid Monitor相关的知识,希望对你有一定的参考价值。
忙里偷个闲,在这里分享一下SpringBoot集成Druid实现数据库监控功能,有什么错误欢迎大家指出!
参考文件:
Spring实现Druid监控:https://www.cnblogs.com/wanghuijie/p/druid_monitor.html
DruidDataSource源码分析:https://www.cnblogs.com/wanghuijie/p/druiddatasource.html
保存Druid监控日志:https://www.cnblogs.com/wanghuijie/p/druid_stat_log.html
言归正传,看看是怎么实现的吧。
一:引入Druid依赖
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>druid</artifactId> 4 <version>1.0.31</version> 5 </dependency>
二:在yml中添加druid配置
1 spring: 2 datasource: 3 # 配置多数据源时使用 4 main: 5 name: AA 6 driver-class-name: com.mysql.jdbc.Driver 7 # 对应自己的数据库连接 8 url: jdbc:mysql:// 9 username: xxx 10 password: xxx 11 # Druid连接池的配置信息 Begin ################################################## 12 type: com.alibaba.druid.pool.DruidDataSource 13 #初始化连接大小 14 initialSize: 5 15 #最小连接池数量 16 minIdle: 5 17 #最大连接池数量 18 maxActive: 20 19 maxIdle: 20 20 # 配置获取连接等待超时的时间 21 maxWait: 60000 22 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 23 timeBetweenEvictionRunsMillis: 60000 24 # 配置一个连接在池中最小生存的时间,单位是毫秒 25 minEvictableIdleTimeMillis: 300000 26 # 测试连接 27 validationQuery: SELECT 1 FROM DUAL 28 # 申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性 29 testWhileIdle: true 30 # 获取连接时执行检测,建议关闭,影响性能 31 testOnBorrow: false 32 # 归还连接时执行检测,建议关闭,影响性能 33 testOnReturn: false 34 # 是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭 35 poolPreparedStatements: false 36 # 开启poolPreparedStatements后生效 37 maxPoolPreparedStatementPerConnectionSize: 20 38 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,\'wall\'用于防火墙 39 filters: stat,wall,log4j 40 logSlowSql: true 41 # Druid连接池的配置信息 End ######################################################
三: 使用java代码读取配置
1 package com.bjgoodwill.oip.hosp.practice.druid; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import com.alibaba.druid.support.http.StatViewServlet; 5 import com.alibaba.druid.support.http.WebStatFilter; 6 import org.apache.commons.lang3.StringUtils; 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.boot.web.servlet.FilterRegistrationBean; 9 import org.springframework.boot.web.servlet.ServletRegistrationBean; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.context.annotation.Primary; 13 14 import javax.sql.DataSource; 15 import java.sql.SQLException; 16 17 @Configuration 18 public class DruidSourceConfig { 19 20 // 有多数据源时可调为多数据源的配置,如@Value("${spring.datasource.main.name}") 21 @Value("${spring.datasource.name}") 22 private String mainName; 23 24 @Value("${spring.datasource.url}") 25 private String mainUrl; 26 27 @Value("${spring.datasource.username}") 28 private String mainUsername; 29 30 @Value("${spring.datasource.password}") 31 private String mainPassword; 32 33 @Value("${spring.datasource.driver-class-name}") 34 private String mainDriverClassName; 35 36 @Value("${spring.datasource.initialSize}") 37 private String mainInitialSize; 38 39 @Value("${spring.datasource.minIdle}") 40 private String mainMinIdle; 41 42 @Value("${spring.datasource.maxActive}") 43 private String mainMaxActive; 44 45 @Value("${spring.datasource.maxWait}") 46 private String mainMaxWait; 47 48 @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") 49 private String mainTimeBetweenEvictionRunsMillis; 50 51 @Value("${spring.datasource.minEvictableIdleTimeMillis}") 52 private String mainMinEvictableIdleTimeMillis; 53 54 @Value("${spring.datasource.validationQuery}") 55 private String mainValidationQuery; 56 57 @Value("${spring.datasource.filters}") 58 private String mainFilters; 59 60 @Value("{spring.datasource.logSlowSql}") 61 private String mainLogSlowSql; 62 63 @Value("${spring.datasource.type}") 64 private String mainType; 65 66 @Value("{spring.datasource.maxIdle}") 67 private String mainMaxIdle; 68 69 @Bean 70 @Primary 71 public DataSource mainDataSource() { 72 DruidDataSource datasource = new DruidDataSource(); 73 74 datasource.setUrl(mainUrl); 75 datasource.setUsername(mainUsername); 76 datasource.setPassword(mainPassword); 77 datasource.setDriverClassName(mainDriverClassName); 78 79 //configuration 80 if (StringUtils.isNotBlank(mainInitialSize)) { 81 datasource.setInitialSize(Integer.parseInt(mainInitialSize)); 82 } 83 if (StringUtils.isNotBlank(mainMinIdle)) { 84 datasource.setMinIdle(Integer.parseInt(mainMinIdle)); 85 } 86 if (StringUtils.isNotBlank(mainMaxActive)) { 87 datasource.setMaxActive(Integer.parseInt(mainMaxActive)); 88 } 89 if (StringUtils.isNotBlank(mainMaxWait)) { 90 datasource.setMaxWait(Integer.parseInt(mainMaxWait)); 91 } 92 if (StringUtils.isNotBlank(mainTimeBetweenEvictionRunsMillis)) { 93 datasource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(mainTimeBetweenEvictionRunsMillis)); 94 } 95 if (StringUtils.isNotBlank(mainMinEvictableIdleTimeMillis)) { 96 datasource.setMinEvictableIdleTimeMillis(Integer.parseInt(mainMinEvictableIdleTimeMillis)); 97 } 98 99 datasource.setValidationQuery(mainValidationQuery); 100 datasource.setTestWhileIdle(true); 101 datasource.setTestOnBorrow(false); 102 datasource.setTestOnReturn(false); 103 try { 104 datasource.setFilters(mainFilters); 105 } catch (SQLException e) { 106 e.printStackTrace(); 107 } 108 return datasource; 109 } 110 111 @Bean 112 public ServletRegistrationBean statViewServlet(){ 113 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); 114 //设置ip白名单 115 servletRegistrationBean.addInitParameter("allow",""); 116 //设置ip黑名单,优先级高于白名单 117 servletRegistrationBean.addInitParameter("deny",""); 118 //设置控制台管理用户 119 servletRegistrationBean.addInitParameter("loginUsername","root"); 120 servletRegistrationBean.addInitParameter("loginPassword","root"); 121 //是否可以重置数据 122 servletRegistrationBean.addInitParameter("resetEnable","false"); 123 return servletRegistrationBean; 124 } 125 126 @Bean 127 public FilterRegistrationBean statFilter(){ 128 //创建过滤器 129 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); 130 //设置过滤器过滤路径 131 filterRegistrationBean.addUrlPatterns("/*"); 132 //忽略过滤的形式 133 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); 134 return filterRegistrationBean; 135 }
四: 执行效果
以上是关于Spring Boot :Druid Monitor的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 1.5.4 整合 druid(十三)
spring boot 自定义druid数据源,如何注入配置
spring boot 自定义druid数据源,如何注入配置