@Test public void testGetPagedService() throws NacosException { serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true); Service service = serviceManager.getService(TEST_NAMESPACE, TEST_SERVICE_NAME); service.addCluster(cluster); ((Set<Instance>) ReflectionTestUtils.getField(cluster, "ephemeralInstances")).add(instance); List<Service> actualServices = new ArrayList<>(8); int actualSize = serviceManager .getPagedService(TEST_NAMESPACE, 0, 10, StringUtils.EMPTY, "1.1.1.1:1", actualServices, true); assertEquals(1, actualSize); assertEquals(TEST_SERVICE_NAME, actualServices.get(0).getName()); }
public int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance, List<Service> serviceList, boolean hasIpCount) { List<Service> matchList; if (chooseServiceMap(namespaceId) == null) { return 0; } if (StringUtils.isNotBlank(param)) { StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); for (String s : param.split(Constants.SERVICE_INFO_SPLITER)) { regex.add(StringUtils.isBlank(s) ? Constants.ANY_PATTERN : Constants.ANY_PATTERN + s + Constants.ANY_PATTERN); } matchList = searchServices(namespaceId, regex.toString()); } else { matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); } if (!CollectionUtils.isEmpty(matchList) && hasIpCount) { matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())) .collect(Collectors.toList()); } if (StringUtils.isNotBlank(containedInstance)) { boolean contained; for (int i = 0; i < matchList.size(); i++) { Service service = matchList.get(i); contained = false; List<Instance> instances = service.allIPs(); for (Instance instance : instances) { if (containedInstance.contains(":")) { if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { contained = true; break; } } else { if (StringUtils.equals(instance.getIp(), containedInstance)) { contained = true; break; } } } if (!contained) { matchList.remove(i); i--; } } } if (pageSize >= matchList.size()) { serviceList.addAll(matchList); return matchList.size(); } for (int i = 0; i < matchList.size(); i++) { if (i < startPage * pageSize) { continue; } serviceList.add(matchList.get(i)); if (serviceList.size() >= pageSize) { break; } } return matchList.size(); }
ServiceManager implements RecordListener<Service> { public int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance, List<Service> serviceList, boolean hasIpCount) { List<Service> matchList; if (chooseServiceMap(namespaceId) == null) { return 0; } if (StringUtils.isNotBlank(param)) { StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); for (String s : param.split(Constants.SERVICE_INFO_SPLITER)) { regex.add(StringUtils.isBlank(s) ? Constants.ANY_PATTERN : Constants.ANY_PATTERN + s + Constants.ANY_PATTERN); } matchList = searchServices(namespaceId, regex.toString()); } else { matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); } if (!CollectionUtils.isEmpty(matchList) && hasIpCount) { matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())) .collect(Collectors.toList()); } if (StringUtils.isNotBlank(containedInstance)) { boolean contained; for (int i = 0; i < matchList.size(); i++) { Service service = matchList.get(i); contained = false; List<Instance> instances = service.allIPs(); for (Instance instance : instances) { if (containedInstance.contains(":")) { if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { contained = true; break; } } else { if (StringUtils.equals(instance.getIp(), containedInstance)) { contained = true; break; } } } if (!contained) { matchList.remove(i); i--; } } } if (pageSize >= matchList.size()) { serviceList.addAll(matchList); return matchList.size(); } for (int i = 0; i < matchList.size(); i++) { if (i < startPage * pageSize) { continue; } serviceList.add(matchList.get(i)); if (serviceList.size() >= pageSize) { break; } } return matchList.size(); } }
ServiceManager implements RecordListener<Service> { public int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance, List<Service> serviceList, boolean hasIpCount) { List<Service> matchList; if (chooseServiceMap(namespaceId) == null) { return 0; } if (StringUtils.isNotBlank(param)) { StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); for (String s : param.split(Constants.SERVICE_INFO_SPLITER)) { regex.add(StringUtils.isBlank(s) ? Constants.ANY_PATTERN : Constants.ANY_PATTERN + s + Constants.ANY_PATTERN); } matchList = searchServices(namespaceId, regex.toString()); } else { matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); } if (!CollectionUtils.isEmpty(matchList) && hasIpCount) { matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())) .collect(Collectors.toList()); } if (StringUtils.isNotBlank(containedInstance)) { boolean contained; for (int i = 0; i < matchList.size(); i++) { Service service = matchList.get(i); contained = false; List<Instance> instances = service.allIPs(); for (Instance instance : instances) { if (containedInstance.contains(":")) { if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { contained = true; break; } } else { if (StringUtils.equals(instance.getIp(), containedInstance)) { contained = true; break; } } } if (!contained) { matchList.remove(i); i--; } } } if (pageSize >= matchList.size()) { serviceList.addAll(matchList); return matchList.size(); } for (int i = 0; i < matchList.size(); i++) { if (i < startPage * pageSize) { continue; } serviceList.add(matchList.get(i)); if (serviceList.size() >= pageSize) { break; } } return matchList.size(); } ServiceManager(SwitchDomain switchDomain, DistroMapper distroMapper, ServerMemberManager memberManager,
            PushService pushService, RaftPeerSet raftPeerSet); }
