Arthas redefine class case tutorial #847 (#1339)

This commit is contained in:
Hollow Man 2020-08-12 19:31:54 +08:00 committed by GitHub
parent 4045d57498
commit 7354d85c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 361 additions and 0 deletions

View File

@ -0,0 +1,16 @@
在新的`Terminal 2`里,下载`arthas-boot.jar`,再用`java -jar`命令启动:
`wget https://arthas.aliyun.com/doc/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot`是`Arthas`的启动程序它启动后会列出所有的Java进程用户可以选择需要诊断的目标进程。
选择第一个进程,输入 `1`{{execute T2}} ,再`Enter/回车`
Attach成功之后会打印Arthas LOGO。输入 `help`{{execute T2}} 可以获取到更多的帮助信息。
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

View File

@ -0,0 +1,85 @@
下面介绍通过`jad`/`mc`/`redefine` 命令实现动态更新代码的功能。
目前,访问 http://localhost/user/0 会返回500异常
`curl http://localhost/user/0`{{execute T3}}
```
{"timestamp":1550223186170,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"id < 1","path":"/user/0"}
```
下面通过热更新代码,修改这个逻辑。
### jad反编译UserController
`jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java`{{execute T2}}
jad反编译的结果保存在 `/tmp/UserController.java`文件里了。
再打开一个`Terminal 3`然后用vim来编辑`/tmp/UserController.java`
`vim /tmp/UserController.java`{{execute T3}}
比如当 user id 小于1时也正常返回不抛出异常
```java
@GetMapping(value={"/user/{id}"})
public User findUserById(@PathVariable Integer id) {
logger.info("id: {}", (Object)id);
if (id != null && id < 1) {
return new User(id, "name" + id);
// throw new IllegalArgumentException("id < 1");
}
return new User(id.intValue(), "name" + id);
}
```
### sc查找加载UserController的ClassLoader
`sc -d *UserController | grep classLoaderHash`{{execute T2}}
```bash
$ sc -d *UserController | grep classLoaderHash
classLoaderHash 1be6f5c3
```
可以发现是 spring boot `LaunchedURLClassLoader@1be6f5c3` 加载的。
请记下你的classLoaderHash后面需要使用它。在这里它是 `1be6f5c3`
注意请使用你的classLoaderHash值覆盖 `<classLoaderHash>` ,然后手动执行下面所有所述命令:
### mc
保存好`/tmp/UserController.java`之后,使用`mc`(Memory Compiler)命令来编译,并且通过`-c`参数指定ClassLoader
`mc -c <classLoaderHash> /tmp/UserController.java -d /tmp`
```bash
$ mc -c 1be6f5c3 /tmp/UserController.java -d /tmp
Memory compiler output:
/tmp/com/example/demo/arthas/user/UserController.class
Affect(row-cnt:1) cost in 346 ms
```
### redefine
再使用`redefine`命令重新加载新编译好的`UserController.class`
`redefine /tmp/com/example/demo/arthas/user/UserController.class`{{execute T2}}
```
$ redefine /tmp/com/example/demo/arthas/user/UserController.class
redefine success, size: 1
```
### 热修改代码结果
`redefine`成功之后,再次访问 https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/0 ,结果是:
```
{
"id": 0,
"name": "name0"
}
```

View File

