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));
}
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, "");
}
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");
}
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();
}
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;
}
Aggregations