当前位置:网站首页>聊聊如何利用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
边栏推荐
- Wechat applet waterfall flow and pull up to the bottom
- Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
- Notes | numpy-11 Array operation
- study hard and make progress every day
- appium1.22.x 版本后的 appium inspector 需单独安装
- Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
- Principles of BTC cryptography
- Realize file download through the tag of < a > and customize the file name
- Go practice -- closures in golang (anonymous functions, closures)
猜你喜欢

(subplots用法)matplotlib如何绘制多个子图(轴域)
![[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)
![[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree](/img/0f/bc8c44aee7a2c9dccac050b1060017.jpg)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree

Interface frequency limit access

Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians

XML配置文件
![[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)
![[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)](/img/8b/c10423ee95200a0d94f9fb9dde76eb.jpg)
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)

Make your own dataset

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
随机推荐
Notes | numpy-11 Array operation
Web APIs exclusivity
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
leetcode406. Rebuild the queue based on height
Ueditor, FCKeditor, kindeditor editor vulnerability
Shallow and first code
Common methods of JS array
Go practice -- closures in golang (anonymous functions, closures)
[develop wechat applet local storage with uni app]
Wechat applet waterfall flow and pull up to the bottom
Redis 过期淘汰机制
最大连续子段和(动态规划,递归,递推)
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
appium1.22.x 版本后的 appium inspector 需单独安装
Distinguish between releases and snapshots in nexus private library
[practical project] autonomous web server
Redis Introduction et explication des types de données
Basic use of Metasploit penetration testing framework
1086 tree traversals again (25 points)