Search in sources :

Example 56 with ServiceInstance

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;
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance) TreeMap(java.util.TreeMap)

Example 57 with ServiceInstance

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;
}
Also used : ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 58 with ServiceInstance

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;
}
Also used : HashMap(java.util.HashMap) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HashMap(java.util.HashMap) Map(java.util.Map) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 59 with ServiceInstance

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;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 60 with ServiceInstance

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;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Aggregations

ServiceInstance (org.springframework.cloud.client.ServiceInstance)73 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)25 Test (org.junit.Test)24 HashMap (java.util.HashMap)12 URI (java.net.URI)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Registration (de.codecentric.boot.admin.server.domain.values.Registration)10 ArrayList (java.util.ArrayList)10 Test (org.junit.jupiter.api.Test)10 Application (de.codecentric.boot.admin.model.Application)7 Map (java.util.Map)7 List (java.util.List)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Random (java.util.Random)4 Collections (java.util.Collections)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 MonitorProperties (com.alibaba.druid.admin.config.MonitorProperties)2 ServiceNode (com.alibaba.druid.admin.model.ServiceNode)2 ConnectionResult (com.alibaba.druid.admin.model.dto.ConnectionResult)2