SpringBoot整合druid数据源及添加Druid监控页面

举报
西魏陶渊明 发表于 2022/09/25 05:59:47 2022/09/25
【摘要】 不是不会,只是没见过,代码只是一种工具,首先要会用,应用中使用druid连接池,并添加监控 1.首先引入druid坐标 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId&...

不是不会,只是没见过,代码只是一种工具,首先要会用,应用中使用druid连接池,并添加监控

  • 1.首先引入druid坐标

  
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid</artifactId>
  4. <version>1.0.11</version>
  5. </dependency>
  • 2.添加druid配置参数

参考:

数据库连接池优化配置(druid,dbcp,c3p0)

参数 默认值 解释
initialSize 3 初始化配置
minIdle 3 最小连接数
maxActive 15 最大连接数
maxWait 5000 获取连接超时时间(单位:ms)
timeBetweenEvictionRunsMillis 90000 连接有效性检测时间(单位:ms)
testOnBorrow false 获取连接检测
testOnReturn false 归还连接检测
minEvictableIdleTimeMillis 1800000 最大空闲时间(单位ms)
testWhileIdle true 在获取连接后,确定是否要进行连接空间时间的检查
  • 配置说明:

1:minEvictableIdleTimeMillis(最大空闲时间):默认为30分钟,配置里面不进行设置。

2:testOnBorrow ,testOnReturn 默认为关闭,可以设置为不配置。

3:testWhileIdle(在获取连接后,确定是否要进行连接空闲时间的检查)。默认为true。配置里面不再进行设置。

  • 流程说明:

1:在第一次调用connection的时候,才会进行 initialSize的初始化。

2:心跳检测时间线程,会休眠timeBetweenEvictionRunsMillis时间,然后只对(没有borrow的线程 减去 minIdle)的线程进行检查,如果空闲时间大于minEvictableIdleTimeMillis则进行close。

3:testWhileIdle必须设置为true,在获取到连接后,先检查testOnBorrow,然后再判定testwhileIdle,如果连接空闲时间大于timeBetweenEvictionRunsMillis,则会进行心跳检测。

4:不需要配置validationQuery,如果不配置的情况下会走ping命令,性能更高。

5:连接保存在数组里面,获取连接的时候,获取数组的最后一位。在imeBetweenEvictionRunsMillis时是从前往后进行检查连接的有效性。

在applicatioin.properties中添加配置


  
  1. druid.url=jdbc:postgresql://139.1X8.1.1X8:1XX0/account
  2. druid.driver-class=org.postgresql.Driver
  3. druid.username=root
  4. druid.password=123
  5. druid.initial-size=1
  6. druid.min-idle=1
  7. druid.max-active=20
  8. druid.test-on-borrow=true
  9. druid.timeBetweenEvictionRunsMillis=9000
  • 3.定义配置类,启动读取druid开头的参数

driver-class有和driverClass是不一样的,所以要引入,参数容错坐标


  
  1. <!--配置命名容错处理-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

  
  1. @ConfigurationProperties(prefix = "druid")
  2. public class DruidProperties {
  3. private String url;
  4. private String username;
  5. private String password;
  6. private String driverClass;
  7. private int maxActive;//最大连接数
  8. private int minIdle;//最小连接数
  9. private int initialSize;//初始化数量和
  10. private boolean testOnBorrow;
  11. private Long timeBetweenEvictionRunsMillis;//心跳
  12. }
  • 4.注入

  
  1. @Configuration
  2. @EnableConfigurationProperties(DruidProperties.class)
  3. @ConditionalOnClass(DruidDataSource.class)
  4. @ConditionalOnProperty(prefix = "druid", name = "url")
  5. @AutoConfigureBefore(DataSourceAutoConfiguration.class)
  6. public class DruidAutoConfiguration {
  7. @Autowired
  8. private DruidProperties properties;
  9. @Bean
  10. public DataSource dataSource() {
  11. DruidDataSource dataSource = new DruidDataSource();
  12. dataSource.setUrl(properties.getUrl());
  13. dataSource.setUsername(properties.getUsername());
  14. dataSource.setPassword(properties.getPassword());
  15. dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
  16. if (properties.getInitialSize() > 0) {
  17. dataSource.setInitialSize(properties.getInitialSize());
  18. }
  19. if (properties.getMinIdle() > 0) {
  20. dataSource.setMinIdle(properties.getMinIdle());
  21. }
  22. if (properties.getMaxActive() > 0) {
  23. dataSource.setMaxActive(properties.getMaxActive());
  24. }
  25. dataSource.setTestOnBorrow(properties.isTestOnBorrow());
  26. try {
  27. dataSource.init();
  28. } catch (SQLException e) {
  29. throw new RuntimeException(e);
  30. }
  31. return dataSource;
  32. }
  33. }
  • 5.添加拦截器,拦截器druid性能监控

  
  1. /**
  2. * @Package: pterosaur.account.config.druid
  3. * @Description: 监控数据库性能
  4. * @author: liuxin
  5. * @date: 17/4/21 上午11:23
  6. */
  7. @SuppressWarnings("serial")
  8. @WebServlet(urlPatterns = "/druid/*",
  9. initParams={
  10. @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
  11. @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
  12. @WebInitParam(name="loginUsername",value="test"),// 用户名
  13. @WebInitParam(name="loginPassword",value="test"),// 密码
  14. @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
  15. })
  16. public class DruidStatViewServlet extends StatViewServlet{
  17. }
  18. /**
  19. * @Package: pterosaur.account.config.filter
  20. * @Description: 拦截druid监控请求
  21. * @author: liuxin
  22. * @date: 17/4/21 上午11:24
  23. */
  24. @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
  25. initParams={
  26. @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
  27. })
  28. public class DruidStatFilter extends WebStatFilter{
  29. }
  • 6.最终要一步,启动扫描Servlet

  
  1. @SpringBootApplication
  2. @MapperScan(basePackages = "pterosaur.account.mapper")
  3. @EnableCaching
  4. @ServletComponentScan //这个
  5. public class AccountApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(AccountApplication.class, args);
  8. }
  9. }

文章来源: springlearn.blog.csdn.net,作者:西魏陶渊明,版权归原作者所有,如需转载,请联系作者。

原文链接:springlearn.blog.csdn.net/article/details/102425283

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。