Spring?Boot如何監(jiān)控SQL運行情況?
前言
監(jiān)控SQL是現(xiàn)在項目運維中必要的一部分,通過SQL監(jiān)控我們能夠明顯的分析系統(tǒng)那些地方存在問題,從而有效的進行SQL優(yōu)化,提升系統(tǒng)的性能。那么常見的SQL監(jiān)控方式又那些呢?

MYSQL監(jiān)控慢SQL
MySLQ如果需要監(jiān)控慢SQL,需要在/ect/my.cnf文件中進行如下配置:
slow_query_log = ON slow_query_log_file = /data/mysql/slow.log long_query_time = 2
- slow_query_log:打開慢SQL日志
- slow_query_log_file:輸出慢SQL文件的路徑
- long_query_time : 定義SQl時間為多久為慢SQL
輸出結果
select userid from t_user;
# Time: 2022-04-16T20:40:30.411674Z
# Query_time: 90.438767 Lock_time: 0.000000 Rows_sent: 43 Rows_examined: 25360591
輸出SQL的查詢時間、鎖定時間、影響的行數(shù)等。
Druid監(jiān)控慢SQL
Spring Boot默認使用的數(shù)據(jù)庫連接池為HikariCP,但是Druid連接池為阿里巴巴開發(fā)的,提供強大的監(jiān)控和擴展功能,如果項目中使用Druid連接池而沒有開啟SQL監(jiān)控,那么對于項目來說就是一種浪費,那么Spring Boot Druid如何監(jiān)控SQL呢?
添加Druid依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>Yml文件中配置Druid屬性
spring:
messages:
encoding: UTF-8
baseName: i18n/messages
fallbackToSystemLocale: false
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&characterEncoding=utf8&serverTimezone=Hongkong&useSSL=true&nullNamePatternMatchesAll=true&autoReconnect=true
username: xx
password: yy
#初始連接池值
initial-size: 10
//最大連接數(shù)
max-active: 100
//最小連接數(shù)
min-idle: 10
#連接等待時間
max-wait: 60000
#
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
#配置StatViewServlet監(jiān)控頁面
stat-view-servlet:
#開啟
enabled: true
#訪問監(jiān)控路徑的地址
url-pattern: /druid/*
#不允許清空,重新統(tǒng)計
reset-enable: false
#登錄用戶名
login-username: admin
#登錄密碼
login-password: admin
#允許訪問地址
allow: 127.0.0.1
########## 配置WebStatFilter,用于采集web關聯(lián)監(jiān)控的數(shù)據(jù) #########
web-stat-filter:
#啟動statFilter
enabled: true
#排除的url
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
#開啟session統(tǒng)計
session-stat-enable: true
#session的最大個數(shù),默認為100
session-stat-max-count: 1000
#過濾的url
url-pattern: /*
filter:
stat:
#開啟慢sql統(tǒng)計
log-slow-sql: true
#慢sql的時間
slow-sql-millis: 2000
merge-sql: true
#開啟druid datasource監(jiān)控
enabled: true
wall:
config:
multi-statement-allow: true監(jiān)控頁面
1.啟動項目,訪問http://ip:port/druid, 進入登錄頁面,輸入用戶名和密碼登錄

2.數(shù)據(jù)源頁面,該頁面配置的是當前dataSource的相關信息

3.SQL監(jiān)控頁面

此頁面會展示所有執(zhí)行sql的語句,包含SQL執(zhí)行次數(shù)、執(zhí)行的時間、事務執(zhí)行次數(shù)、最慢執(zhí)行時間等信息
4.URI監(jiān)控,統(tǒng)計了所有Controller接口的訪問以及執(zhí)行情況

5.去除相關的廣告
訪問監(jiān)控頁面的時候,你可能會在頁面底部(footer)看到阿里巴巴的廣告 原因:引入的druid的jar包中的common.js(里面有一段js代碼是給頁面的footer追加廣告的) 去除的方案有兩種
5.1 直接注釋源碼中的的相關代碼
找到 druid-1.19 jar包 在common.j中直接注釋如下代碼即可
// this.buildFooter();
5.2 自定義過濾來去除廣告
@WebFilter(urlPatterns = "/druid/js/common.js")
public class RemoveDruidAdvertFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
String text = Utils.readFromResource("support/http/resources/js/common.js");
text = text.replace("this.buildFooter();", "");
response.getWriter().write(text);
}
}注意需要在中添加相關的過濾器
@SpringBootApplication
@ServletComponentScan("com.skywares.fw.common.filter")//配置過濾器的地址
public class FwCoreApplication
{
public static void main(String[] args)
{
SpringApplication.run(FwCoreApplication.class, args);
}
}特殊說明
目前Driud監(jiān)控SQL功能只能針對單數(shù)據(jù)庫的情況,對于多數(shù)據(jù)源的場景無法滿足。
Prometheus+grafana實現(xiàn)SQL監(jiān)控
Prometheus+grafana實現(xiàn)SQL監(jiān)控方案比較復雜,本文中就不詳細講解了,在后續(xù)的性能優(yōu)化專欄中會重點講解。
結尾
SQL監(jiān)控是項目中比較重要的一環(huán),本文從幾個方法來講解如何監(jiān)控SQL,如果項目中還沒使用此功能,那么趕緊去開啟吧。
到此這篇關于Spring Boot如何監(jiān)控SQL運行情況的文章就介紹到這了,更多相關SpringBoot監(jiān)控SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot中的properties參數(shù)配置詳解
這篇文章主要介紹了spring boot中的properties參數(shù)配置,需要的朋友可以參考下2017-09-09
SpringBoot中整合MyBatis-Plus的方法示例
這篇文章主要介紹了SpringBoot中整合MyBatis-Plus的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Struts2的配置 struts.xml Action詳解
這篇文章主要介紹了Struts2的配置 struts.xml Action詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

