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

9 篇文章 0 订阅
6 篇文章 1 订阅

Druid的监控统计

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

1、配置pom.xml

<!-- druid数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.20</version>
</dependency>

<!-- SpringBoot程序监控系统 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

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

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/blog
    username: root
    password: test
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 2000
      validationQuery: select 1
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      filters: stat, wall
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        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中添加监控配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 2000
      validationQuery: select 1
      testOnBorrow: false
      testOnReturn: false
      testWhileIdle: true
      filters: stat,wall
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: 123
    blog:
      jdbc-url: jdbc:mysql://localhost:3306/blog
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
    user:
      jdbc-url: jdbc:mysql://localhost:3306/user
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver

3、数据源配置

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

blog的配置类:

@Configuration
@MapperScan(basePackages = "com.tn222.springboot.article10.dao.blog", sqlSessionTemplateRef = "blogSqlSessionTemplate")
public class DataSourceBlogConfig {

    @Value("${spring.datasource.blog.jdbc-url}")
    private String url;

    @Value("${spring.datasource.blog.username}")
    private String username;

    @Value("${spring.datasource.blog.password}")
    private String password;

    @Value("${spring.datasource.blog.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.druid.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.druid.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.druid.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.druid.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.druid.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.druid.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.druid.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.druid.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.druid.filters}")
    private String filters;

    @Primary
    @Bean(name = "blogDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.blog")
    public DataSource blogDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);

        // 具体配置
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setTestWhileIdle(testWhileIdle);

        // 配置druid监控sql语句,如果你有两个数据源,这个配置哪个数据源就监控哪个数据源的sql,同时配置就都监控
        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Bean(name = "blogSqlSessionFactory")
    public SqlSessionFactory blogSqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "blogSqlSessionTemplate")
    public SqlSessionTemplate blogSqlSessionTemplate(@Qualifier("blogSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

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

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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值