网站首页 > 博客文章 正文
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
若您觉得还可以,请帮忙点个“赞”,谢谢~
猜你喜欢
- 2024-10-03 Spring Boot 3.x 使用 Druid(spring boot druid 配置)
- 2024-10-03 springboot配置druid数据源(springboot druiddatasource)
- 2024-10-03 WEB项目开发之SpringMVC+Mybatis搭建网站框架完整版
- 2024-10-03 springboot 引入数据库,集成mybatis-Generator自动生成
- 2024-10-03 阿里巴巴数据库连接池DruidDataSource配置及其常见问题汇总
- 2024-10-03 2021 最新版 Spring Boot 速记教程
- 2024-10-03 配置 Druid 数据源及密码加密 | SpringBoot 2.7 实战基础
- 2024-10-03 绝了!Dataway让SpringBoot不在需要Controller、Service、Mapper
- 2024-10-03 Dataway让SpringBoot不在需要Controller、Service、DAO、Mapper
- 2024-10-03 spring使用技巧-如何实现动态数据源
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)