当前位置:   article > 正文

Spring Boot(十):Druid的监控统计和多数据源配置_stat-view-servlet

stat-view-servlet

Druid的监控统计

Druid内置提供一个StatFilter,用于统计监控信息。下面我们就来做一些配置,启动Druid的监控。

1、配置pom.xml

  1. <!-- druid数据库连接池 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. <version>1.1.20</version>
  6. </dependency>
  7. <!-- SpringBoot程序监控系统 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-actuator</artifactId>
  11. </dependency>

2、在application.yml中添加监控配置

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://localhost:3306/blog
  4. username: root
  5. password: test
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. druid:
  8. initialSize: 5
  9. minIdle: 5
  10. maxActive: 20
  11. maxWait: 2000
  12. validationQuery: select 1
  13. testOnBorrow: false
  14. testOnReturn: false
  15. testWhileIdle: true
  16. filters: stat, wall
  17. stat-view-servlet:
  18. enabled: true
  19. url-pattern: /druid/*
  20. reset-enable: true
  21. login-username: admin
  22. login-password: 123

Druid的配置详解见《Spring Boot(九):数据库连接池Druid》

filters节点:配置监控统计拦截的filters

1)stat:StatFilter的别名是stat,配置stat表示开启SQL监控

2)wall:开启SQL防火墙

stat-view-servlet:配置 Druid 监控信息显示页面

1)url-pattern:访问地址规则

2)reset-enable:是否允许清空统计数据,false:不允许,true:允许

3)login-username:监控页面的用户户

4)login-password:监控页面的密码

3、调用接口

我们使用上篇文章《Spring Boot(九):数据库连接池Druid》中的实体、Dao层和Controller层代码来调用接口

4、访问监控

打开http://localhost:8080/druid/,会看到如下的登录页面:

输入配置的账号密码之后,会看到监控统计页面:

数据源:可以看到数据库连接池的配置信息及当前的使用情况

SQL监控:该数据源中执行的SQL语句及其统计数据

SQL防火墙:SQL的防御统计和表的访问统计

Druid的多数据源配置

《Spring Boot(八):MyBatis的多数据源配置》中,我们已经看到了需要配置多数据源的场景,下面我们来看看Druid的多数据源配置

1、配置pom.xml

跟上面“Druid的监控统计”配置一样

2、在application.yml中添加监控配置

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. druid:
  5. initialSize: 5
  6. minIdle: 5
  7. maxActive: 20
  8. maxWait: 2000
  9. validationQuery: select 1
  10. testOnBorrow: false
  11. testOnReturn: false
  12. testWhileIdle: true
  13. filters: stat,wall
  14. stat-view-servlet:
  15. enabled: true
  16. url-pattern: /druid/*
  17. reset-enable: true
  18. login-username: admin
  19. login-password: 123
  20. blog:
  21. jdbc-url: jdbc:mysql://localhost:3306/blog
  22. username: root
  23. password: 123456
  24. driver-class-name: com.mysql.cj.jdbc.Driver
  25. user:
  26. jdbc-url: jdbc:mysql://localhost:3306/user
  27. username: root
  28. password: 123456
  29. driver-class-name: com.mysql.cj.jdbc.Driver

3、数据源配置

配置文件配置好之后,我们创建两个配置类来加载配置信息,初始化数据源

blog的配置类:

  1. @Configuration
  2. @MapperScan(basePackages = "com.tn222.springboot.article10.dao.blog", sqlSessionTemplateRef = "blogSqlSessionTemplate")
  3. public class DataSourceBlogConfig {
  4. @Value("${spring.datasource.blog.jdbc-url}")
  5. private String url;
  6. @Value("${spring.datasource.blog.username}")
  7. private String username;
  8. @Value("${spring.datasource.blog.password}")
  9. private String password;
  10. @Value("${spring.datasource.blog.driver-class-name}")
  11. private String driverClassName;
  12. @Value("${spring.datasource.druid.initialSize}")
  13. private int initialSize;
  14. @Value("${spring.datasource.druid.minIdle}")
  15. private int minIdle;
  16. @Value("${spring.datasource.druid.maxActive}")
  17. private int maxActive;
  18. @Value("${spring.datasource.druid.maxWait}")
  19. private int maxWait;
  20. @Value("${spring.datasource.druid.validationQuery}")
  21. private String validationQuery;
  22. @Value("${spring.datasource.druid.testOnBorrow}")
  23. private boolean testOnBorrow;
  24. @Value("${spring.datasource.druid.testOnReturn}")
  25. private boolean testOnReturn;
  26. @Value("${spring.datasource.druid.testWhileIdle}")
  27. private boolean testWhileIdle;
  28. @Value("${spring.datasource.druid.filters}")
  29. private String filters;
  30. @Primary
  31. @Bean(name = "blogDataSource")
  32. @ConfigurationProperties(prefix = "spring.datasource.blog")
  33. public DataSource blogDataSource() {
  34. DruidDataSource dataSource = new DruidDataSource();
  35. dataSource.setUrl(url);
  36. dataSource.setUsername(username);
  37. dataSource.setPassword(password);
  38. dataSource.setDriverClassName(driverClassName);
  39. // 具体配置
  40. dataSource.setInitialSize(initialSize);
  41. dataSource.setMinIdle(minIdle);
  42. dataSource.setMaxActive(maxActive);
  43. dataSource.setMaxWait(maxWait);
  44. dataSource.setValidationQuery(validationQuery);
  45. dataSource.setTestOnBorrow(testOnBorrow);
  46. dataSource.setTestOnReturn(testOnReturn);
  47. dataSource.setTestWhileIdle(testWhileIdle);
  48. // 配置druid监控sql语句,如果你有两个数据源,这个配置哪个数据源就监控哪个数据源的sql,同时配置就都监控
  49. try {
  50. dataSource.setFilters(filters);
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. return dataSource;
  55. }
  56. @Bean(name = "blogSqlSessionFactory")
  57. public SqlSessionFactory blogSqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception {
  58. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  59. bean.setDataSource(dataSource);
  60. return bean.getObject();
  61. }
  62. @Bean(name = "blogSqlSessionTemplate")
  63. public SqlSessionTemplate blogSqlSessionTemplate(@Qualifier("blogSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  64. return new SqlSessionTemplate(sqlSessionFactory);
  65. }
  66. }

user的配置类:

除了userDataSource上没有@Primary注解外,其他的同blog配置类(blog改为user)

4、实体类和dao层配置

具体可参见《Spring Boot(八):MyBatis的多数据源配置》中的实体类和dao层配置(除了命名空间不同外,其他相同)

5、测试验证

1)接口

编写ArticleController和UserInfoController,具体可参见《Spring Boot(八):MyBatis的多数据源配置》中的controller

分别调用/article/get和/user/get接口,可以看到,获取到了相应的信息

2)监控统计

访问http://localhost:8080/druid/,可以看到数据源tab中出现了两个数据源,SQL监控中出现了调用接口所用到的sql的监控信息

本文示例代码,详见https://gitee.com/tunan222/spring-boot-demo

更多内容,请关注公众号“图南随笔”:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/46595
推荐阅读
相关标签
  

闽ICP备14008679号