Eureka 服务器和 UnknownHostException [英] Eureka server and UnknownHostException
问题描述
我已经安装了一个 Eureka 服务器并注册了一个叫做定价服务的服务.Eureka 仪表板显示UP 定价服务:4ac78ca47bdbebbb5fec98345c6232af0状态下.
I have installed an Eureka server and have registered a service called pricing-service. The Eureka dashboard shows UP pricing-service:4ac78ca47bdbebb5fec98345c6232af0 under status.
现在我有一个完全独立的 Spring Boot Web 服务,它调用(通过 WebClient 实例)定价服务为 http://pricing-service 但我得到 "reactor.core.Exceptions$ReactiveException: java.net.UnknownHostException:没有这样的主机是已知的(定价服务)"例外.
Now I have a completely separate Spring boot web service which calls (through a WebClient instance) the pricing-service as http://pricing-service but I get "reactor.core.Exceptions$ReactiveException: java.net.UnknownHostException: No such host is known (pricing-service)" exception.
所以控制器无法通过主机名找到定价服务.此外,控制器如何知道 Eureka 服务器以获取定价服务?Web 服务的 application.properties 中不应该有对它的引用吗?我在网上找不到任何内容.
So the Controller can't find the pricing-service by hostname.Further, how is the controller aware of the Eureka server in order to get to pricing-service? Shouldn't there be a reference to it in the application.properties of the web service? I couldn't find anything around the web.
推荐答案
现在我有一个完全独立的 Spring Boot Web 服务,它调用(通过 WebClient 实例)定价服务为 http://pricing-service
Now I have a completely separate Spring boot web service which calls (through a WebClient instance) the pricing-service as http://pricing-service
- 您的此单独服务(
WebClient
服务)还必须向Eureka Server
注册. - 默认情况下,
webclient
不知道必须使用load-balancer
来调用其他eureka 实例
.
- This separate service (the
WebClient
Service) of yours must also register itself withEureka Server
. - By default,
webclient
is not aware of having to useload-balancer
to make calls to othereureka instances
.
这是启用此类 WebClient
bean 的一种方法:
Here is one of the ways to enable such a WebClient
bean:
@Configuration
public class MyBeanConfig {
@Bean
WebClient webClient(LoadBalancerClient lbClient) {
return WebClient.builder()
.filter(new LoadBalancerExchangeFilterFunction(lbClient))
.build();
}
}
然后,您可以使用此 webClient
bean 进行调用:
Then, you can use this webClient
bean to make calls as:
@Component
public class YourClient {
@Autowired
WebClient webClient;
public Mono<ResponseDto> makeCall() {
return webClient
.get()
.uri("http://pricing-service/")
// <-- change your body and subscribe to result
}
注意:初始化WebClient
的Bean
可以进一步探索这里.
Note: Initializing a Bean
of WebClient
can be explored further here.
这篇关于Eureka 服务器和 UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!