【问题标题】:Ribbon with Spring Cloud and Eureka java.lang.IllegalStateException: No instances available for localhost带有 Spring Cloud 和 Eureka java.lang.IllegalStateException 的功能区:没有可用于 localhost 的实例
【发布时间】:2017-05-24 23:39:14
【问题描述】:

我正在使用

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

【问题讨论】:

    标签: spring-cloud spring-cloud-netflix netflix-ribbon


    【解决方案1】:

    当您使用 @LoadBalanced RestTemplate 时,主机名必须是 serviceId 而不是实际的主机名。在您的情况下,它正在尝试查找 localhost 的尤里卡记录,但找不到。请参阅the documentation 了解如何使用多个RestTemplate 对象,一个负载均衡,一个不负载。

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

    【讨论】:

    • 感谢@spencergibb,我还注意到 netflix(1.0.3) 需要制作 Rest 模板 LoadBalanced,但在 1.2.3 中它不存在。
    • 不,它就在那里。您的配置可能有问题。
    【解决方案2】:

    根据我的阅读,当您在使用此 Netflix 云时尝试 Autowire RestTemplate 时会出现某种问题。但是我找到了解决方法。首先声明一个新的@Component 类,并在其中创建一个返回 RestTemplate 的方法:

    @Component
    public class RestTemplateComponentFix{
    
     @Autowired
     SomeConfigurationYouNeed someConfiguration;
    
     @LoadBalanced
     public RestTemplate getRestTemplate() {
           // TODO set up your restTemplate
            rt.setRequestFactory( new HttpComponentsClientHttpRequestFactory() );
            return rt;
        }
    
    }
    

    之后,只需在您的类中自动装配 restTemplateComponentFix,当您需要其余模板时,调用 restTemplate() 方法。像这样的:

    @Service
    public class someClass{
    
        @Autowired
        RestTemplateComponentFix restTemplateComponentFix;
    
        public void methodUsingRestTemplate(){
            // Some code...
            RestTemplate rt = restTemplateComponentFix.getRestTemplate();
            // Some code...
        }
    }
    

    很酷的部分是,您可以使用以下代码轻松地对该代码进行单元测试:

    RestTemplate rt = Mockito.mock(RestTemplate.class) 
    when(restTemplateComponentFix.getRestTemplate()).thenReturn(rt);
    when(rt.someMethod()).thenReturn(something);
    

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 2015-12-12
      • 2020-06-26
      • 2016-07-17
      • 1970-01-01
      相关资源
      最近更新 更多