英文:
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<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?
答案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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论