EurekaClient
EurekaClient是Eureka提供的服务发现的接口。
在Consumer模块中,加入:
@RequestMapping("/application")
public Applications application() {
Applications applications = eurekaClient.getApplications();
return applications;
}
getApplications获取所有注册的服务列表,访问http://localhost:8001/application,看到:

可以看到已注册服务的信息。
@RequestMapping("/getServiceList")
public List<InstanceInfo> getServiceList() {
List<InstanceInfo> list = eurekaClient.getInstancesByVipAddress("producer", false);
return list;
}
getInstancesByVipAddress通过应用程序名获取对应的服务,第二个参数是是否是安全配置。访问http://localhost:8001/getServiceList,看到:

现在演示调用远程服务:
@RequestMapping("/getHello")
public String getHello() {
List<InstanceInfo> list = eurekaClient.getInstancesByVipAddress("producer", false);
if (list != null && !list.isEmpty()) {
InstanceInfo instanceInfo = list.get(0);
String url = "http://"+instanceInfo.getIPAddr()+":"+ instanceInfo.getPort();
String forObject = restTemplate.getForObject(url + "/hello", String.class);
return forObject;
}
return null;
}
访问http://localhost:8001/getHello,得到hello,8000结果。

浙公网安备 33010602011771号