2016-04-06 18 views
4

私は春ブーツ1.3.3春ブート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> 

編集私の春の雲の依存関係のため、私はBrixton.RC1を使用してい

を使用してい

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

My Applicationは次の注釈で設定されています。

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

私はそれでサービスと残りのコントローラがあります。

@RestController 
public class MyController { 

    @Autowired 
    private MyService myService; 

そして、私のサービスで、私は別のユーレカ登録されたサービスを呼び出すためにautowired残りのテンプレートを使用しようとしているが。

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

} 

私はこれが別のプロジェクトで働いているので、私は完全に混乱しています。出力もここにあります:

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 

私は両方のサービスがユーレカに登録されていることがわかります。 MyServiceでは、することができます: 'myotherservice'

  • ユーレカ
  • クエリユーレカと

    • レジスタの正しいホストとポート

    しかしautowired RestTemplateはUnknownHostExceptionがを投げているとの有効な応答を取得します。

  • +0

    使用しているSpring Cloudのバージョンは何ですか?あなたの春の雲の依存関係を表示する。あなたの 'RestTemplate'が検出のために正しく設定されていません。 – spencergibb

    答えて

    12

    answerは、RC1の時点で@LoadbalancedRestTemplateを作成する必要があります。

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

    ありがとうございます! 2回! – DaShaun

    関連する問題