当前位置:网站首页>聊聊如何利用p6spy进行sql监控
聊聊如何利用p6spy进行sql监控
2022-07-03 05:14:00 【linyb极客之路】
前言
1、p6spy简介
P6Spy 是一个框架,无需对现有应用程序进行任何代码更改,即可无缝拦截和记录数据库数据。通过 P6Spy 我们可以对 SQL 语句进行拦截,相当于一个 SQL 语句的记录器,这样我们可以用它来作相关的分析,比如性能分析
2、实现原理
p6spy将应用的数据源给劫持了,应用操作数据库其实在调用p6spy的数据源,p6spy劫持到需要执行的sql或者hql之类的语句之后,他自己去调用一个realDatasource,再去操作数据库
3、相关官方文档
github:https://github.com/p6spy/p6spy
官网:https://p6spy.readthedocs.io/en/latest/index.html
p6spy使用
1、在项目中的pom引入相关的GAV
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>${p6spy.version}</version> </dependency>2、切换项目中的jdbc驱动以及数据源
改成如下内容
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: ${DRIVER_CALSS_NAME:com.p6spy.engine.spy.P6SpyDriver} url: ${DATASOURCE_URL:jdbc:p6spy:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai} username: ${DATASOURCE_USERNAME:root} password: ${DATASOURCE_PWD:123456}3、在resource目录下添加spy.properties
配置如示例下内容
#3.2.1以上使用modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory#3.2.1以下使用或者不配置#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory# 自定义日志打印#logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLoggerlogMessageFormat=com.github.lybgeek.p6spy.extentsion.CustomP6SpyLogger#日志输出到控制台#appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger# 使用日志系统记录 sql#appender=com.p6spy.engine.spy.appender.Slf4JLoggerappender=com.github.lybgeek.p6spy.extentsion.CustomStdoutLogger# 设置 p6spy driver 代理deregisterdrivers=true# 取消JDBC URL前缀useprefix=true# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.excludecategories=info,debug,result,commit,resultset# 日期格式dateformat=yyyy-MM-dd HH:mm:ss# 实际驱动可多个#driverlist=org.h2.Driver# 是否开启慢SQL记录outagedetection=true# 慢SQL记录标准 2 秒outagedetectioninterval=24、自定义日志格式【可选】
public class CustomP6SpyLogger implements MessageFormattingStrategy { /** * Sql日志格式化 * * @param connectionId: 连接ID * @param now: 当前时间 * @param elapsed: 花费时间 * @param category: 类别 * @param prepared: 预编译SQL * @param sql: 最终执行的SQL * @param url: 数据库连接地址 * @return 格式化日志结果 */ @Override public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { return StringUtils.isNotBlank(sql) ? " 耗时:" + elapsed + " ms " + now + "\n 执行 SQL:" + sql.replaceAll("[\\s]+", " ") + "\n" : ""; }}在spy.properties中配置自定义日志格式
logMessageFormat=com.github.lybgeek.p6spy.extentsion.CustomP6SpyLogger5、自定义日志输出【可选】
public class CustomStdoutLogger extends com.p6spy.engine.spy.appender.StdoutLogger{ @Override public void logText(String text) { System.out.println("sql:" + text); }}在spy.properties中配置自定义日志输出
appender=com.github.lybgeek.p6spy.extentsion.CustomStdoutLogger6、测试观察控制台输出
sql: 耗时:1 ms 2022-05-10 11:38:34 执行 SQL:SELECT id,username,password,fullname,mobile,email FROM t_user总结
p6spy可以根据sql的执行效率分析sql对sql进行优化,但因为p6spy会对性能有一定影响,因此不适合在正式环境上使用。此外关于p6spy更详细的配置可以查看如下链接 https://p6spy.readthedocs.io/en/latest/configandusage.html
demo链接
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-p6spy
边栏推荐
- Detailed explanation of yolov5 training own data set
- ES7 easy mistakes in index creation
- XML配置文件
- 在PyCharm中配置使用Anaconda环境
- study hard and make progress every day
- Dynamic programming - related concepts, (tower problem)
- Common interview questions of microservice
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- SSM framework integration
- Basic use of Metasploit penetration testing framework
猜你喜欢

How to connect the network: Chapter 1 CSDN creation punch in
![[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)](/img/af/8dfa783c87363a9d75c52e7680d508.jpg)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)

Burp suite plug-in based on actual combat uses tips

微服务常见面试题

leetcode435. Non overlapping interval

Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)

es7创建索引容易犯的错误
![[basic grammar] C language uses for loop to print Pentagram](/img/9e/021c6c0e748e0981d4233f74c83e76.jpg)
[basic grammar] C language uses for loop to print Pentagram

Actual combat 8051 drives 8-bit nixie tube

【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
随机推荐
Go practice -- use redis in golang (redis and go redis / redis)
Notes | numpy-11 Array operation
Differences among bio, NiO and AIO
Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Actual combat 8051 drives 8-bit nixie tube
Yolov5 model construction source code details | CSDN creation punch in
Source insight garbled code solution
Redis 过期淘汰机制
The IntelliJ platform completely disables the log4j component
Gbase8s unique index and non unique index
What is UUID
乾元通多卡聚合路由器的技术解析
Promise
1119 pre- and post order traversals (30 points)
JS dynamic table creation
leetcode452. Detonate the balloon with the minimum number of arrows
Class loading mechanism (detailed explanation of the whole process)
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
Technical analysis of qianyuantong multi card aggregation router