HTTP请求使用Eureka时未填充端口。

huangapple go评论75阅读模式
英文:

HTTP request with Eureka does not fill port

问题

我在Docker容器中使用Docker Compose运行多个服务。

我尝试从一个服务向另一个服务发起HTTP请求,但它失败了,因为端口始终被设置为'80',而不是使用Eureka注册的端口。

当我像这样遍历发现的应用程序时:

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);
    }
}

我得到了这个(正确的)输出:

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

但当调用像这样进行时:

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

会抛出这个异常,指示它正在调用端口80:

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

当我指定端口("http://identity-service:10103/api/auth/validate")时,它工作得非常好。

有人知道为什么它不使用Eureka中的端口吗?

英文:

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&lt;Application&gt; applications = discoveryClient.getApplications().getRegisteredApplications();

   for (Application application : applications) {
       List&lt;InstanceInfo&gt; applicationsInstances = application.getInstances();
            for (InstanceInfo applicationsInstance : applicationsInstances) {

                String name = applicationsInstance.getAppName();
                String url = applicationsInstance.getHomePageUrl();
                System.out.println(name + &quot;: &quot; + 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(&quot;http://identity-service/api/auth/validate&quot;, uriBuilder -&gt; uriBuilder.queryParam(&quot;token&quot;, &quot;xyz&quot;).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?

答案1

得分: 0

-> 您遇到的问题可能与您在webClientBuilder中构建HTTP请求的URI方式有关。默认情况下,使用字符串参数调用uri()方法时,WebClient将不考虑从Eureka获取的服务URL中指定的端口,而会使用默认端口,即HTTP的80端口。

-> 使用URI对象的resolve()方法来添加特定路径

英文:

-> 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.

答案2

得分: 0

经过大量测试,我最终找到了解决方法。

我没有正确配置WebClientBuilder。在将LoadBalanced注解添加到Bean之后,服务名称得到了正确解析。

@Configuration
public class WebClientConfiguration {

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

}
英文:

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();
    }

}

huangapple
  • 本文由 发表于 2023年7月27日 23:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定