Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate configuration strategies to call multiple rest services from a single API

I have a scenario where there is an aggregate endpoint to call multiple downstream systems which are RESTful and gives back the consolidated response from all these systems.

I am currently using a rest template that is configured as a singleton bean and injects it to the corresponding services to make the rest call. The RestTemplate is using the default CloseableHttpClient as the HttpClient, which will close the connections once the request is successful.

Would this be a good approach or would it be better if the rest template is configured per service that is calling its RESTful service?

like image 734
Imran Mohammed Avatar asked Mar 07 '18 01:03

Imran Mohammed


People also ask

How do you call multiple REST API parallel in spring boot?

To do so, you have to do the following steps : Add @Async annotation to the function you want to parallelize getCountriesByLanguage and getCountriesByRegion. Change the return type of the function by CompletableFuture<List<Country>>

How do you call two methods simultaneously in spring boot?

If you make your search methods async, either using thread executors pool, or @Async , you will be able to invoke them at once, and gather results after finish. Just remember, that you will have to use Future for this.

How do I call API from RestTemplate?

Consuming PUT API by using RestTemplate - exchange() method Assume this URL http://localhost:8080/products/3 returns the below response and we are going to consume this API response by using Rest Template. Autowired the Rest Template Object. Use HttpHeaders to set the Request Headers.


2 Answers

RestTemplate is thread safe. You could use a pooling connection manager:

  @Bean
  public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
    PoolingHttpClientConnectionManager result = new PoolingHttpClientConnectionManager();
    result.setMaxTotal(20);  // FIXME Consider making this value configurable
    return result;
  }

  @Bean
  public RequestConfig requestConfig() {
    RequestConfig result = RequestConfig.custom()
      // FIXME Consider making these values configurable
      .setConnectionRequestTimeout(2000)
      .setConnectTimeout(2000)
      .setSocketTimeout(2000)
      .build();
    return result;
  }

  @Bean
  public CloseableHttpClient httpClient(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager, RequestConfig requestConfig) {
    CloseableHttpClient result = HttpClientBuilder
      .create()
      .setConnectionManager(poolingHttpClientConnectionManager)
      .setDefaultRequestConfig(requestConfig)
      .build();
    return result;
  }

  @Bean
  public RestTemplate restTemplate(HttpClient httpClient) {
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);
    return new RestTemplate(requestFactory);
  }

And also important, you might need to change RestTemplate's default settings based on observation / load tests, RestTemplate doesn't necessary use the whole pool to prevent a host from hijacking it.

You can read more at my blog Troubleshooting Spring's RestTemplate Requests Timeout

like image 171
ootero Avatar answered Oct 22 '22 16:10

ootero


From Spring Docs

RestTemplate

The RestTemplate is the central Spring class for client-side HTTP access. Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework and other portfolio projects. This means, for instance, that the RestTemplate is thread-safe once constructed, and that you can use callbacks to customize its operations.

Hence you can create your RestTemplate its safe to share with multiple threads invoking a REST call simultaneously.

You should also consider cost of creating and destroying an instance. If each thread or each rest call creates a dedicated RestTemplate it will hamper your apps performance.

Ref: https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

like image 33
Amit Phaltankar Avatar answered Oct 22 '22 15:10

Amit Phaltankar