当前位置:网站首页>聊聊如何利用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
- Shuttle + alluxio accelerated memory shuffle take-off
- Common methods of JS array
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
- Dynamic programming - related concepts, (tower problem)
- [research materials] 2021 China's game industry brand report - Download attached
- Redis Introduction et explication des types de données
- 1106 lowest price in supply chain (25 points)
- 1119 pre- and post order traversals (30 points)
猜你喜欢

Promise

Principles of BTC cryptography

Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)

leetcode435. Non overlapping interval

Web APIs exclusivity

Oracle SQL table data loss

leetcode860. Lemonade change

Gbase8s unique index and non unique index
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

Basic knowledge of reflection (detailed explanation)
随机推荐
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
Review the configuration of vscode to develop golang
Notes | numpy-08 Advanced index
Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
1094 the largest generation (25 points)
微服务常见面试题
Audio Focus Series: write a demo to understand audio focus and audiomananger
1107 social clusters (30 points)
Detailed explanation of yolov5 training own data set
[research materials] 2021 China's game industry brand report - Download attached
Interview question -- output the same characters in two character arrays
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Flutter monitors volume to realize waveform visualization of audio
1087 all roads lead to Rome (30 points)
Basic use of Metasploit penetration testing framework
Sprintf formatter abnormal exit problem
Ueditor, FCKeditor, kindeditor editor vulnerability
Kept hot standby and haproxy
The IntelliJ platform completely disables the log4j component