spring-bootspring-integrationspring-cloud-loadbalancer

how to do client side load balancing in spring integration


we can add client-side load balancing in spring boot applications by,

@Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        final RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }

This will take care of the microservice resolution as well. ie. identifying service by the URL like "http://service_name/api/v1/endpoint/".

Is there any similar mechanism for name resolution in Spring integration?


Solution

  • See this ctor for Spring Integration HTTP Outbound Channel Adapter:

    /**
     * Create a handler that will send requests to the provided URI using a provided RestTemplate
     * @param uri The URI.
     * @param restTemplate The rest template.
     */
    public HttpRequestExecutingMessageHandler(String uri, RestTemplate restTemplate) {
    

    So, when you configure a @ServiceActivator (handle() in Java DSL), you just inject your load-balanced RestTemplate and everything should work as expected.