将请求发送到Spring服务器而无需等待响应

时间:2020-08-19 17:51:04

标签: spring rest asynchronous service resttemplate

我正在编写两个进行一些计算的服务。其中之一必须仅向第二个服务发送大量请求,而无需等待响应:

...
for (String url: oneService.getUrlList()) {
    try {
       sendRequests(restTemplate, url);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
}
...

@Async
void sendRequests(RestTemplate restTemplate, String url) throws InterruptedException {
   restTemplate.postForLocation("http://second-servive/calculation/" + url, String.class);
}

我在第二个服务的控制器中:

@RequestMapping(value = "calculation/{url}", method = RequestMethod.POST)
public void getShortCircuitCalculation(@PathVariable String url) {
     secondService.getCalculation(url);
}

我使用eureka服务器创建了第二个服务的许多实例,以加快计算速度。但是,使用 @Async 批注和第二个服务的实例需要很多时间,就像单个实例一样。第一服务仍在等待第二服务的响应。我尝试在单独的线程中发送响应,但是得到了相同的结果。

有什么方法可以不等待第二项服务的响应并继续发送响应,以便实例进行计算?

0 个答案:

没有答案