spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException

流过昼夜 提交于 2020-01-02 06:52:10

问题


I'm using spring boot 1.3.3

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

Edit

I'm using Brixton.RC1 for my spring cloud dependencies

<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 is setup with these annotations:

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

I have a rest controller with service in it:

@RestController
public class MyController {

    @Autowired
    private MyService myService;

And in my service I'm trying to use an autowired rest template to call out to another eureka registered service.

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

}

I'm completely confused because I have this working in another project. Here is the output too:

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

So I can see that both services are registered with Eureka. MyService is able to:

  • register with Eureka
  • query Eureka for 'myotherservice'
  • get a valid response with correct host and port

But the autowired RestTemplate is throwing UnknownHostException.


回答1:


The answer is you need to create a @Loadbalanced RestTemplate as of RC1.

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}


来源:https://stackoverflow.com/questions/36458975/spring-boot-autowired-discoveryclient-resttemplate-unknownhostexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!