Search in sources :

Example 41 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project micro-service by Lovnx.

the class ComputeController method add.

@RequestMapping(value = "/**", method = RequestMethod.GET)
public String add(@RequestParam Integer a, @RequestParam Integer b, HttpServletRequest request) {
    System.out.println(request.getRequestURL());
    ServiceInstance instance = client.getLocalServiceInstance();
    Integer r = a + b;
    logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
    return "From Service-B, Result is " + r + "\nPort:" + instance.getPort();
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 42 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project coffeenet-starter by coffeenet.

the class IntegrationCoffeeNetAppServiceTest method getAppsNotOfTypeEurekaServiceInstance.

@Test
public void getAppsNotOfTypeEurekaServiceInstance() {
    String frontPageName = "frontpage";
    ServiceInstance serviceInstance = mock(ServiceInstance.class);
    when(discoveryClientMock.getInstances(frontPageName)).thenReturn(singletonList(serviceInstance));
    when(discoveryClientMock.getServices()).thenReturn(singletonList(frontPageName));
    AppQuery query = AppQuery.builder().withAppName(frontPageName).build();
    Map<String, List<CoffeeNetApp>> coffeeNetApps = coffeeNetAppService.getApps(query);
    assertThat(coffeeNetApps.entrySet(), hasSize(0));
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) EurekaServiceInstance(org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 43 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project druid by alibaba.

the class MonitorStatService method getAllServiceNodeMap.

/**
 * 获取所有服务信息
 *
 * @return
 */
public Map<String, ServiceNode> getAllServiceNodeMap() {
    List<String> services = discoveryClient.getServices();
    List<ServiceNode> serviceNodes = new ArrayList<>();
    for (String service : services) {
        List<ServiceInstance> instances = discoveryClient.getInstances(service);
        for (ServiceInstance instance : instances) {
            String host = instance.getHost();
            String instanceId = instance.getInstanceId();
            if (instanceId == null) {
                instanceId = instance.getMetadata().get("nacos.instanceId").replaceAll("#", "-").replaceAll("@@", "-");
            }
            int port = instance.getPort();
            String serviceId = instance.getServiceId();
            // 根据前端参数采集指定的服务
            if (monitorProperties.getApplications().contains(serviceId)) {
                ServiceNode serviceNode = new ServiceNode();
                serviceNode.setId(instanceId);
                serviceNode.setPort(port);
                serviceNode.setAddress(host);
                serviceNode.setServiceName(serviceId);
                serviceNodes.add(serviceNode);
                serviceIdMap.put(instanceId, serviceNode);
            }
        }
    }
    return serviceNodes.parallelStream().collect(Collectors.toMap(i -> i.getServiceName() + "-" + i.getAddress() + "-" + i.getPort(), Function.identity(), (v1, v2) -> v2));
}
Also used : JSONUtils(com.alibaba.druid.support.json.JSONUtils) HttpUtil(com.alibaba.druid.admin.util.HttpUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) DiscoveryClient(org.springframework.cloud.client.discovery.DiscoveryClient) HashMap(java.util.HashMap) Function(java.util.function.Function) SqlListResult(com.alibaba.druid.admin.model.dto.SqlListResult) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ServiceNode(com.alibaba.druid.admin.model.ServiceNode) JSONArray(com.alibaba.fastjson.JSONArray) MonitorProperties(com.alibaba.druid.admin.config.MonitorProperties) WebResult(com.alibaba.druid.admin.model.dto.WebResult) Map(java.util.Map) ConnectionResult(com.alibaba.druid.admin.model.dto.ConnectionResult) SpringStatManager(com.alibaba.druid.support.spring.stat.SpringStatManager) StringUtils(com.alibaba.druid.util.StringUtils) Collectors(java.util.stream.Collectors) SqlDetailResult(com.alibaba.druid.admin.model.dto.SqlDetailResult) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) JSON(com.alibaba.fastjson.JSON) MapComparator(com.alibaba.druid.util.MapComparator) DruidStatServiceMBean(com.alibaba.druid.stat.DruidStatServiceMBean) WebAppStatManager(com.alibaba.druid.support.http.stat.WebAppStatManager) WallResult(com.alibaba.druid.admin.model.dto.WallResult) JSONObject(com.alibaba.fastjson.JSONObject) ServiceInstance(org.springframework.cloud.client.ServiceInstance) DataSourceResult(com.alibaba.druid.admin.model.dto.DataSourceResult) Collections(java.util.Collections) ServiceNode(com.alibaba.druid.admin.model.ServiceNode) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 44 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project druid by alibaba.

the class MonitorStatService method getServiceAllNodeMap.

/**
 * 获取指定服务名的所有节点
 *
 * @param parameters
 * @return
 */
public Map<String, ServiceNode> getServiceAllNodeMap(Map<String, String> parameters) {
    String requestServiceName = parameters.get("serviceName");
    List<String> services = discoveryClient.getServices();
    List<ServiceNode> serviceNodes = new ArrayList<>();
    for (String service : services) {
        List<ServiceInstance> instances = discoveryClient.getInstances(service);
        for (ServiceInstance instance : instances) {
            String host = instance.getHost();
            String instanceId = instance.getInstanceId();
            if (instanceId == null) {
                instanceId = instance.getMetadata().get("nacos.instanceId").replaceAll("#", "-").replaceAll("@@", "-");
            }
            int port = instance.getPort();
            String serviceId = instance.getServiceId();
            // 根据前端参数采集指定的服务
            if (serviceId.equalsIgnoreCase(requestServiceName)) {
                ServiceNode serviceNode = new ServiceNode();
                serviceNode.setId(instanceId);
                serviceNode.setPort(port);
                serviceNode.setAddress(host);
                serviceNode.setServiceName(serviceId);
                serviceNodes.add(serviceNode);
                serviceIdMap.put(instanceId, serviceNode);
            }
        }
    }
    return serviceNodes.parallelStream().collect(Collectors.toMap(i -> i.getServiceName() + "-" + i.getAddress() + "-" + i.getPort(), Function.identity(), (v1, v2) -> v2));
}
Also used : JSONUtils(com.alibaba.druid.support.json.JSONUtils) HttpUtil(com.alibaba.druid.admin.util.HttpUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) DiscoveryClient(org.springframework.cloud.client.discovery.DiscoveryClient) HashMap(java.util.HashMap) Function(java.util.function.Function) SqlListResult(com.alibaba.druid.admin.model.dto.SqlListResult) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ServiceNode(com.alibaba.druid.admin.model.ServiceNode) JSONArray(com.alibaba.fastjson.JSONArray) MonitorProperties(com.alibaba.druid.admin.config.MonitorProperties) WebResult(com.alibaba.druid.admin.model.dto.WebResult) Map(java.util.Map) ConnectionResult(com.alibaba.druid.admin.model.dto.ConnectionResult) SpringStatManager(com.alibaba.druid.support.spring.stat.SpringStatManager) StringUtils(com.alibaba.druid.util.StringUtils) Collectors(java.util.stream.Collectors) SqlDetailResult(com.alibaba.druid.admin.model.dto.SqlDetailResult) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) JSON(com.alibaba.fastjson.JSON) MapComparator(com.alibaba.druid.util.MapComparator) DruidStatServiceMBean(com.alibaba.druid.stat.DruidStatServiceMBean) WebAppStatManager(com.alibaba.druid.support.http.stat.WebAppStatManager) WallResult(com.alibaba.druid.admin.model.dto.WallResult) JSONObject(com.alibaba.fastjson.JSONObject) ServiceInstance(org.springframework.cloud.client.ServiceInstance) DataSourceResult(com.alibaba.druid.admin.model.dto.DataSourceResult) Collections(java.util.Collections) ServiceNode(com.alibaba.druid.admin.model.ServiceNode) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 45 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project topcom-cloud by 545314690.

the class HelloController method add.

@RequestMapping(value = "/add")
public int add(int a, int b) {
    ServiceInstance instance = client.getLocalServiceInstance();
    logger.info("/add:" + instance.getHost() + ",service_id: " + instance.getServiceId());
    return a + b;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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