Search in sources :

Example 1 with DefaultPage

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);
    });
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultPage(org.apache.dubbo.common.utils.DefaultPage) LinkedList(java.util.LinkedList) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with DefaultPage

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);
    });
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultPage(org.apache.dubbo.common.utils.DefaultPage) LinkedList(java.util.LinkedList) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

LinkedList (java.util.LinkedList)2 DefaultPage (org.apache.dubbo.common.utils.DefaultPage)2 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)2 KeeperException (org.apache.zookeeper.KeeperException)2