Search in sources :

Example 1 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project spring-cloud-alibaba by alibaba.

the class NacosDiscoveryEndpoint method nacosDiscovery.

/**
 * @return nacos discovery endpoint
 */
@ReadOperation
public Map<String, Object> nacosDiscovery() {
    Map<String, Object> result = new HashMap<>();
    result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
    NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
    List<ServiceInfo> subscribe = Collections.emptyList();
    try {
        subscribe = namingService.getSubscribeServices();
        for (ServiceInfo serviceInfo : subscribe) {
            List<Instance> instances = namingService.getAllInstances(serviceInfo.getName(), serviceInfo.getGroupName());
            serviceInfo.setHosts(instances);
        }
    } catch (Exception e) {
        log.error("get subscribe services from nacos fail,", e);
    }
    result.put("subscribe", subscribe);
    return result;
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) HashMap(java.util.HashMap) NamingService(com.alibaba.nacos.api.naming.NamingService) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Example 2 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class DiskCacheTest method setUp.

@Before
public void setUp() {
    System.out.println(CACHE_DIR);
    serviceInfo = new ServiceInfo("G@@testName", "testClusters");
    instance = new Instance();
    instance.setClusterName("testClusters");
    instance.setIp("1.1.1.1");
    instance.setPort(1234);
    instance.setServiceName("testName");
    instance.addMetadata("chinese", "中文");
    serviceInfo.setHosts(Collections.singletonList(instance));
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Before(org.junit.Before)

Example 3 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceInfoHolderTest method testProcessServiceInfoWithPushEmpty.

@Test
public void testProcessServiceInfoWithPushEmpty() {
    ServiceInfo oldInfo = new ServiceInfo("a@@b@@c");
    Instance instance1 = createInstance("1.1.1.1", 1);
    Instance instance2 = createInstance("1.1.1.2", 2);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(instance1);
    hosts.add(instance2);
    oldInfo.setHosts(hosts);
    Properties prop = new Properties();
    prop.setProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION, "true");
    ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
    holder.processServiceInfo(oldInfo);
    ServiceInfo newInfo = new ServiceInfo("a@@b@@c");
    final ServiceInfo actual = holder.processServiceInfo(newInfo);
    Assert.assertEquals(oldInfo.getKey(), actual.getKey());
    Assert.assertEquals(2, actual.getHosts().size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceInfoHolderTest method testProcessServiceInfo.

@Test
public void testProcessServiceInfo() {
    ServiceInfo info = new ServiceInfo("a@@b@@c");
    Instance instance1 = createInstance("1.1.1.1", 1);
    Instance instance2 = createInstance("1.1.1.2", 2);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(instance1);
    hosts.add(instance2);
    info.setHosts(hosts);
    Properties prop = new Properties();
    ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
    ServiceInfo actual1 = holder.processServiceInfo(info);
    Assert.assertEquals(info, actual1);
    Instance newInstance1 = createInstance("1.1.1.1", 1);
    newInstance1.setWeight(2.0);
    Instance instance3 = createInstance("1.1.1.3", 3);
    List<Instance> hosts2 = new ArrayList<>();
    hosts2.add(newInstance1);
    hosts2.add(instance3);
    ServiceInfo info2 = new ServiceInfo("a@@b@@c");
    info2.setHosts(hosts2);
    ServiceInfo actual2 = holder.processServiceInfo(info2);
    Assert.assertEquals(info2, actual2);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceInfoHolderTest method testGetServiceInfo.

@Test
public void testGetServiceInfo() {
    ServiceInfo info = new ServiceInfo("a@@b@@c");
    Instance instance1 = createInstance("1.1.1.1", 1);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(instance1);
    info.setHosts(hosts);
    Properties prop = new Properties();
    ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
    ServiceInfo expect = holder.processServiceInfo(info);
    String serviceName = "b";
    String groupName = "a";
    String clusters = "c";
    ServiceInfo actual = holder.getServiceInfo(serviceName, groupName, clusters);
    Assert.assertEquals(expect.getKey(), actual.getKey());
    Assert.assertEquals(expect.getHosts().size(), actual.getHosts().size());
    Assert.assertEquals(expect.getHosts().get(0), actual.getHosts().get(0));
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)74 Test (org.junit.Test)44 Instance (com.alibaba.nacos.api.naming.pojo.Instance)29 ArrayList (java.util.ArrayList)17 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)12 Properties (java.util.Properties)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)8 Before (org.junit.Before)8 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)7 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)5 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)4 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 HashMap (java.util.HashMap)4 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)3 Secured (com.alibaba.nacos.auth.annotation.Secured)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3