ServiceManager implements RecordListener<Service> { public int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance, List<Service> serviceList, boolean hasIpCount) { List<Service> matchList; if (chooseServiceMap(namespaceId) == null) { return 0; } if (StringUtils.isNotBlank(param)) { StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); for (String s : param.split(Constants.SERVICE_INFO_SPLITER)) { regex.add(StringUtils.isBlank(s) ? Constants.ANY_PATTERN : Constants.ANY_PATTERN + s + Constants.ANY_PATTERN); } matchList = searchServices(namespaceId, regex.toString()); } else { matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); } if (!CollectionUtils.isEmpty(matchList) && hasIpCount) { matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())) .collect(Collectors.toList()); } if (StringUtils.isNotBlank(containedInstance)) { boolean contained; for (int i = 0; i < matchList.size(); i++) { Service service = matchList.get(i); contained = false; List<Instance> instances = service.allIPs(); for (Instance instance : instances) { if (containedInstance.contains(":")) { if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { contained = true; break; } } else { if (StringUtils.equals(instance.getIp(), containedInstance)) { contained = true; break; } } } if (!contained) { matchList.remove(i); i--; } } } if (pageSize >= matchList.size()) { serviceList.addAll(matchList); return matchList.size(); } for (int i = 0; i < matchList.size(); i++) { if (i < startPage * pageSize) { continue; } serviceList.add(matchList.get(i)); if (serviceList.size() >= pageSize) { break; } } return matchList.size(); } ServiceManager(SwitchDomain switchDomain, DistroMapper distroMapper, ServerMemberManager memberManager,
            PushService pushService, RaftPeerSet raftPeerSet); @PostConstruct void init(); Map<String, Service> chooseServiceMap(String namespaceId); void addUpdatedServiceToQueue(String namespaceId, String serviceName, String serverIP, String checksum); @Override boolean interests(String key); @Override boolean matchUnlistenKey(String key); @Override void onChange(String key, Service service); @Override void onDelete(String key); RaftPeer getMySelfClusterState(); void updatedHealthStatus(String namespaceId, String serviceName, String serverIP); Set<String> getAllServiceNames(String namespaceId); Map<String, Set<String>> getAllServiceNames(); Set<String> getAllNamespaces(); List<String> getAllServiceNameList(String namespaceId); Map<String, Set<Service>> getResponsibleServices(); int getResponsibleServiceCount(); int getResponsibleInstanceCount(); void easyRemoveService(String namespaceId, String serviceName); void addOrReplaceService(Service service); void createEmptyService(String namespaceId, String serviceName, boolean local); void createServiceIfAbsent(String namespaceId, String serviceName, boolean local, Cluster cluster); void registerInstance(String namespaceId, String serviceName, Instance instance); void updateInstance(String namespaceId, String serviceName, Instance instance); void addInstance(String namespaceId, String serviceName, boolean ephemeral, Instance... ips); void removeInstance(String namespaceId, String serviceName, boolean ephemeral, Instance... ips); Instance getInstance(String namespaceId, String serviceName, String cluster, String ip, int port); List<Instance> updateIpAddresses(Service service, String action, boolean ephemeral, Instance... ips); Service getService(String namespaceId, String serviceName); boolean containService(String namespaceId, String serviceName); void putService(Service service); List<Service> searchServices(String namespaceId, String regex); int getServiceCount(); int getInstanceCount(); int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance,
            List<Service> serviceList, boolean hasIpCount); }
