Arthas–基础–02–安装
1、准备
1.1、下载Arthas
cd /root/arthas
wget https://arthas.aliyun.com/arthas-boot.jar
1.2、下载测试demo和启动
cd /root/arthas
wget https://arthas.aliyun.com/math-game.jar
# 启动
java -jar math-game.jar
math-game
是一个简单的程序,每隔一秒生成一个随机数,再执行质因数分解,并打印出分解结果。
2、安装
2.1、安装Arthas
java -jar arthas-boot.jar
2.2、卸载Arthas
# 删除对应的目录就好
rm -rf /root/arthas
2.3、退出 arthas
命令 | 说明 |
---|---|
quit或者exit | 退出当前的连接,Attach 到目标进程上的 arthas 还会继续运行,端口会保持开放,下次连接时可以直接连接上。 |
stop | 完全退出 arthas |
3、附录
3.1、math-game源码
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* MathGame.<br>
* @author gongqiang <br>
* @version 1.0.0 2021年6月4日<br>
* @see
* @since JDK 1.5.0
*/
public class MathGame {
private static Random random = new Random();
private int illegalArgumentCount = 0;
public List<Integer> primeFactors(int number) {
if (number < 2) {
++this.illegalArgumentCount;
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
}
ArrayList<Integer> result = new ArrayList<Integer>();
int i = 2;
while (i <= number) {
if (number % i == 0) {
result.add(i);
number /= i;
i = 2;
continue;
}
++i;
}
return result;
}
public static void main(String[] args) throws InterruptedException {
MathGame game = new MathGame();
while (true) {
game.run();
TimeUnit.SECONDS.sleep(1L);
}
}
public void run() throws InterruptedException {
try {
int number = random.nextInt() / 10000;
List<Integer> primeFactors = this.primeFactors(number);
MathGame.print(number, primeFactors);
}
catch (Exception e) {
System.out.println(String.format("illegalArgumentCount:%3d,", this.illegalArgumentCount) + e.getMessage());
}
}
public static void print(int number, List<Integer> primeFactors) {
StringBuffer sb = new StringBuffer(number + "=");
for (int factor : primeFactors) {
sb.append(factor).append('*');
}
if (sb.charAt(sb.length() - 1) == '*') {
sb.deleteCharAt(sb.length() - 1);
}
System.out.println(sb);
}
}