spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException

2023-12-20

我正在使用 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

我正在使用 Brixton.RC1 作为我的 Spring Cloud 依赖项

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

我的应用程序是使用这些注释设置的:

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

我有一个带有服务的休息控制器:

@RestController
public class MyController {

    @Autowired
    private MyService myService;

在我的服务中,我尝试使用自动连接的休息模板来调用另一个尤里卡注册服务。

@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

所以我可以看到这两个服务都注册到了 Eureka。 MyService 能够:

  • 向尤里卡注册
  • 查询尤里卡“myotherservice”
  • 使用正确的主机和端口获得有效响应

但自动装配的 RestTemplate 抛出 UnknownHostException。


The answer https://github.com/spring-cloud/spring-cloud-commons/blob/master/docs/src/main/asciidoc/spring-cloud-commons.adoc#spring-resttemplate-as-a-load-balancer-client你需要创建一个@Loadbalanced RestTemplate自 RC1 起。

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException 的相关文章

随机推荐