skip to Main Content

I have multiple services running in docker containers through docker-compose.

I am trying to make a http request from one service to another but it fails since the port always gets filled as ’80’ instead of using the port registered with Eureka.

When I iterate through the discovered applications like so:

List<Application> applications = discoveryClient.getApplications().getRegisteredApplications();

   for (Application application : applications) {
       List<InstanceInfo> applicationsInstances = application.getInstances();
            for (InstanceInfo applicationsInstance : applicationsInstances) {

                String name = applicationsInstance.getAppName();
                String url = applicationsInstance.getHomePageUrl();
                System.out.println(name + ": " + url);
            }
        }

I get this (correct) output:

IDENTITY-SERVICE: http://172.20.0.3:10103/ 

But when the call gets made like this:

return webClientBuilder
                    .build()
                    .post()
                    .uri("http://identity-service/api/auth/validate", uriBuilder -> uriBuilder.queryParam("token", "xyz").build())
                    .retrieve()
                    .bodyToMono(ValidationResponseDTO.class);

this exception, indicating that it is calling port 80, is thrown:

org.springframework.web.reactive.function.client.WebClientRequestException: finishConnect(..) failed: Connection refused: identity-service/172.20.0.3:80

When I specify the port ("http://identity-service:10103/api/auth/validate") it works perfectly fine.

Anyone have an idea why it is not using the port from Eureka?

2

Answers


  1. Chosen as BEST ANSWER

    After a lot of testing I finally found the solution.

    I had not configured the WebClientBuilder correctly. After adding the LoadBalanced annotation to the Bean the service name gets correctly resolved.

    @Configuration
    public class WebClientConfiguration {
    
        @Bean
        @LoadBalanced
        public WebClient.Builder webClientBuilder(){
            return WebClient.builder();
        }
    
    }
    

  2. -> The issue you are facing is likely related to how you are constructing the URI for
    the HTTP request in your webClientBuilder. By default, when using the uri()
    method with a string argument, the WebClient will not take into account the port
    specified in the service URL obtained from Eureka, and it will use the default
    port, which is 80 for HTTP.

    -> Use the resolve() method of the URI object to add the specific path.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search