记录一次线上服务器出现cpu占用过高,避免之后再次出现相关问题,这里记录一下
用代码模拟cpu 占用过高进行排查
新建一个web项目,这里以springboot为例子新建一个web项目
package com.wanjun.jvm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class JvmApplication {
public static void main(String[] args) {
SpringApplication.run(JvmApplication.class, args);
}
//这里写个死循环 模拟 cpu 占用
@RequestMapping("/cpu")
public String testCpu() {
System.out.println("request cpu");
while (true) {
}
}
}
将项目部署到linux 服务器上
启动项目
访问
curl localhost:8080/cpu
排查
查看占用cpu高的进程
top
如下图:可看出PID为4060的java进程占用cpu最高,达到了99.5%
查看子进程情况:
top -p 4606 -H
将 子进程id 转换成16进制
printf "%x \n" 4648
查询具体出现问题的代码位置
jstack 4606|grep 1228 -C 30
如下图:可看出是JvmApplication类的第19行出现问题