SpringBoot在启动后,首次调用接口的时候是比较慢的,造成这种结果的原因是 DispatcherServlet 没有预热的原因,在SpringBoot启动的时候 DispatcherServlet 并没有进行初始化,而在第一次接口请求的时候,才会进行初始化操作,我们来看一个简单的小Demo
@Slf4j
@RestController
public class TAPI {
@GetMapping(value = "/t1", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity ttt() {
log.info("接收到请求");
return new ResponseEntity("SUCCESS", HttpStatus.OK);
}
}
本地调用接口后,日志打印
2021-09-07 23:31:32.976 INFO 18580 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-09-07 23:31:32.977 INFO 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-09-07 23:31:32.977 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2021-09-07 23:31:32.977 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2021-09-07 23:31:32.977 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2021-09-07 23:31:32.978 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@498403ab
2021-09-07 23:31:32.979 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@6ba46e2
2021-09-07 23:31:32.980 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-09-07 23:31:32.980 INFO 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
2021-09-07 23:31:32.993 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : GET "/t2", parameters={}
2021-09-07 23:31:33.002 DEBUG 18580 --- [ XNIO-1 task-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.hjx.controller.TAPI2#ttt()
2021-09-07 23:31:33.038 DEBUG 18580 --- [ XNIO-1 task-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json]
2021-09-07 23:31:33.039 DEBUG 18580 --- [ XNIO-1 task-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["SUCCESS"]
2021-09-07 23:31:33.060 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
再次请求接口之后的日志
2021-09-07 23:34:07.961 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : GET "/t2", parameters={}
2021-09-07 23:34:07.962 DEBUG 18580 --- [ XNIO-1 task-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.hjx.controller.TAPI2#ttt()
2021-09-07 23:34:07.963 DEBUG 18580 --- [ XNIO-1 task-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json]
2021-09-07 23:34:07.963 DEBUG 18580 --- [ XNIO-1 task-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["SUCCESS"]
2021-09-07 23:34:07.964 DEBUG 18580 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2次请求之间的操作还是很明显的,那么怎么解决这个问题呢,我大概找了下网上的帖子,基本就3种,下面我列一下和网上给出的解决方案
1、dispatcherServlet 是懒加载的
解决办法:添加配置 spring.mvc.servlet.load-on-startup=1
2、数据库链接是懒加载的
解决方法:预加载连接池
3、linux 下真随机数生成器
解决方案:修改JDK配置 securerandom.source=file:/dev./urandom(该配置文件不同版本JDK位置不同,各位可以百度google)
网上提到的这几种方法,我都试了一遍,都没有作用,尤其是第一条,发现那个配置根本就没什么作用,第3条处理方法,试了后也没啥反应,基本可以确定网上流传最多的这2个思路都没什么卵用,都是一个抄一个
其实解决这个问题最靠谱的方案就是对接口做预加载,就是SpringBoot启动后,先调用一次接口,把 DispatcherServlet 预加载进去就可以了,以下是写的一个简单的预加载类,大家可以参考一下
@Slf4j
@Configuration
public class LoadDatabase {
@Bean
CommandLineRunner initController(RestTemplate restTemplate) {
return args -> {
log.info("REQUEST " + restTemplate.execute("http://127.0.0.1:9999/t1", HttpMethod.GET, null, null));
};
}
}
之后调用接口就正常了,这里的预加载解决方案不光可以解决第一次调用请求慢的问题,我们还可以把系统需要的一些常用数据预加载到缓冲或者内存里面,从而加快系统的整体响应速度
转载
SpringBoot重启后首次接口请求慢的问题
https://blog.csdn.net/gnf_cc/article/details/120169568