use of org.apache.dubbo.common.utils.DefaultPage in project dubbo by alibaba.
the class ZookeeperServiceDiscovery method getInstances.
@Override
public Page<ServiceInstance> getInstances(String serviceName, int offset, int pageSize, boolean healthyOnly) {
String path = buildServicePath(serviceName);
return execute(path, p -> {
List<ServiceInstance> serviceInstances = new LinkedList<>();
int totalSize = 0;
try {
List<String> serviceIds = new LinkedList<>(curatorFramework.getChildren().forPath(p));
totalSize = serviceIds.size();
Iterator<String> iterator = serviceIds.iterator();
for (int i = 0; i < offset; i++) {
if (iterator.hasNext()) {
// remove the elements from 0 to offset
iterator.next();
iterator.remove();
}
}
for (int i = 0; i < pageSize; i++) {
if (iterator.hasNext()) {
String serviceId = iterator.next();
ServiceInstance serviceInstance = build(serviceDiscovery.queryForInstance(serviceName, serviceId));
serviceInstances.add(serviceInstance);
}
}
if (healthyOnly) {
Iterator<ServiceInstance> instanceIterator = serviceInstances.iterator();
while (instanceIterator.hasNext()) {
ServiceInstance instance = instanceIterator.next();
if (!instance.isHealthy()) {
instanceIterator.remove();
}
}
}
} catch (KeeperException.NoNodeException e) {
logger.warn(p + " path not exist.", e);
}
return new DefaultPage<>(offset, pageSize, serviceInstances, totalSize);
});
}
use of org.apache.dubbo.common.utils.DefaultPage in project dubbo by apache.
the class ZookeeperServiceDiscovery method getInstances.
@Override
public Page<ServiceInstance> getInstances(String serviceName, int offset, int pageSize, boolean healthyOnly) {
String path = buildServicePath(serviceName);
return execute(path, p -> {
List<ServiceInstance> serviceInstances = new LinkedList<>();
int totalSize = 0;
try {
List<String> serviceIds = new LinkedList<>(curatorFramework.getChildren().forPath(p));
totalSize = serviceIds.size();
Iterator<String> iterator = serviceIds.iterator();
for (int i = 0; i < offset; i++) {
if (iterator.hasNext()) {
// remove the elements from 0 to offset
iterator.next();
iterator.remove();
}
}
for (int i = 0; i < pageSize; i++) {
if (iterator.hasNext()) {
String serviceId = iterator.next();
ServiceInstance serviceInstance = build(serviceDiscovery.queryForInstance(serviceName, serviceId));
serviceInstances.add(serviceInstance);
}
}
if (healthyOnly) {
Iterator<ServiceInstance> instanceIterator = serviceInstances.iterator();
while (instanceIterator.hasNext()) {
ServiceInstance instance = instanceIterator.next();
if (!instance.isHealthy()) {
instanceIterator.remove();
}
}
}
} catch (KeeperException.NoNodeException e) {
logger.warn(p + " path not exist.", e);
}
return new DefaultPage<>(offset, pageSize, serviceInstances, totalSize);
});
}
Aggregations