springboot结合p6spy进行SQL监控

1.学习p6spy的相关链接

英文文档:Integrating P6Spy --- p6spy 3.9.2-SNAPSHOT documentationhttps://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.https://github.com/p6spy/p6spy

mybatis-plus相关介绍:

SQL分析与打印 | MyBatis-Plus (baomidou.com)https://baomidou.com/guides/p6spy/

2.p6spy简介

2.1p6spy是什么

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

2.2p6spy能做什么

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

3.p6spy整合springboot

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

XML 复制代码
   <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.9.1</version>
        </dependency>

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

配置之前:

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

修改配置后:

XML 复制代码
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文件,内容如下:

XML 复制代码
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.启动后查询数据库中的数据,控制台会自动打印内容如下:

相关推荐
江晓鱼未暖几秒前
十七、Redis 核心原理与架构详解
大数据·数据库·数据仓库·redis·缓存·架构
copyer_xyf1 分钟前
PostgreSQL 做向量检索:一条单库路线
数据库·agent·nestjs
caishenzhibiao4 分钟前
顺势捕猎者副图 同花顺期货通指标
java·c语言·c#
Access开发易登软件5 分钟前
Access 怎么做前后端分离?用 Web API 读写 SQL Server
前端·数据库·人工智能·microsoft·excel·access
Juicedata9 分钟前
腾讯云 x JuiceFS:基于 FoundationDB 的企业级统一存储实践
数据库·人工智能·科技·云计算·腾讯云
小罗水20 分钟前
第13章 Redis 缓存、幂等锁与任务状态
数据库·redis·缓存
SelectDB技术团队29 分钟前
当 PostgreSQL 面临性能瓶颈:80TB 电商业务迁移至 Apache Doris 的实践思考
数据库·postgresql·apache
心念枕惊33 分钟前
新写了个直播录制工具,可录制抖音快手斗鱼直播
运维·服务器·数据库
笨蛋不要掉眼泪35 分钟前
RabbitMQ消息队列:SpringAMQP
java·分布式·rabbitmq
逐米时代1 小时前
向量数据库选型——Chroma、Qdrant、Milvus到底怎么选
数据库·milvus