Spring异步分离controller的线程让response在给出response之前不会等待

问题描述 投票:0回答:0

在放入项目之前使用@Async 在 spring boot 中尝试异步。

问题是控制器的响应。 如您所见,rest 模板响应时间的日志很短,但控制器的响应时间需要一些时间。 有没有办法让控制器也成为一个单独的线程,试图将@Async 放在控制器上但遗憾的是当我请求每件事都失败时因为已经有@Async 在服务中。

我使用 jMeter 请求 500 个用户并发。

任务执行器配置

@Bean("threadPoolTask")
    public TaskExecutor aSyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("Async-");
        executor.initialize();
        return executor;
    }

响应时间的控制器监听器

public class ResponseTimeAdvice  extends AbstractRequestLoggingFilter {

    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        long startTime = (Long) request.getAttribute("startTime");
        long duration = System.currentTimeMillis() - startTime;
        log.info("Response time for {} took {} ms", request.getRequestURI(), duration);
    }

    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        request.setAttribute("startTime", System.currentTimeMillis());
    }
}

重新设计服务

   @Async("threadPoolTask")
    public CompletableFuture<UrlCat> getCat2(){
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<?> httpEntity = new HttpEntity<>(httpHeaders);
             long startTime = System.currentTimeMillis();
            ResponseEntity<UrlCat> response = restTemplate.exchange(catUrl, HttpMethod.GET,httpEntity,UrlCat.class);
            long elapsedTime = System.currentTimeMillis() - startTime;
            log.info("Restemplate response time: {} ms", elapsedTime);
            return  CompletableFuture.completedFuture(response.getBody());

    }

使用 db 实现服务

   public CompletableFuture<UrlCat>  catApi5extend()  {
            CompletableFuture<UrlCat> completableFuture2 =   personApiService.getCat2().thenApply
                    (response -> catRepo.save(response) );
            return  CompletableFuture.completedFuture(completableFuture2.join());
    }

ServiceImpl 连接的控制器

    @GetMapping("/cat")
//    @Async("threadPoolTask")
    public CompletableFuture<UrlCat> catController(){
      CompletableFuture<UrlCat> urlCat =  catApi.catApi5extend();
       return CompletableFuture.completedFuture(urlCat.join());
    }

记录 500 个并发用户的 rest 模板和控制器的响应时间。

Rest模板:

Restemplate response time:
控制器:
Response time for /cat took

Start log

2023-04-04 21:26:10.639  INFO 50576 --- [        Async-4] c.e.C.test.CatApiService                 : Restemplate response time: 1327 ms
2023-04-04 21:26:10.639  INFO 50576 --- [        Async-3] c.e.C.test.CatApiService                 : Restemplate response time: 1328 ms
2023-04-04 21:26:10.639  INFO 50576 --- [        Async-2] c.e.C.test.CatApiService                 : Restemplate response time: 1327 ms
2023-04-04 21:26:10.651  INFO 50576 --- [        Async-1] c.e.C.test.CatApiService                 : Restemplate response time: 1340 ms
2023-04-04 21:26:10.754  INFO 50576 --- [io-8080-exec-87] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1458 ms
2023-04-04 21:26:10.754  INFO 50576 --- [nio-8080-exec-2] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1516 ms
2023-04-04 21:26:10.754  INFO 50576 --- [io-8080-exec-88] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1458 ms
2023-04-04 21:26:10.754  INFO 50576 --- [io-8080-exec-85] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1460 ms
2023-04-04 21:26:10.924  INFO 50576 --- [        Async-3] c.e.C.test.CatApiService                 : Restemplate response time: 225 ms
2023-04-04 21:26:10.924  INFO 50576 --- [        Async-2] c.e.C.test.CatApiService                 : Restemplate response time: 225 ms
2023-04-04 21:26:10.928  INFO 50576 --- [io-8080-exec-86] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1634 ms
2023-04-04 21:26:10.928  INFO 50576 --- [io-8080-exec-89] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1629 ms
2023-04-04 21:26:10.936  INFO 50576 --- [        Async-1] c.e.C.test.CatApiService                 : Restemplate response time: 237 ms
2023-04-04 21:26:10.937  INFO 50576 --- [        Async-4] c.e.C.test.CatApiService                 : Restemplate response time: 238 ms
2023-04-04 21:26:10.939  INFO 50576 --- [io-8080-exec-84] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1649 ms
2023-04-04 21:26:10.940  INFO 50576 --- [nio-8080-exec-4] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 1705 ms

没有放所有日志,太长了

End log

4-04 21:26:46.461  INFO 50576 --- [io-8080-exec-66] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14641 ms
2023-04-04 21:26:46.694  INFO 50576 --- [        Async-2] c.e.C.test.CatApiService                 : Restemplate response time: 234 ms
2023-04-04 21:26:46.694  INFO 50576 --- [        Async-1] c.e.C.test.CatApiService                 : Restemplate response time: 234 ms
2023-04-04 21:26:46.696  INFO 50576 --- [io-8080-exec-94] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14566 ms
2023-04-04 21:26:46.696  INFO 50576 --- [io-8080-exec-95] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14566 ms
2023-04-04 21:26:46.698  INFO 50576 --- [        Async-3] c.e.C.test.CatApiService                 : Restemplate response time: 238 ms
2023-04-04 21:26:46.700  INFO 50576 --- [io-8080-exec-93] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14569 ms
2023-04-04 21:26:46.703  INFO 50576 --- [        Async-4] c.e.C.test.CatApiService                 : Restemplate response time: 243 ms
2023-04-04 21:26:46.705  INFO 50576 --- [io-8080-exec-96] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14576 ms
2023-04-04 21:26:46.969  INFO 50576 --- [        Async-4] c.e.C.test.CatApiService                 : Restemplate response time: 265 ms
2023-04-04 21:26:46.969  INFO 50576 --- [        Async-3] c.e.C.test.CatApiService                 : Restemplate response time: 270 ms
2023-04-04 21:26:46.969  INFO 50576 --- [        Async-1] c.e.C.test.CatApiService                 : Restemplate response time: 274 ms
2023-04-04 21:26:46.969  INFO 50576 --- [        Async-2] c.e.C.test.CatApiService                 : Restemplate response time: 274 ms
2023-04-04 21:26:46.971  INFO 50576 --- [o-8080-exec-100] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14537 ms
2023-04-04 21:26:46.971  INFO 50576 --- [io-8080-exec-97] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14537 ms
2023-04-04 21:26:46.971  INFO 50576 --- [io-8080-exec-99] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14536 ms
2023-04-04 21:26:46.971  INFO 50576 --- [io-8080-exec-98] c.e.C.listener.ResponseTimeAdvice        : Response time for /cat took 14536 ms
java spring-boot asynchronous completable-future
© www.soinside.com 2019 - 2024. All rights reserved.