use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.
the class Swagger2Config method getAllServiceByEureka.
/**
* 获取Eureka上面的所有服务
* @return TreeMap<服务名,List<该服务的所有在线运行节点>>
*/
private TreeMap getAllServiceByEureka() {
List<ServiceInstance> temp = new ArrayList<>();
List<String> allServiceId = discoveryClient.getServices();
for (String serviceId : allServiceId) {
List<ServiceInstance> list = discoveryClient.getInstances(serviceId);
if (list != null && !list.isEmpty()) {
temp.addAll(list);
}
}
// 获取所有的服务和所在的地址
TreeMap nameAndUri = new TreeMap<>();
for (ServiceInstance serviceInstance : temp) {
if (!nameAndUri.containsKey(serviceInstance.getServiceId().toString())) {
nameAndUri.put(serviceInstance.getServiceId().toString(), new ArrayList<>());
}
ArrayList list = (ArrayList) nameAndUri.get(serviceInstance.getServiceId().toString());
list.add(serviceInstance.getUri().toString());
}
return nameAndUri;
}
use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.
the class IndexController method showAllServerForEureka.
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(value = "/showAllServerForEureka", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public ModelAndView showAllServerForEureka(ModelAndView mv) {
log.info("测试获取所有服务列表");
List<ServiceInstance> temp = new ArrayList<>();
List<String> allServiceId = discoveryClient.getServices();
for (String serviceId : allServiceId) {
List<ServiceInstance> list = discoveryClient.getInstances(serviceId);
if (list != null && !list.isEmpty()) {
temp.addAll(list);
}
}
// 获取所有的服务和所在的地址
Map nameAndUri = new TreeMap<>();
for (ServiceInstance serviceInstance : temp) {
if (!nameAndUri.containsKey(serviceInstance.getServiceId().toString())) {
nameAndUri.put(serviceInstance.getServiceId().toString(), new ArrayList<>());
}
ArrayList list = (ArrayList) nameAndUri.get(serviceInstance.getServiceId().toString());
list.add(serviceInstance.getUri().toString());
}
mv.setViewName("swaggerui-eureka");
mv.addObject("allService", nameAndUri);
return mv;
}
use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.
the class HelloController method sayHello2.
// 使用该注解描述接口方法信息
@ApiOperation(value = "sayHello", notes = "sayHello对象参数版")
@RequestMapping(value = "/sayHello2", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public Map sayHello2(@RequestBody HelloRequest hr) {
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
log.info("discovery client output:" + instance.getHost() + ":" + instance.getPort() + "/" + instance.getServiceId());
log.info("进入服务生产者");
Map map = new HashMap<String, String>();
map.put("name", hr.getName());
map.put("say", hr.getSay());
map.put("randomId", hr.getRandomId());
log.info("服务生产者返回数据");
return map;
}
use of org.springframework.cloud.client.ServiceInstance in project apollo by ctripcorp.
the class SpringCloudInnerDiscoveryService method getServiceInstances.
@Override
public List<ServiceDTO> getServiceInstances(String serviceId) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
if (!CollectionUtils.isEmpty(instances)) {
instances.forEach(instance -> {
ServiceDTO serviceDTO = this.toServiceDTO(instance, serviceId);
serviceDTOList.add(serviceDTO);
});
}
return serviceDTOList;
}
use of org.springframework.cloud.client.ServiceInstance in project apollo by ctripcorp.
the class ConsulDiscoveryServiceTest method mockServiceInstance.
private ServiceInstance mockServiceInstance(String instanceId, String ip, int port) {
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(serviceInstance.getInstanceId()).thenReturn(instanceId);
when(serviceInstance.getHost()).thenReturn(ip);
when(serviceInstance.getPort()).thenReturn(port);
return serviceInstance;
}
Aggregations