Spring Boot Web 应用哀求响应太慢排查
线上接口响应太慢,Spring Boot + 内嵌的 Tomcat 服务器,Tomcat 处理处罚哀求的最大线程数普通情况是 150 左右,最大是 200,以是当同时处理处罚的哀求过多,而且每个哀求不停没有处理处罚完成。全部的线程都在繁忙,没有办法处理处罚新的哀求,就会导致新的哀求排队等候处理处罚,从而造成了迟迟无法响应的线上变乱,用户体验太差。
场景复现
一个简单的 controller 接口
- @RestController
- public class SpringBootController {
- @GetMapping("/")
- public String index() throws InterruptedException {
- // 每个线程都睡眠,模拟业务接口响应慢的问题
- Thread.sleep(60 * 30 * 1000);
- System.out.println("current thread: " + Thread.currentThread().getName());
- return "Hello Spring Boot!";
- }
- }
复制代码 随后设置 tomcat 服务器的最大线程为 5 :
- server:
- port: 8080
- tomcat:
- threads:
- max: 5
- spring:
- application:
- name: simple-application
-
复制代码 测试
之后我们在单元测试中模拟 10 个线程对接口发起哀求
- public class RequestTest {
- public static void main(String[] args) {
-
- for (int i = 0; i < 10; i++) {
- new Thread(new RequestTask()).start();
- }
- }
- private static class RequestTask implements Runnable {
- @Override
- public void run() {
- try {
- URL url = new URL("http://localhost:8080/");
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- int responseCode = connection.getResponseCode();
- System.out.println("Response Code: " + responseCode);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
复制代码 首先,我们解释掉 controller 中的就寝代码,运行测试得到以下的输出:
- spring boot web 应用响应输出:
- 2024-08-01T15:56:25.369+08:00 INFO 7304 --- [simple-application] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
- current thread: http-nio-8080-exec-4
- current thread: http-nio-8080-exec-5
- current thread: http-nio-8080-exec-1
- current thread: http-nio-8080-exec-2
- current thread: http-nio-8080-exec-3
- current thread: http-nio-8080-exec-5
- current thread: http-nio-8080-exec-2
- current thread: http-nio-8080-exec-4
- current thread: http-nio-8080-exec-1
- current thread: http-nio-8080-exec-3
-
- 测试输出:
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
- Response Code: 200
复制代码 我们看到线程在响应输出。接下来,打开解释代码,模拟变乱现场。将会在控制台看不到任何输出,测试方法也会卡住不动。
排查
我们利用 JVM 的一些命令来举行排查:
- # 首先查看 java 的进程 id
- C:\Users\Administrator>jps -l
- 18400 indi.yuluo.tomcat.SpringBootApplication
- 10324 jdk.jcmd/sun.tools.jps.Jps
- 16872 org.jetbrains.jps.cmdline.Launcher
- 18072 org.jetbrains.idea.maven.server.RemoteMavenServer36
- 9624
- 7372 inid.yuluo.tomcat.RequestTest
复制代码 我们看到 SpringBootApplication 和 RequestTest 在运行,之后利用 jstack 命令生成线程快照,并保存为文件。
- C:\Users\Administrator>jstack 18400 > C:\Users\Administrator\Desktop\spring-boot-thread_dump.txt
复制代码 打开线程快照文件,搜索 http-nio 就能看到 Tomcat 的哀求处理处罚线程,全部的哀求处理处罚线程状态都是 TIMED_WAITING ,表示线程正在等候另一个线程实行特定的动作,但是有一个指定的等候时间。而且能直接看到哀求是壅闭在了哪个代码位置。
- "http-nio-8080-exec-2" #28 daemon prio=5 os_prio=0 cpu=0.00ms elapsed=187.14s tid=0x0000015ffe6089f0 nid=0x1ee4 waiting on condition [0x00000026eb5fc000]
- # `TIMED_WAITING`
- java.lang.Thread.State: TIMED_WAITING (sleeping)
- at java.lang.Thread.sleep(java.base@17.0.1/Native Method)
- # 阻塞位置:
- at indi.yuluo.tomcat.controller.SpringBootController.index(SpringBootController.java:18)
复制代码 解决方案
适当修改 Tomcat 的最大线程数,可以增加并发哀求的处理处罚能力。
适当调大 Tomcat 的最小空闲线程数,可以确保在并发高峰时刻,Tomcat 能敏捷响应新的哀求,而不需要重新创建线程。
修改值需要对用户体量做出预估之后,举行测试之后确定。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |