当前位置: 首页 > article >正文

springboot结合p6spy进行SQL监控

1.学习p6spy的相关链接

英文文档:Integrating P6Spy — p6spy 3.9.2-SNAPSHOT documentationicon-default.png?t=O83Ahttps://p6spy.readthedocs.io/en/latest/integration.html

github链接:GitHub - p6spy/p6spy: P6Spy is a framework that enables database data to be seamlessly intercepted and logged with no code changes to the application.icon-default.png?t=O83Ahttps://github.com/p6spy/p6spy

mybatis-plus相关介绍:

SQL分析与打印 | MyBatis-Plus (baomidou.com)icon-default.png?t=O83Ahttps://baomidou.com/guides/p6spy/

2.p6spy简介

2.1p6spy是什么

P6Spy 是一款开源的 Java 框架,是一个强大的数据库监控工具。可以帮助开发者监控并分析应用程序与数据库之间的交互行为。

2.2p6spy能做什么

  • 像往常一样正常使用mybatisplus或mybatisflex,p6spy在幕后帮我们记录所有的SQL及其执行时间。
  • 通过配置进行慢SQL检测
  • 自定义输出SQL相关性能的日志格式
  • 对日志输出进行控制,可输出到控制台或者文件系统

3.p6spy整合springboot

1.在pom.xml文件引入依赖:

   <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.9.1</version>
        </dependency>

2.在application.yml文件修改数据库相关配置:

配置之前:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis-flex?
    username: root
    password: 123456

修改配置后:

spring:
  datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://localhost:3306/mybatis-flex?
    username: root
    password: 123456

3.在application.yml同级创建spy.properties文件,内容如下:

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 使用日志系统记录sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置记录Log例外
excludecategories=info,debug,result,batc,resultset
# 设置使用p6spy driver来做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动
driverlist=com.mysql.jdbc.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 秒
outagedetectioninterval=2
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗时: %(executionTime) ms | 连接信息: %(category)-%(connectionId) | 执行语句: %(sql)

4.启动后查询数据库中的数据,控制台会自动打印内容如下:


http://www.kler.cn/news/317114.html

相关文章:

  • Hadoop 性能优化高频面试题及答案
  • XSS—xss-labs靶场通关
  • codeforces round974 div3 分层图 树形dp
  • uniApp实现APP内自更新
  • 【OpenCV】场景中人的识别与前端计数
  • 针对论坛系统设计测试用例
  • 分布式难题-三座大山NPC
  • 使用streaming-json-py插件处理JSON数据流:详细指南
  • 【论文阅读笔记】TOOD: Task-aligned One-stage Object Detection
  • Linux服务部署指南
  • Vue3教程 - 1 Vue简介
  • minitrace使用
  • 只装了WPS,DOC文档无法打开
  • c语言面试字符串复制
  • PHP邮箱系统:从入门到实战搭建教程指南!
  • 12. Scenario Analysis for greedy algorithm
  • java中使用BP网络进行回归
  • 【ComfyUI】控制光照节点——ComfyUI-IC-Light-Native
  • 爵士编曲:爵士鼓编写 爵士鼓笔记 底鼓和军鼓 闭镲和开镲 嗵鼓
  • 9.23作业
  • 无人机之激光避障篇
  • 3.4 爬虫实战-爬去智联招聘职位信息
  • 什么是反射,反射用途,spring哪些地方用到了反射,我们项目中哪些地方用到了反射
  • 【python】requests 库 源码解读、参数解读
  • Maven笔记(一):基础使用【记录】
  • Spring Boot 中的拦截器 Interceptors
  • 【已解决】用JAVA代码实现递归算法-从自然数中取3个数进行组合之递归算法-用递归算法找出 n(n>=3) 个自然数中取 3 个数的组合。
  • 在云渲染中3D工程文件安全性怎么样?
  • 【HarmonyOS】深入理解@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化
  • Unity-Screen屏幕相关