2017-01-10 1 views
1

에 사용할 수있는 인스턴스 내가 사용하고리본 : 로컬 호스트

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

다음 예외가 throw됩니다.

java.lang.IllegalStateException : 로컬 호스트 에 사용할 수있는 인스턴스 org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute (RibbonLoadBalancerClient.java:90)에서 org.springframework.cloud.client.loadbalancer에서 org.springframework.retry.support.RetryTemplate.doExecute에서 org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor $ 1.doWithRetry (RetryLoadBalancerInterceptor.java:48) 에서 .RetryLoadBalancerInterceptor $ 1.doWithRetry (RetryLoadBalancerInterceptor.java:60) (RetryTemplate.java:276) at org.springframework.retry.support.RetryTemplate. 실행 (RetryTemplate.java:157)

답변

2

당신이 @LoadBalancedRestTemplate 호스트 이름이 서비스 id 아닌 실제 호스트 이름이어야합니다 사용합니다. 귀하의 케이스에서, 그것은 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 클라우드를 사용하는 동안 Restowemplate을 Autowire하려고하면 문제가 발생합니다. 그러나 해결 방법을 찾았습니다. 먼저 새로운 @Component 클래스를 선언하고 그 안에 RestTemplate을 반환하는 방법 작성 :

@Component 
public class RestTemplateComponentFix{ 

@Autowired 
SomeConfigurationYouNeed someConfiguration; 

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

} 

당신이 나머지 템플릿이 restTemplate() 메서드를 호출해야 할 때 그냥 클래스와 경우에 restTemplateComponentFix을 자동으로 묶어 후를. 이런 식으로 뭔가가 :

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