Search in sources :

Example 11 with ServiceInfo

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

the class NacosNamingServiceTest method testSelectInstancesWithHealthyFlag.

@Test
public void testSelectInstancesWithHealthyFlag() throws NacosException {
    // given
    Instance healthyInstance = new Instance();
    healthyInstance.setHealthy(true);
    Instance instance1 = new Instance();
    instance1.setHealthy(false);
    Instance instance2 = new Instance();
    instance2.setHealthy(true);
    instance2.setEnabled(false);
    Instance instance3 = new Instance();
    instance3.setHealthy(true);
    instance3.setWeight(0.0);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(healthyInstance);
    hosts.add(instance1);
    hosts.add(instance2);
    hosts.add(instance3);
    ServiceInfo info = new ServiceInfo();
    info.setHosts(hosts);
    String serviceName = "service1";
    String groupName = "group1";
    List<String> clusterList = Arrays.asList("cluster1", "cluster2");
    when(proxy.queryInstancesOfService(serviceName, groupName, "cluster1,cluster2", 0, false)).thenReturn(info);
    // when
    List<Instance> instances = client.selectInstances(serviceName, groupName, clusterList, true, false);
    // then
    Assert.assertEquals(1, instances.size());
    Assert.assertSame(healthyInstance, instances.get(0));
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 12 with ServiceInfo

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

the class NacosNamingServiceTest method testSelectOneHealthyInstance2.

@Test
public void testSelectOneHealthyInstance2() throws NacosException {
    // given
    Instance healthyInstance = new Instance();
    healthyInstance.setIp("1.1.1.1");
    healthyInstance.setPort(1000);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(healthyInstance);
    ServiceInfo infoWithHealthyInstance = new ServiceInfo();
    infoWithHealthyInstance.setHosts(hosts);
    when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance);
    String serviceName = "service1";
    String groupName = "group1";
    // when
    client.selectOneHealthyInstance(serviceName, groupName);
    // then
    verify(proxy, times(1)).subscribe(serviceName, groupName, "");
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 13 with ServiceInfo

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

the class NacosNamingServiceTest method testSelectOneHealthyInstance5.

@Test
public void testSelectOneHealthyInstance5() throws NacosException {
    // given
    Instance healthyInstance = new Instance();
    healthyInstance.setIp("1.1.1.1");
    healthyInstance.setPort(1000);
    List<Instance> hosts = new ArrayList<>();
    hosts.add(healthyInstance);
    ServiceInfo infoWithHealthyInstance = new ServiceInfo();
    infoWithHealthyInstance.setHosts(hosts);
    when(proxy.subscribe(anyString(), anyString(), anyString())).thenReturn(infoWithHealthyInstance);
    String serviceName = "service1";
    List<String> clusterList = Arrays.asList("cluster1", "cluster2");
    // when
    client.selectOneHealthyInstance(serviceName, clusterList);
    // then
    verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2");
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 with ServiceInfo

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

the class CatalogServiceV2Impl method listInstances.

@Override
public List<? extends Instance> listInstances(String namespaceId, String groupName, String serviceName, String clusterName) throws NacosException {
    Service service = Service.newService(namespaceId, groupName, serviceName);
    if (!ServiceManager.getInstance().containSingleton(service)) {
        throw new NacosException(NacosException.NOT_FOUND, String.format("service %s@@%s is not found!", groupName, serviceName));
    }
    if (!serviceStorage.getClusters(service).contains(clusterName)) {
        throw new NacosException(NacosException.NOT_FOUND, "cluster " + clusterName + " is not found!");
    }
    ServiceInfo serviceInfo = serviceStorage.getData(service);
    ServiceInfo result = ServiceUtil.selectInstances(serviceInfo, clusterName);
    return result.getHosts();
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 15 with ServiceInfo

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

the class InstanceOperatorClientImpl method listInstance.

@Override
public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster, boolean healthOnly) {
    Service service = getService(namespaceId, serviceName, true);
    // For adapt 1.X subscribe logic
    if (subscriber.getPort() > 0 && pushService.canEnablePush(subscriber.getAgent())) {
        String clientId = IpPortBasedClient.getClientId(subscriber.getAddrStr(), true);
        createIpPortClientIfAbsent(clientId);
        clientOperationService.subscribeService(service, subscriber, clientId);
    }
    ServiceInfo serviceInfo = serviceStorage.getData(service);
    ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(service).orElse(null);
    ServiceInfo result = ServiceUtil.selectInstancesWithHealthyProtection(serviceInfo, serviceMetadata, cluster, healthOnly, true, subscriber.getIp());
    // adapt for v1.x sdk
    result.setName(NamingUtils.getGroupedName(result.getName(), result.getGroupName()));
    return result;
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

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