EurekaClient
@Qualifier("eurekaClient")
@Autowired
private EurekaClient eurekaClient;
public String dataServiceUrlByEurekaClient() {
InstanceInfo instance = eurekaClient.getNextServerFromEureka("spring-data-demo", false);
return instance.getHomePageUrl();
}
DiscoveryClient
@Autowired
private DiscoveryClient discoveryClient;
public String dataServiceUrlByDiscoveryClient() {
List<ServiceInstance> list = discoveryClient.getInstances("spring-data-demo");
if (list != null && list.size() > 0 ) {
return String.valueOf(list.get(0).getUri());
}
return null;
}
RestTemplate
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/restTemplateTest")
public String restTemplateTest(){
String str = restTemplate.getForObject("http://data-demo/demo/hello", String.class);
return str;
}
转载于:https://blog.51cto.com/881206524/2118415