带有 Spring Cloud 和 Eureka java.lang.IllegalStateException 的 Ribbon:没有可用于本地主机的实例 [英] Ribbon with Spring Cloud and Eureka java.lang.IllegalStateException: No instances available for localhost
本文介绍了带有 Spring Cloud 和 Eureka java.lang.IllegalStateException 的 Ribbon:没有可用于本地主机的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
我的主要课程:
@SpringBootApplication
//@Configuration
@ComponentScan(basePackages = "com.mypackage")
@EnableAutoConfiguration
@EnableEurekaClient
@EnableSwagger2
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
@LoadBalanced
@Bean(name="template")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
我的服务电话:
@Autowired
private RestTemplate template;
ResponseEntity<String> avs = template.exchange("http://localhost:7075/xyz/json/authenticate",HttpMethod.POST ,request,String.class);
抛出以下异常
java.lang.IllegalStateException: No instances available for localhost
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90)
at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:60)
at org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor$1.doWithRetry(RetryLoadBalancerInterceptor.java:48)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:276)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:157)
推荐答案
当您使用 @LoadBalanced
RestTemplate
时,主机名需要是 serviceId 而不是实际的主机名.在您的情况下,它正在尝试为 localhost
查找尤里卡记录,但找不到.有关如何使用多个 RestTemplate,请参阅文档
对象,一个负载均衡,一个没有.
When you use a @LoadBalanced
RestTemplate
the hostname needs to be a serviceId not an actual hostname. In your case, it's trying to find a eureka record for localhost
and can't find one. See the documentation for how to use multiple RestTemplate
objects, one load balanced, one not.
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate loadBalanced() {
return new RestTemplate();
}
@Primary
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
public class MyClass {
@Autowired
private RestTemplate restTemplate;
@Autowired
@LoadBalanced
private RestTemplate loadBalanced;
public String doOtherStuff() {
return loadBalanced.getForObject("http://stores/stores", String.class);
}
public String doStuff() {
return restTemplate.getForObject("http://example.com", String.class);
}
}
这篇关于带有 Spring Cloud 和 Eureka java.lang.IllegalStateException 的 Ribbon:没有可用于本地主机的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文