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=UTCusername: rootdriver-class-name: com.mysql.cj.jdbc.Driverpassword: hao20001010

3.编写配置类

@Configuration
public class MyDataSourceConfig {@ConfigurationProperties("spring.datasource")//与配置文件进行绑定@Beanpublic DruidDataSource druidDataSource(){return new DruidDataSource();}
//配置监控页功能@Beanpublic 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 {@AutowiredJdbcTemplate 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")@Beanpublic DruidDataSource druidDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();//加入监控功能dataSource.setFilters("stat");return dataSource;}@Beanpublic 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")@Beanpublic DruidDataSource druidDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();//加入监控功能dataSource.setFilters("stat");return dataSource;}@Beanpublic ServletRegistrationBean statsViewServlet(){StatViewServlet statViewServlet = new StatViewServlet();ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");return bean;}/*** WebStatFilter 用于采集web-jdbc关联监控的数据* @return*/@Beanpublic 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=UTCusername: rootdriver-class-name: com.mysql.cj.jdbc.Driverpassword: hao20001010druid:filters: stat,wallaop-patterns: com.hao.boot.*stat-view-servlet:enabled: truelogin-username: adminlogin-password: adminreset-enable: falseweb-stat-filter:enabled: trueurl-pattern: /*exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'filter:stat:slow-sql-millis: 1000log-slow-sql: trueenabled: truewall:enabled: true

再次运行

其他具体操作访问官方文档

Spring Boot配置druid监控页功能相关推荐

  1. spring boot配置druid(德鲁伊)

    spring boot配置druid(德鲁伊) 关于druid的介绍请看 阿里巴巴温少访谈 1.引入相关依赖,全部依赖是上一篇spring boot+mybatis依赖的基础上,再加上下边的依赖,如下 ...

  2. Spring Boot 集成 Druid 监控数据源

    关注"Java后端技术全栈" 回复"面试"获取全套大厂面试资料 Druid 介绍 Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和 ...

  3. Spring Boot 整合Druid监控

    一.Druid简介 Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可 ...

  4. Spring boot Security Druid 监控

    1. Druid 配置属性 Druid数据源(连接池): 类似 c3p0.dbcp 数据源,可设置数据源连接初始化大小.最大连接数.等待时间.最小连接数等: Druid web 监控 filter(W ...

  5. Spring Boot Welcome Page 欢迎页功能

    Spring Boot 2.7.1 官方文档里对其 Welcome Page 欢迎页功能有如下描述: 官方文档地址 1.1.5. Welcome Page Spring Boot supports b ...

  6. spring boot配置druid

    为什么80%的码农都做不了架构师?>>>    在pom中添加starter依赖 <dependency><groupId>com.alibaba</g ...

  7. java was datasource_使用Spring Boot配置Druid时dataSource无法被autowired

    配置好了dataSource之后,想测试一下有没有配置成功 package com.yang.Controller; import com.alibaba.druid.pool.DruidDataSo ...

  8. spring boot 配置 druid的filters时报错 Reason: org.apache.log4j.Priority

    报错信息: Failed to bind properties under 'spring.datasource.druid' to javax.sql.DataSource: Property: s ...

  9. Spring Boot集成Druid监控

    package com.xxxxxxx.framework.datasource.druid;import com.alibaba.druid.support.http.WebStatFilter;i ...

最新文章

  1. ATS 5.2.1中Background-Fetch插件调研笔记
  2. LintCode 最大正方形
  3. linux配置静态IP后ping外网不通的解决方案
  4. Android 异步加载图片分析
  5. 1365. 有多少小于当前数字的数字(哈希表)
  6. 当在做产品规划时,我们应该干什么 | PMcaff-产品
  7. Matlab | 数字信号处理:用FFT做谱分析
  8. [BZOJ5303] [HAOI2018] 反色游戏
  9. 破解系统设计访谈:Twitter软件工程师的提示
  10. java中System类、String Builder类简介
  11. Python爬虫入门五之URLError异常处理
  12. 浅谈SEO翻倍提升网站流量
  13. Android系统分辨率修改方法,Android:系统分辨率的修改
  14. 社工心理学:如何让LOL找你约架的社会人给你道歉
  15. Hides for Mac v5.6.0.1 一键隐藏所有应用
  16. 语法和语义之间的差异_语法和语义之间的区别
  17. 怎样在网站网页中插入音乐?
  18. 2021年美亚杯个人赛复盘
  19. 重装系统 SSD+HDD并开启IRST(英特尔快速存储技术)
  20. aws scp上传下载文件

热门文章

  1. 谈谈数据库的ACID
  2. php操作剪贴板内容代码,详细解答JS操作剪贴板
  3. mysql strict_关于mysql 严格模式 Strict Mode的说明讲解
  4. Vue 和 React 大厂面试通关指南.pdf
  5. 未定义的索引 php中_php的未定义索引如何解决
  6. 解决端口8080被占用的问题!
  7. Cppcheck代码扫描工具介绍
  8. 一种可扩展、精确的激光雷达点云冬季除雪算法
  9. 爬虫---scrapy爬虫框架(详细+实战)
  10. 位置式 PID 算法、恩智浦杯智能车电机PID