2016-04-06 23 views
4

Bahar çizme 1.3.3yay çizme Autowired DiscoveryClient RestTemplate UnknownHostException

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.3.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

Düzenleme benim bahar bulut bağımlılıkları için ben Brixton.RC1 kullanıyorum

kullanıyorum

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-dependencies</artifactId> 
      <version>Brixton.RC1</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

Uygulamam şu ek açıklamalarla ayarlanır:

@EnableDiscoveryClient 
@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

İçinde hizmetiyle bir dinlenme denetleyicisi vardır:

@RestController 
public class MyController { 

    @Autowired 
    private MyService myService; 

Ve Hizmetimde

başka eureka kayıtlı hizmete dışarı aramak için bir autowired dinlenme şablonu kullanmaya çalışıyorum.

@Service 
public class MyServiceImpl { 

    private final ParameterizedTypeReference<Answer> ANSWER = new ParameterizedTypeReference<Answer>() {}; 

    @Autowired 
    private DiscoveryClient discoveryClient; //just here for testing 

    @Autowired 
    private RestTemplate restTemplate; // 

    public String someCoolMethod(String input){ 
     log.info("Instance available? " + isInstanceAvailable()); 

     final RequestEntity<String> requestEntity = RequestEntity.post(URI.create("http://myotherservice/othermethod")).accept(MediaType.APPLICATION_JSON).body(input); 
     final ResponseEntity<String> response = this.restTemplate.exchange(requestEntity, ANSWER); 
     log.info(response); 
     return response.getBody(); 
    } 

    private Boolean isInstanceAvailable(){ 
     List<ServiceInstance> instances = discoveryClient.getInstances("myotherservice"); 
     for(ServiceInstance si : instances){ 
      log.info(si.getUri().toString()); 
     } 
     return instances.size() > 0; 
    } 

} 

Tamamen kafam karıştı çünkü bu başka bir projede çalışıyor. İşte çıktı:

INFO http://192.168.1.10:1234 
INFO Instance available? true 
ERROR I/O error on POST request for "http://myotherservice/othermethod": myotherservice; nested exception is java.net.UnknownHostException: myotherservice 

Her iki hizmetin de Eureka'ya kayıtlı olduğunu görebiliyorum. MyService yapabiliyor: 'myotherservice'

  • için Eureka
  • sorgu Eureka ile

    • sicil doğru, host ve port

    Ama autowired RestTemplate UnknownHostException atıyor ile geçerli bir yanıt almak.

  • +0

    Hangi Spring Cloud sürümünü kullanıyorsunuz? Yay bulut bağımlılarınızı gösterin. "RestTemplate" iniz, keşif için doğru şekilde yapılandırılmıyor. – spencergibb

    cevap

    12

    answer, RC1'den itibaren @LoadbalancedRestTemplate oluşturmanız gerekir.

    @Configuration 
    public class MyConfiguration { 
    
        @LoadBalanced 
        @Bean 
        RestTemplate restTemplate() { 
         return new RestTemplate(); 
        } 
    } 
    
    +0

    Çok teşekkür ederim! İki defa! – DaShaun