网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何使用Arthas获取SpringApplicationContext还原问题现场

这篇文章主要介绍“如何使用Arthas 获取Spring ApplicationContext还原问题现场”,在日常操作中,相信很多人在如何使用Arthas 获取Spring ApplicationContext还原问题现场问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用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 代码还是没有执行通知

如何使用Arthas 获取Spring ApplicationContext还原问题现场

  • 去翻生产日志,只输入了开始并没有输出定时任务结束,感觉是哪里卡死,想当然以为如果超时总会到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 即可捕获
    如何使用Arthas 获取Spring ApplicationContext还原问题现场

  • 根据目标的索引,执行自定义 OGNL 表达式即可

tt -i 1019 -w 'target.getApplicationContext()'

如何使用Arthas 获取Spring ApplicationContext还原问题现场

使用ApplicationContext获取 定时任务bean 执行 startSchedule
tt -i 1000 -w 'target.getApplicationContext().getBean("baiduSchedule").startSchedule()'

ok 任务重新触发了

事故原因调查清楚,由于使用hutool 的工具类 没有设置timeout 导致无限等待,所以没有执行catch 逻辑

到此,关于“如何使用Arthas 获取Spring ApplicationContext还原问题现场”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网页标题:如何使用Arthas获取SpringApplicationContext还原问题现场
转载来源:http://bjjierui.cn/article/jhcjge.html

其他资讯