arthas是阿里开源的一个java线上监控以及诊断工具,在docker容器中我们无需重启服务,也不用更改代码,就可以完成对应用内存、线程、日志级别的修改、方法调用的出入参、异常监测、执行耗时等,xxxx.xxxx.xxxxx为脱敏内容
1. 在docker容器中怎么安装和启动arthas
1.1 远程下载arthas的jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
1.2 启动arthas
java -jar arthas-boot.jar
2. arthas可以解决哪些问题以及怎么解决
2.1 线上发布的容器内是否是包含最新编写的代码?反编译
jad com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance
2.2 线上加载的环境变量有哪些,变量值是什么?
sysenv
sysenv JAVA_OPTS
ognl '#env=@System@getenv(),{#env}'
ognl '#config=@System@getenv("proxy.config"),{#config}'
2.3 线上的系统属性有哪些,属性值是什么?
sysprop
sysprop nacos.config.password
sysprop nacos.config.password test 永久修改
ognl '#properties=@System@getProperties(),{#properties}'
ognl '#SW_AGENT_NAME=@System@getProperty("SW_AGENT_NAME"),{#SW_AGENT_NAME}'
2.4 打印日志级别较低的日志:FATAL > ERROR > WARN > INFO > DEBUG > TRACE(未生效)
logger-- root路径下整体更新
logger --name ROOT --level debug-- 没有特别指定日志等级
logger -n com.xxxx.xxxx.xxxxx.cache;-- 查看classloader hashcode
sc -d com.xxxx.xxxx.xxxxx.cache;-- 根据hashcode更新level
logger -c 5cc69cfe --name ROOT --level debug
2.5 查看类的静态属性以及对静态属性的处理
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCachegetstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'entrySet().iterator.{? #this.key=="I_REDPAY_P01_REDPAYGETORDER_1.0_DEF"}'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'get("I_REDPAY_P01_REDPAYGETORDER_1.0_DEF")'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'get("I_REDPAY_P01_REDPAYGETORDER_1.0_DEF").getCode()'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'size()'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'put("1","2")'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'entrySet().iterator.{? #this.key=="1"}'
getstatic com.xxxx.xxxx.xxxxx.cache.ApiDefCache apiDefCache 'remove("1")'sc -d com.xxxx.xxxx.xxxxx.cache.ApiDefCache
ognl '@com.xxxx.xxxx.xxxxx.cache.ApiDefCache@apiDefCache'(不一定可以找到类,因为默认只会去SystemClassLoader里找类)
ognl -c 5cc69cfe '@com.xxxx.xxxx.xxxxx.cache.ApiDefCache@apiDefCache'getstatic com.xxxx.xxxx.xxxxx.util.ProxyUtil proxySelectFromDbSwitch
getstatic com.xxxx.xxxx.xxxxx.util.ProxyUtil proxySelectFromDbSwitch '#this.value=true'
ognl -c 5cc69cfe '@com.xxxx.xxxx.xxxxx.util.ProxyUtil@proxySelectFromDbSwitch'
ognl -c 5cc69cfe '#value=true, @com.xxxx.xxxx.xxxxx.util.ProxyUtil@proxySelectFromDbSwitch.value=#value'
2.6 查看实例对象的方法调用以及实例对象的属性
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send-- -x 指定输出结果的属性遍历深度
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -x 3-- 只查看入参
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params}"-- 只查看第一个入参
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params[0]}"-- 只查看第一个入参的code字段
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params[0].code}"-- 只查看第一个入参且入参的code字段值为payment
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '{params[0]}' '{params[0].code=="payment"}'-- 只查看方法所在类对象实例
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{traget}"-- 只查看方法所在类对象实例的defaultOkHttpClient字段
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{target.defaultOkHttpClient}" -x 3-- 只查看方法的返回结果
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{result}"-- 查看方法的入参,所在类实例,返回结果
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send "{params, target, result}"-- 在方法调用之前观察(返回值,异常均不存在)
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -b-- 在方法调用异常之后观察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -e-- 在方法返回之后观察(默认)
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -s-- 在方法结束之后观察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send -fAtEnter,AtExit,AtExceptionExit。对应函数入口,函数正常 return,函数抛出异常-- 当方法耗时超过200ms才进行观察
watch com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '{params, returnObj}' '#cost>200' -x 2
2.7 调整展示结果的形式为json
options json-format true
2.8 方法调用链路分析
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send '#cost>200'
2.9 将stack、watch等命令在后台执行并将结果输出到指定文件目录
stack com.xxxx.xxxx.xxxxx.communication.http.HttpClientInstance send >> stack.out & -- 查看
cat stack.out-- 查看后台执行的任务
jobs
-- 停止后台任务
kill <job-id>
-- 后台任务最多支持同时执行8个,默认一天后会超时自动关闭,可以通过options的job-timeout调整超时时间
2.10 容器内部线程分析
–查看最繁忙的前3个线程
thread -n 3
3. 高级用法
- 获取Spring context,然后调用bean执行方法等等:
- https://github.com/alibaba/arthas/issues/482
- 本地代码热部署到远程服务器:
- https://arthas.aliyun.com/doc/idea-plugin.html
4. 官方文档
https://arthas.aliyun.com/doc