ServiceManager implements RecordListener<Service> { public int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance, List<Service> serviceList, boolean hasIpCount) { List<Service> matchList; if (chooseServiceMap(namespaceId) == null) { return 0; } if (StringUtils.isNotBlank(param)) { StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); for (String s : param.split(Constants.SERVICE_INFO_SPLITER)) { regex.add(StringUtils.isBlank(s) ? Constants.ANY_PATTERN : Constants.ANY_PATTERN + s + Constants.ANY_PATTERN); } matchList = searchServices(namespaceId, regex.toString()); } else { matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); } if (!CollectionUtils.isEmpty(matchList) && hasIpCount) { matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())) .collect(Collectors.toList()); } if (StringUtils.isNotBlank(containedInstance)) { boolean contained; for (int i = 0; i < matchList.size(); i++) { Service service = matchList.get(i); contained = false; List<Instance> instances = service.allIPs(); for (Instance instance : instances) { if (containedInstance.contains(":")) { if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { contained = true; break; } } else { if (StringUtils.equals(instance.getIp(), containedInstance)) { contained = true; break; } } } if (!contained) { matchList.remove(i); i--; } } } if (pageSize >= matchList.size()) { serviceList.addAll(matchList); return matchList.size(); } for (int i = 0; i < matchList.size(); i++) { if (i < startPage * pageSize) { continue; } serviceList.add(matchList.get(i)); if (serviceList.size() >= pageSize) { break; } } return matchList.size(); } ServiceManager(SwitchDomain switchDomain, DistroMapper distroMapper, ServerMemberManager memberManager,
            PushService pushService, RaftPeerSet raftPeerSet); @PostConstruct void init(); Map<String, Service> chooseServiceMap(String namespaceId); void addUpdatedServiceToQueue(String namespaceId, String serviceName, String serverIP, String checksum); @Override boolean interests(String key); @Override boolean matchUnlistenKey(String key); @Override void onChange(String key, Service service); @Override void onDelete(String key); RaftPeer getMySelfClusterState(); void updatedHealthStatus(String namespaceId, String serviceName, String serverIP); Set<String> getAllServiceNames(String namespaceId); Map<String, Set<String>> getAllServiceNames(); Set<String> getAllNamespaces(); List<String> getAllServiceNameList(String namespaceId); Map<String, Set<Service>> getResponsibleServices(); int getResponsibleServiceCount(); int getResponsibleInstanceCount(); void easyRemoveService(String namespaceId, String serviceName); void addOrReplaceService(Service service); void createEmptyService(String namespaceId, String serviceName, boolean local); void createServiceIfAbsent(String namespaceId, String serviceName, boolean local, Cluster cluster); void registerInstance(String namespaceId, String serviceName, Instance instance); void updateInstance(String namespaceId, String serviceName, Instance instance); void addInstance(String namespaceId, String serviceName, boolean ephemeral, Instance... ips); void removeInstance(String namespaceId, String serviceName, boolean ephemeral, Instance... ips); Instance getInstance(String namespaceId, String serviceName, String cluster, String ip, int port); List<Instance> updateIpAddresses(Service service, String action, boolean ephemeral, Instance... ips); Service getService(String namespaceId, String serviceName); boolean containService(String namespaceId, String serviceName); void putService(Service service); List<Service> searchServices(String namespaceId, String regex); int getServiceCount(); int getInstanceCount(); int getPagedService(String namespaceId, int startPage, int pageSize, String param, String containedInstance,
            List<Service> serviceList, boolean hasIpCount);  }