2017-01-10 3 views
1

のために利用可能なインスタンス私が使用していますリボン:localhostの

<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: でローカルホストのために利用可能なインスタンスorg.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90) org.springframework.cloud.client.loadbalancerで.RetryLoadBalancerInterceptor $ 1.doWithRetry(RetryLoadBalancerInterceptor.java:60) でorg.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor $ 1.doWithRetry(RetryLoadBalancerInterceptor.java:48) org.springframework.retry.support.RetryTemplate.doExecuteで(RetryTemplate.java:276) at org.springframework.retry.support.RetryTemplate。実行(RetryTemplate.java:157)

答えて

2

あなたは@LoadBalancedRestTemplateホスト名がいるServiceIDない実際のホスト名にする必要があります。あなたのケースでは、それはlocalhostのユーレカレコードを見つけようとしていて、それを見つけることができません。複数のRestTemplateオブジェクトを使用する方法については、the documentationを参照してください。一方は負荷分散、もう一方は負荷分散です。

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

おかげで@spencergibb、もう一つ、私がネットフリックス(1.0.3)との通知があり、このような何か残りのテンプレートLoadBalancedを作成する必要がありますが、1.2.3ではそれが存在しません。 – atiwari54

+0

いいえ、そこにあります。おそらくあなたの設定に何か問題があります。 – spencergibb

1

このNetflixクラウドを使用しているときにRestTemplateを自動起動しようとすると、何か問題が発生します。しかし、私は回避策を見つけました。あなたは残りの部分テンプレートがrestTemplate()メソッドを呼び出す必要があるときにちょうどその時、あなたのクラスでrestTemplateComponentFixをAutowireし、その後

@Component 
public class RestTemplateComponentFix{ 

@Autowired 
SomeConfigurationYouNeed someConfiguration; 

@LoadBalanced 
public RestTemplate getRestTemplate() { 
     // TODO set up your restTemplate 
     rt.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); 
     return rt; 
    } 

} 

:まず新しい@Componentクラスを宣言し、それに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);