Java Code Examples for com.alibaba.nacos.api.naming.NamingService#getAllInstances()

The following examples show how to use com.alibaba.nacos.api.naming.NamingService#getAllInstances() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServiceProvider.java    From nacos-tutorial with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws NacosException {
    Properties properties = new Properties();
    properties.setProperty("serverAddr", Constants.NACOS_SERVER_ADDRESS);
    properties.setProperty("namespace", Constants.NAMESPACE);

    NamingService naming = NamingFactory.createNamingService(properties);

    naming.registerInstance(Constants.SERVICE_NAME, Constants.IP_1, Constants.PORT_1, Constants.CLUSTER_NAME_1);
    naming.registerInstance(Constants.SERVICE_NAME, Constants.IP_2, Constants.PORT_2, Constants.CLUSTER_NAME_2);
    List<Instance> instances = naming.getAllInstances(Constants.SERVICE_NAME);
    System.out.println("getAllInstances after registered\ninstance size="
            + instances.size() + "\ninstance list=" + instances);
    try {
        int in = System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: NacosSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(TaskDO taskDO) {
    try {

        NamingService sourceNamingService =
            nacosServerHolder.get(taskDO.getSourceClusterId(), taskDO.getGroupName());
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), taskDO.getGroupName());

        sourceNamingService.unsubscribe(taskDO.getServiceName(), nacosListenerMap.get(taskDO.getTaskId()));

        // 删除目标集群中同步的实例列表
        List<Instance> instances = destNamingService.getAllInstances(taskDO.getServiceName());
        for (Instance instance : instances) {
            if (needDelete(instance.getMetadata(), taskDO)) {
                destNamingService.deregisterInstance(taskDO.getServiceName(), instance.getIp(), instance.getPort());
            }
        }
    } catch (Exception e) {
        log.error("delete task from nacos to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}
 
Example 3
Source File: EurekaSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(TaskDO taskDO) {

    try {
        specialSyncEventBus.unsubscribe(taskDO);
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), taskDO.getNameSpace());
        List<Instance> allInstances = destNamingService.getAllInstances(taskDO.getServiceName());
        deleteAllInstance(taskDO, destNamingService, allInstances);

    } catch (Exception e) {
        log.error("delete a task from eureka to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}
 
Example 4
Source File: EurekaSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sync(TaskDO taskDO) {
    try {
        EurekaNamingService eurekaNamingService = eurekaServerHolder.get(taskDO.getSourceClusterId(), null);
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null);
        List<InstanceInfo> eurekaInstances = eurekaNamingService.getApplications(taskDO.getServiceName());
        List<Instance> nacosInstances = destNamingService.getAllInstances(taskDO.getServiceName());

        if (CollectionUtils.isEmpty(eurekaInstances)) {
            // Clear all instance from Nacos
            deleteAllInstance(taskDO, destNamingService, nacosInstances);
        } else {
            if (!CollectionUtils.isEmpty(nacosInstances)) {
                // Remove invalid instance from Nacos
                removeInvalidInstance(taskDO, destNamingService, eurekaInstances, nacosInstances);
            }
            addValidInstance(taskDO, destNamingService, eurekaInstances);
        }
        specialSyncEventBus.subscribe(taskDO, this::sync);
    } catch (Exception e) {
        log.error("sync task from eureka to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.SYNC_ERROR);
        return false;
    }
    return true;
}
 
Example 5
Source File: ConsulSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(TaskDO taskDO) {

    try {
        specialSyncEventBus.unsubscribe(taskDO);
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null);
        List<Instance> allInstances = destNamingService.getAllInstances(taskDO.getServiceName());
        for (Instance instance : allInstances) {
            if (needDelete(instance.getMetadata(), taskDO)) {

                destNamingService.deregisterInstance(taskDO.getServiceName(), instance.getIp(), instance.getPort());
            }
        }

    } catch (Exception e) {
        log.error("delete task from consul to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}
 
Example 6
Source File: ZookeeperSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(TaskDO taskDO) {
    try {

        CloseableUtils.closeQuietly(pathChildrenCacheMap.get(taskDO.getTaskId()));
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null);
        if (nacosServiceNameMap.containsKey(taskDO.getTaskId())) {
            List<Instance> allInstances =
                destNamingService.getAllInstances(nacosServiceNameMap.get(taskDO.getTaskId()));
            for (Instance instance : allInstances) {
                if (needDelete(instance.getMetadata(), taskDO)) {
                    destNamingService.deregisterInstance(instance.getServiceName(), instance.getIp(),
                        instance.getPort());
                }
                nacosServiceNameMap.remove(taskDO.getTaskId());

            }
        }

    } catch (Exception e) {
        log.error("delete task from zookeeper to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}
 
Example 7
Source File: ConsulSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
private void cleanAllOldInstance(TaskDO taskDO, NamingService destNamingService, Set<String> instanceKeys)
    throws NacosException {
    List<Instance> allInstances = destNamingService.getAllInstances(taskDO.getServiceName());
    for (Instance instance : allInstances) {
        if (needDelete(instance.getMetadata(), taskDO)
            && !instanceKeys.contains(composeInstanceKey(instance.getIp(), instance.getPort()))) {

            destNamingService.deregisterInstance(taskDO.getServiceName(), instance.getIp(), instance.getPort());
        }
    }
}