com.alibaba.nacos.api.naming.listener.EventListener Java Examples

The following examples show how to use com.alibaba.nacos.api.naming.listener.EventListener. 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: NacosRegistry.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(ClusterBooking booking) {
    return Futures.call(future -> {
        NacosClusterBooking ncBooking = (NacosClusterBooking) booking;
        //创建listener
        EventListener listener = event -> {
            if (event instanceof NamingEvent) {
                NamingEvent e = (NamingEvent) event;
                doUpdate(ncBooking, e.getInstances());
            }
        };
        ncBooking.setListener(listener);
        //订阅
        registry.namingService.subscribe(ncBooking.getServiceName(), listener);
        future.complete(null);
    });
}
 
Example #2
Source File: ServiceConsumer.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.subscribe(Constants.SERVICE_NAME, new EventListener() {
        @Override
        public void onEvent(Event event) {
            NamingEvent namingEvent = (NamingEvent) event;
            printInstances(namingEvent);
            mockConsume(naming, Constants.SERVICE_NAME);
        }
    });
    try {
        int in = System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: NacosRegistry.java    From dubbo-registry-nacos with Apache License 2.0 6 votes vote down vote up
private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener)
        throws NacosException {
    EventListener eventListener = event -> {
        if (event instanceof NamingEvent) {
            NamingEvent e = (NamingEvent) event;
            List<Instance> instances = e.getInstances();


            if(isServiceNamesWithCompatibleMode(url)){
                /**
                 * Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
                 * in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
                 */
                NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances);
                instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName);
            }

            notifySubscriber(url, listener, instances);
        }
    };
    namingService.subscribe(serviceName, eventListener);
}
 
Example #4
Source File: NacosSyncToZookeeperServiceImpl.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());
        EventListener eventListener = nacosListenerMap.remove(taskDO.getTaskId());
        PathChildrenCache pathChildrenCache = pathChildrenCacheMap.get(taskDO.getTaskId());
        sourceNamingService.unsubscribe(taskDO.getServiceName(), eventListener);
        CloseableUtils.closeQuietly(pathChildrenCache);
        Set<String> instanceUrlSet = instanceBackupMap.get(taskDO.getTaskId());
        CuratorFramework client = zookeeperServerHolder.get(taskDO.getDestClusterId(), taskDO.getGroupName());
        for (String instanceUrl : instanceUrlSet) {
            client.delete().quietly().forPath(instanceUrl);
        }
    } catch (Exception e) {
        log.error("delete task from nacos to zk was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}
 
Example #5
Source File: NacosRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void unSubscribe(ConsumerConfig config) {
    if (config.isSubscribe()) {
        String serviceName = NacosRegistryHelper.buildServiceName(config, config.getProtocol());
        try {
            EventListener eventListener = consumerListeners.remove(config);
            if (null != eventListener) {
                namingService.unsubscribe(serviceName, defaultCluster, eventListener);
            }
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {

                if (e instanceof SofaRpcRuntimeException) {
                    throw (SofaRpcRuntimeException) e;
                } else {
                    throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_LISTENER, EXT_NAME), e);
                }
            }
        }

        providerObserver.removeProviderListener(config);
    }
}
 
Example #6
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doUnsubscribe(ClusterBooking booking) {
    return Futures.call(future -> {
        NacosClusterBooking ncBooking = (NacosClusterBooking) booking;
        EventListener listener = ncBooking.removeListener();
        if (listener != null) {
            registry.namingService.unsubscribe(ncBooking.getServiceName(), listener);
        }
        future.complete(null);
    });
}
 
Example #7
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, String groupName, List<String> clusters,
		EventListener listener) throws NacosException {

}
 
Example #8
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, List<String> clusters,
		EventListener listener) throws NacosException {

}
 
Example #9
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, String groupName, EventListener listener)
		throws NacosException {

}
 
Example #10
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, EventListener listener)
		throws NacosException {

}
 
Example #11
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, String groupName, List<String> clusters,
		EventListener listener) throws NacosException {

}
 
Example #12
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, List<String> clusters,
		EventListener listener) throws NacosException {

}
 
Example #13
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, String groupName, EventListener listener)
		throws NacosException {

}
 
Example #14
Source File: MockNamingService.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, EventListener listener)
		throws NacosException {

}
 
Example #15
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, String groupName, List<String> clusters,
		EventListener listener) throws NacosException {
	delegate.unsubscribe(serviceName, groupName, clusters, listener);
}
 
Example #16
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, List<String> clusters,
		EventListener listener) throws NacosException {
	delegate.unsubscribe(serviceName, clusters, listener);
}
 
Example #17
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, String groupName, EventListener listener)
		throws NacosException {
	delegate.unsubscribe(serviceName, groupName, listener);
}
 
Example #18
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void unsubscribe(String serviceName, EventListener listener)
		throws NacosException {
	delegate.unsubscribe(serviceName, listener);
}
 
Example #19
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, String groupName, List<String> clusters,
		EventListener listener) throws NacosException {
	delegate.subscribe(serviceName, groupName, clusters, listener);
}
 
Example #20
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, List<String> clusters,
		EventListener listener) throws NacosException {
	delegate.subscribe(serviceName, clusters, listener);
}
 
Example #21
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, String groupName, EventListener listener)
		throws NacosException {
	delegate.subscribe(serviceName, groupName, listener);
}
 
Example #22
Source File: DelegatingNamingService.java    From nacos-spring-project with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(String serviceName, EventListener listener)
		throws NacosException {
	delegate.subscribe(serviceName, listener);
}
 
Example #23
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public EventListener removeListener() {
    EventListener res = listener;
    this.listener = null;
    return res;
}
 
Example #24
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public void setListener(EventListener listener) {
    this.listener = listener;
}
 
Example #25
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public EventListener getListener() {
    return listener;
}