If you really omit the microservice path, note that creating an HTTP client for each request and executing synchronous blocking requests will not really scale.
Here are some ideas:
Create one instance of RestTemplate
You can create a single instance of RestTemplate and enter it in several places in your application.
@Configuration public class MyConfiguration { @Bean public RestTemplate restTemplate() { return new RestTemplate(new HttpComponentsClientHttpRequestFactory()); } }
Better wrap this REST client cache
Check out Sagan will take it .
Better yet, go asynchronously
You can use AsyncRestTemplate ; very useful, especially if your controllers need to complete several requests, and if you do not want to block the webapp stream. Your controller may even return DeferredResult or ListenableFuture , which will make your webapp more scalable.
And Spring Cloud + Netflix
You can also check out Spring Cloud and Spring Cloud Netflix . You will see interesting features: load balancing, recovery, circuit breakers, etc.
source share