使用Arthas定位线上问题
2021年4月28日大约 1 分钟
背景:有一个导出PDF的功能,在本地运行正常,在线上异常,抛出的异常无法直接定位到问题。
1、使用arthas来跟踪
- 进入arths,attach进入项目
$ as.sh
Arthas script version: 3.5.5
[INFO] JAVA_HOME: /usr/local/jdk1.8.0_72
[INFO] Process 57135 already using port 3658
[INFO] Process 57135 already using port 8563
Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
[1]: 134242 ccs-gateway.jar
[2]: 47802 ccs-data-biz.jar
2
- 根据线上log定位到异常大致的位置
- 到本地IDE打开源码
createPDF方法空指针,那首先肯定想到的是参数htmlStr是null
- 进入freemarkerRender方法查看,为何返回null
直接查看关键处理函数
template.process(dataMap, out);
使用arthas的watch命令,追踪参数、返回值、异常信息
watch freemarker.template.Template process '{params,returnObj,throwExp}' -n 5 -x 3 '1==1'
追踪发现
源码中因为异常未指定捕获
TemplateModelException
所以template.process(dataMap, out);
方法报错后直接返回了null,所以直接把异常捕获改成ExcelpTion
即可