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 ...
随机推荐
- JavaWeb 10_Filter过滤器
一.什么是Filter? 1.Filter 过滤器它是JavaWeb的三大组件之一-.三大组件分别是: Servlet 程序.Listener 监听器.Filter 过滤器2.Filter 过滤器它是 ...
- 5月11日 python学习总结 子查询、pymysql模块增删改查、防止sql注入问题
一.子查询 子查询:把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询 select emp.name from emp inner join dep on emp.dep_id ...
- 5月7日 python学习总结 MySQL数据库(一)
一.数据库介绍 1.数据库相关概念 数据库服务器(本质就是一台计算机,该计算机之上安装有数据库管理软件的服务端) 数据库管理系统RDBMS(本质就是一个C/S机构的套接字软件) 库(文件夹)===&g ...
- 领域驱动模型DDD(一)——服务拆分策略
前言 领域驱动模型设计在业界也喊了几年口号了,但是对于很多"务实"的程序员来说,纸上谈"术"远比敲代码难得太多太多.本人能力有限,在拜读相关作品时既要隐忍书中晦 ...
- 通过rem自适应屏幕尺寸
通过rem自适应屏幕尺寸 常用的前端单位 px px就是pixel的缩写,设备分辨率,物理像素 pt pt就是point的缩写,逻辑分辨率,逻辑像素 em 参考物是父元素的font-size,具有继承 ...
- li 与 li 之间有空白间隔是什么原因引起的,有什么解决办法
li 与 li 之间有空白间隔是什么原因引起的,有什么解决办法 原因 浏览器会把inline元素间的空白字符(空格.换行.Tab等)渲染成一个空格.而为了美观,我们通常是一个 放在一行,这导致 换行后 ...
- maven在idea中的一点使用技巧
maven在idea中的一点使用技巧 idea已经支持将参数的意思也展示出来,确实很方便. -U是强制拉取,因为如果拉取某个jar包,失败了,那么在一段时间内,idea不会重试,除非指定-U. -X呢 ...
- 什么是 Spring Boot?
Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手.
- 服务端处理 Watcher 实现 ?
1.服务端接收 Watcher 并存储 接收到客户端请求,处理请求判断是否需要注册 Watcher,需要的话将数据节点 的节点路径和 ServerCnxn(ServerCnxn 代表一个客户端和服务端 ...
- 使用SpringDataJdbc的@Query注解实现自动映射结果集 ----- RowMapper接口
使用@Query注解的时候,常常需要写sql来映射非域类的实例,通常的做法就是 实现 RowMapper接口,然后new实例一个一个的设置值进去...为此.出世了自动映射工具类 注意事项:此抽象类只是 ...