DaS*_*aun 4 spring-boot spring-cloud spring-cloud-netflix
我正在使用弹簧靴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>
Run Code Online (Sandbox Code Playgroud)
编辑
我正在使用Brixton.RC1来获得我的Spring云依赖项
<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>
Run Code Online (Sandbox Code Playgroud)
我的应用程序使用以下注释进行设置:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个服务的休息控制器:
@RestController
public class MyController {
@Autowired
private MyService myService;
Run Code Online (Sandbox Code Playgroud)
在我的服务中,我正在尝试使用自动装配的休息模板来呼叫另一个尤里卡注册的服务.
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我完全感到困惑,因为我在另一个项目中工作.这也是输出:
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
Run Code Online (Sandbox Code Playgroud)
所以我可以看到这两项服务都在Eureka注册.MyService能够:
但自动装配的RestTemplate会抛出UnknownHostException.
spe*_*ibb 12
的回答是,你需要创建一个@Loadbalanced RestTemplate为RC1的.
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4733 次 |
| 最近记录: |