使用Arthas 獲取Spring ApplicationContext還原問題現場

背景

最近來了個實習僧小弟,安排他實現對目標網站 連通性檢測的小功能,簡單講就是將下邊的shell 腳本換成Java 代碼來實現

#!/bin/bash
URL="https://www.baidu"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
#echo $HTTP_CODE
if [ $HTTP_CODE != '200' ];then
curl 'https://oapi.dingtalk.com/robot/send?access_token=xx' \
   -H 'Content-Type: application/json' \
   -d '{"msgtype": "text",
        "text": {
             "content": "百度平臺狀態不正常,請注意!"
        },
        "isAtAll": true
      }'

fi

功能實現

使用spring task

@Scheduled(cron = "0 0 0/1 * * ? ")
public void startSchedule() {
    log.info("開始執行定時任務 ,檢測百度網站連通性");
    try {
        HttpResponse response = HttpRequest.get("").execute();
        if (HttpStatus.HTTP_OK != response.getStatus()) {
            this.send2DingTalk(response.getStatus());
        }
        log.info("請求百度成功,返回報文:{}",response.body());
    } catch (HttpException e) {
        log.error("請求異常百度:{}", e);
        this.send2DingTalk(e.getMessage());
    }
    log.info("執行檢測百度網站連通任務完畢");
}

問題描述

部署在服務器上,我的老jio本 都已經呼叫任務狀態不正常了,可是小弟的Java 代碼還是沒有執行通知

  • 去翻生產日誌,只輸入了開始並沒有輸出定時任務結束,感覺是哪裏卡死,想當然以爲如果超時總會到catch 邏輯,排查無果
  • 由於任務是一小時一次,如何快速觸發一下這個異常,還原事故現場
  • 由於使用簡單的Spring Task 沒有圖形化界面和API接口

Arthas 還原事故現場,重新觸發任務

核心拿到 spring context 然後執行它的 startSchedule 方法

確定監控點

  • SpringMVC 的請求會通過 RequestMappingHandlerAdapter 執行invokeHandlerMethod 到達目標接口上進行處理
  • 而在 RequestMappingHandlerAdapter類中有 getApplicationContext()
@Nullable
public final ApplicationContext getApplicationContext() throws IllegalStateException {
    if (this.applicationContext == null && this.isContextRequired()) {
        throw new IllegalStateException("ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
    } else {
        return this.applicationContext;
    }
}
  • 任意執行一次請求獲取到 RequestMappingHandlerAdapter target 目標,然後執行 getApplicationContext

tt命令 獲取到ApplicationContext

  • arthas 執行 tt
tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
  • 任意執行一次web 請求,tt 即可捕獲

  • 根據目標的索引,執行自定義 OGNL 表達式即可
tt -i 1019 -w 'target.getApplicationContext()'

使用ApplicationContext獲取 定時任務bean 執行 startSchedule

tt -i 1000 -w 'target.getApplicationContext().getBean("baiduSchedule").startSchedule()'

ok 任務重新觸發了

事故原因調查清楚,由於使用hutool 的工具類 沒有設置timeout 導致無限等待,所以沒有執行catch 邏輯

總結

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章