Spring Boot配置druid监控页功能
1.导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hao</groupId>
<artifactId>spring-boot-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jdbc</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<mysql.version>8.0.19</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.在application.ymal中配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
password: hao20001010
3.编写配置类
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")//与配置文件进行绑定
@Bean
public DruidDataSource druidDataSource(){
return new DruidDataSource();
}
//配置监控页功能
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
}
Spring中内置的有数据库连接池,如果使用Druid连接池,需要将该组件导入到容器中
4.运行访问http://localhost:8080/druid
结果:

接着通过查询数据库查看SQL监控
编写Controller类
/**
* @author:抱着鱼睡觉的喵喵
* @date:2020/12/23
* @description:
*/
@RestController
public class SqlController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/sql")
public String control(){
Long query= jdbcTemplate.queryForObject("select count(*) from admin", Long.class);
return "query";
}
}
*更改配置类,开启监控功能
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DruidDataSource druidDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
//加入监控功能
dataSource.setFilters("stat");
return dataSource;
}
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
}
1.访问http://localhost:8080/druid,点开SQL监控
2.打开另外一个页面访问http://localhost:8080/sql
3.刷新监控页出现如下

开启Web应用监控
在配置类中导入开启Web应用的组件
/**
* @author:抱着鱼睡觉的喵喵
* @date:2020/12/23
* @description:
*/
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DruidDataSource druidDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
//加入监控功能
dataSource.setFilters("stat");
return dataSource;
}
@Bean
public ServletRegistrationBean statsViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
return bean;
}
/**
* WebStatFilter 用于采集web-jdbc关联监控的数据
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter(){
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> filterFilter=new FilterRegistrationBean<>(webStatFilter);
filterFilter.setUrlPatterns(Arrays.asList("/*"));//拦截所有资源
//以下资源不拦截*.js,*.gif,*.jpg,*.css等
filterFilter.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.css,*.ico,/druid/*");
return filterFilter;
}
}
2.运行测试
访问http://localhost:8080/sql
查看Web应用监控数据

开启防火墙
在filter中添加wall

启动访问http://localhost:8080/druid,点击SQL防火墙
访问http://localhost:8080/sql
再次刷新监控页面

开启druid登录功能
在配置类中增加
运行访问http://localhost:8080/druid

可以在配置文件进行配置

============================================================================================================================================================================================================================================
下面使用ymal配置文件进行配置
导入坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
spring:
datasource:
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
password: hao20001010
druid:
filters: stat,wall
aop-patterns: com.hao.boot.*
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
再次运行

其他具体操作访问官方文档
Spring Boot配置druid监控页功能的更多相关文章
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot配置druid数据源和监控配置
直接上代码: 一.pom.xml中添加依赖 <dependency> <groupId>com.github.drtrang</groupId> <artif ...
- spring boot:配置druid数据库连接池(开启sql防火墙/使用log4j2做异步日志/spring boot 2.3.2)
一,druid数据库连接池的功能? 1,Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 2,druid的官方站: http ...
- Spring Boot (13) druid监控
druid druid是和tomcat jdbc一样优秀的连接池,出自阿里巴巴.除了连接池,druid哈hi有一个很实用的监控功能. pom.xml 添加了以下依赖后,会自动用druid连接池替代默认 ...
- spring boot 开启Druid监控功能
1.配置yml spring: datasource: # 数据源基本配置 username: song password: 123456 driver-class-name: com.mysql.j ...
- spring boot(11)-druid监控
druid druid是和tomcat jdbc一样优秀的连接池,出自阿里巴巴.关于druid连接池参数,参考 https://github.com/alibaba/druid/wiki/DruidD ...
- spring boot配置druid数据连接池
Druid是阿里巴巴开源项目中一个数据库连接池. Druid是一个jdbc组合,包含三个部分, 1.DruidDriver代理Driver,能够提供基于Filter-Chain模式得插件体系2.Dru ...
- Spring Boot使用Druid连接池基本配置
以下为Spring Boot配置Druid 一.pom.xml配置 <dependency> <groupId>com.alibaba</groupId> < ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
随机推荐
- DataFrame计算corr()函数计算相关系数时,出现返回值为空或NaN的情况+np.log1p()
- LGP5363题解
感觉博弈题都是高大上神秘结论... 感谢@KaiSuoShuTong 开锁疏通愿意教我这题的博弈部分/qq 考虑每次移动棋子,实际上是有一车 \(a_i\),每次操作相当于令 \(a_i-c,a_{i ...
- OpenCV基础_二
阈值和平滑处理 cv2.threshold()二值化函数 ret,thresh = cv2.threshold(src, thresh, maxval, type[, dst]) 参数 src:所要进 ...
- Flask 之 WebSocket
http:是一个协议 规定:数据传输格式 -/r/n/r/n 一次的请求,一次的响应,断开了 短链接 无状态 服务器收到的请求,做出的响应给客户端 客户端主动向服务器发起请求 基于socket sen ...
- Windows 如何打开 .md 文件
•问题 最近在学习[C++|黑马程序员],对于课堂笔记中的 .md 文档无从下手,要是直接通过 Chrome 打开排版又很乱,且不能正常显示URL中的内容: 这可如何是好??? •解决方案 1. 打开 ...
- sharding-jdbc教程 看这一篇就够了
Sharding-JDBC是ShardingSphere的第一个产品,也是ShardingSphere的前身. 它定位为轻量级Java框架,在Java的JDBC层提供的额外服务.它使用客户端直连数 ...
- kafka分布式的情况下,如何保证消息的顺序?
作者:可期链接:https://www.zhihu.com/question/266390197/answer/772404605来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- idea中将已有项目转变为git项目,并提交到git服务器上
idea中将已有项目转变为git项目,并提交到git服务器上 前言 有时候,本地有个项目不错,想要分享到github或者码云上,我们就会有这样的需求:项目在本地已经存在(可能不是git项目),又想提交 ...
- 深入理解Java虚拟机-HotSpot虚拟机对象探秘
一.对象的创建过程 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析和初始化过.如果没有,那就先执行相应的类 ...
- Gradle依赖声明类型
compileOnly -用于编译生产代码所必需的依赖关系,但不应作为运行时类路径的一部分 implementation(取代compile)-用于编译和运行时 runtimeOnly(取代runti ...