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 ...
随机推荐
- 13 数组 Java内存分析 三种初始化
Java内存分析 三种初始化 静态初始化 //静态初始化 创建+赋值 int[] a = {1,2,3}; Man[] mans = {new Man(1,1),new Man(2,2)}; 动态初始 ...
- k8s原来这么简单(一)核心组件与工作原理
k8s官方文档:https://kubernetes.io/zh/docs/home/ 前提 掌握容器技术:Docker,Containerd等 K8S优势 使用简单,少量人/小团队可以轻松维护大型 ...
- 时间模块 time 随机模块random os模块,sys模块
时间模块 time #时间模块 import time #三种格式 #时间戳时间:是一个浮点数,以秒为单位,计算机用语 #结构化时间 :是一个元组 #用于中间转换 #格式化时间:str数据类型, 用 ...
- Django的缓存机制和信号
Django的缓存机制 1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户 ...
- mycat分库分表 看这一篇就够了
之前我们已经讲解过了数据的切分,主要有两种方式,分别是垂直切分和水平切分,所谓的垂直切分就是将不同的表分布在不同的数据库实例中,而水平切分指的是将一张表的数据按照不同的切分规则切分在不同实例的相同 ...
- CF 920A Water The Garden
本题可以看做是一个数学题 因为 在第 1 和第 3 个洒水器之间的 花园灌溉的时间只要 (1 + 3 ) >> 1 - 1 + 1;//这么长的时间 那么我么就可以以此类推到 从而我么可以 ...
- HMS Core机器学习服务图像超分能力,基于深度学习提升新闻阅读体验
在移动端阅读资讯时,人们对高分辨率.高质量的图像要求越来越高.但受限于网络流量.存储.图片源等诸多因素,用户无法便捷获得高质量图片.移动端显示设备的高分辨率图片获得问题亟待解决.不久前,HMS Cor ...
- 【算法】两个list合并
转载博客地址 http://blog.sina.com.cn/s/blog_5da93c8f0101fdrp.html 有两个ArrayList,分别为list1和list2,分析这两个list后生成 ...
- Ribbon负载均衡能干什么?
(1)将用户的请求平摊的分配到多个服务上 (2)集中式LB即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责把访问请求通过某种策略转发至 ...
- 请解释Spring Bean的生命周期?
首先说一下Servlet的生命周期:实例化,初始init,接收请求service,销毁destroy: Spring上下文中的Bean生命周期也类似,如下: (1)实例化Bean: 对于BeanFac ...