千家信息网

spring cloud eureka 消费端

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,EurekaClient@Qualifier("eurekaClient")@Autowiredprivate EurekaClient eurekaClient;public String data
千家信息网最后更新 2025年01月23日spring cloud eureka 消费端

EurekaClient

@Qualifier("eurekaClient")@Autowiredprivate EurekaClient eurekaClient;public String dataServiceUrlByEurekaClient() {    InstanceInfo instance = eurekaClient.getNextServerFromEureka("spring-data-demo", false);    return instance.getHomePageUrl();}

DiscoveryClient

@Autowiredprivate DiscoveryClient discoveryClient;public String dataServiceUrlByDiscoveryClient() {    List list = discoveryClient.getInstances("spring-data-demo");    if (list != null && list.size() > 0 ) {        return String.valueOf(list.get(0).getUri());    }    return null;}

RestTemplate

@Configurationpublic class RestTemplateConfiguration {    @Bean    @LoadBalanced    RestTemplate restTemplate(){        return new RestTemplate();    }}
@Autowiredprivate RestTemplate restTemplate;@GetMapping("/restTemplateTest")public String restTemplateTest(){        String str = restTemplate.getForObject("http://data-demo/demo/hello", String.class);        return str; }
0