@ -0,0 +1,10 @@
通过本教程基本掌握了Arthas热更新代码。更多高级特性可以在下面的进阶指南里继续了解。
* [Arthas进阶](https://arthas.aliyun.com/doc/arthas-tutorials?language=cn&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas 文档](https://arthas.aliyun.com/doc/)
欢迎关注公众号获取Arthas项目的信息源码分析案例实践。
![Arthas公众号](/arthas/scenarios/common-resources/assets/qrcode_gongzhonghao.jpg)

View File

@ -0,0 +1,45 @@
{
"title": "Arthas 热更新代码 案例",
"description": "Arthas 热更新代码 案例",
"difficulty": "高级使用者",
"time": "10分钟",
"details": {
"steps": [
{
"title": "Start demo",
"text": "start-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "热更新代码",
"text": "case-jad-mc-redefine.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
},
"assets": {
"host01": []
}
},
"environment": {
"uilayout": "terminal",
"showdashboard": true,
"dashboards": [
{
"name": "Web Port 80",
"port": 80
}
]
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

View File

@ -0,0 +1,11 @@
![Arthas](https://arthas.aliyun.com/doc/_images/arthas.png)
`Arthas` 是Alibaba开源的Java诊断工具深受开发者喜爱。在线排查问题无需重启动态跟踪Java代码实时监控JVM状态。
`Arthas` 支持JDK 6+支持Linux/Mac/Windows采用命令行交互模式同时提供丰富的 `Tab` 自动补全功能,进一步方便进行问题的定位和诊断。
* Github: https://github.com/alibaba/arthas
* 文档: https://arthas.aliyun.com/doc/

View File

@ -0,0 +1,14 @@
下载`demo-arthas-spring-boot.jar`,再用`java -jar`命令启动:
`wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar`{{execute T1}}
`demo-arthas-spring-boot`是一个很简单的spring boot应用源代码[查看](https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-arthas-spring-boot)
启动之后可以访问80端口 https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com
![Demo Web](/arthas/scenarios/common-resources/assets/demo-web.png)

View File

@ -0,0 +1,16 @@
In the new `Terminal 2`, download `arthas-boot.jar` and start with the `java -jar` command:
`wget https://arthas.aliyun.com/doc/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot` is the launcher for `Arthas`. It lists all the Java processes, and the user can select the target process to be diagnosed.
Select the first process, type `1`{{execute T2}} then type `Enter`
After the Attach is successful, Arthas LOGO is printed. Enter `help`{{execute T2}} for more help.
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

View File

@ -0,0 +1,88 @@
This case introduces the ability to dynamically update code via the `jad`/`mc`/`redefine` command.
Currently, visiting http://localhost/user/0 will return a 500 error:
`curl http://localhost/user/0`{{execute T3}}
```
{"timestamp":1550223186170,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"id < 1","path":"/user/0"}
```
This logic will be modified by `redefine` command below.
### Use jad command to decompile UserController
`jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java`{{execute T2}}
The result of jad command will be saved in the `/tmp/UserController.java` file.
Then open `Terminal 3`, use `vim` to edit `/tmp/UserController.java`:
`vim /tmp/UserController.java`{{execute T3}}
For example, when the user id is less than 1, it also returns normally without throwing an exception:
```java
@GetMapping(value={"/user/{id}"})
public User findUserById(@PathVariable Integer id) {
logger.info("id: {}", (Object)id);
if (id != null && id < 1) {
return new User(id, "name" + id);
// throw new IllegalArgumentException("id < 1");
}
return new User(id.intValue(), "name" + id);
}
```
### Use sc command to find the ClassLoader that loads the UserController
`sc -d *UserController | grep classLoaderHash`{{execute T2}}
```bash
$ sc -d *UserController | grep classLoaderHash
classLoaderHash 1be6f5c3
```
It can be found that it is loaded by spring boot `LaunchedURLClassLoader@1be6f5c3`.
Please write down your classLoaderHash here, in the case here, it's `1be6f5c3`. It will be used in the future steps.
Note: Please replace `<classLoaderHash>` with your classLoaderHash above, then execute the commands manually in the following steps:
### mc
After saving `/tmp/UserController.java`, compile with the `mc` (Memory Compiler) command and specify the ClassLoader with the `-c` option:
`mc -c <classLoaderHash> /tmp/UserController.java -d /tmp`
```bash
$ mc -c 1be6f5c3 /tmp/UserController.java -d /tmp
Memory compiler output:
/tmp/com/example/demo/arthas/user/UserController.class
Affect(row-cnt:1) cost in 346 ms
```
### redefine
Then reload the newly compiled `UserController.class` with the `redefine` command:
`redefine /tmp/com/example/demo/arthas/user/UserController.class`{{execute T2}}
```
$ redefine /tmp/com/example/demo/arthas/user/UserController.class
redefine success, size: 1
```
### Check the results of the hotswap code
After the `redefine` command is executed successfully, visit https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/0 again.
The result is:
```
{
"id": 0,
"name": "name0"
}
```

View File

@ -0,0 +1,6 @@
Through this tutorial, you can know how to Hotswap code. More advanced features can be found in the Advanced Guide below.
* [Arthas Advanced](https://arthas.aliyun.com/doc/arthas-tutorials?language=en&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas Documentation](https://arthas.aliyun.com/doc/en)

View File

@ -0,0 +1,45 @@
{
"title": "Arthas Hotswap code",
"description": "Arthas Hotswap code",
"difficulty": "expert",
"time": "10 minutes",
"details": {
"steps": [
{
"title": "Start demo",
"text": "start-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "Hotswap code",
"text": "case-jad-mc-redefine.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
},
"assets": {
"host01": []
}
},
"environment": {
"uilayout": "terminal",
"showdashboard": true,
"dashboards": [
{
"name": "Web Port 80",
"port": 80
}
]
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

View File

@ -0,0 +1,11 @@
![Arthas](https://arthas.aliyun.com/doc/_images/arthas.png)
`Arthas` is a Java diagnostic tool open-sourced by Alibaba middleware team. Arthas helps developers in trouble-shooting issues in production environment for Java based applications without modifying code or restarting servers.
`Arthas` supports JDK 6+, supports Linux/Mac/Windows.
* Github: https://github.com/alibaba/arthas
* Documentation: https://arthas.aliyun.com/doc/en

View File

@ -0,0 +1,14 @@
Download `demo-arthas-spring-boot.jar`, and start with `java -jar` command:
`wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar`{{execute T1}}
`demo-arthas-spring-boot` is a simple Spring Boot demo, the source code here: [View](https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-arthas-spring-boot)
After booting, access port 80: https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com
![Demo Web](/arthas/scenarios/common-resources/assets/demo-web.png)