spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException [英] spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException
问题描述
我使用的是 Spring Boot 1.3.3
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>
编辑
我正在使用 Brixton.RC1 作为我的 Spring Cloud 依赖项
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;
在我的服务中,我尝试使用自动装配的休息模板来调用另一个 eureka 注册服务.
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
所以我可以看到这两个服务都在 Eureka 注册.MyService 能够:
So I can see that both services are registered with Eureka. MyService is able to:
- 注册尤里卡
- 在 Eureka 中查询myotherservice"
- 使用正确的主机和端口获得有效响应
但是自动装配的 RestTemplate 正在抛出 UnknownHostException.
But the autowired RestTemplate is throwing UnknownHostException.
推荐答案
answer 是否需要创建一个 @Loadbalanced
RestTemplate
从 RC1 开始.
The answer is you need to create a @Loadbalanced
RestTemplate
as of RC1.
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
这篇关于spring-boot Autowired DiscoveryClient RestTemplate UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!