com.alibaba.nacos.api.naming.listener.NamingEvent Java Examples
The following examples show how to use
com.alibaba.nacos.api.naming.listener.NamingEvent.
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: ServiceConsumer.java From nacos-tutorial with Apache License 2.0 | 6 votes |
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 #2
Source File: ServiceConsumer.java From nacos-tutorial with Apache License 2.0 | 6 votes |
public static void printInstances(NamingEvent namingEvent) { List<Instance> instanceList = namingEvent.getInstances(); StringBuilder sb = new StringBuilder(); sb.append("[\n"); int i = 0, s = instanceList.size(); for (Instance instance : instanceList) { sb.append("\t").append(instance); if (i++ < s - 1) { sb.append(",").append("\n"); } } sb.append("\n]"); System.out.println("===========receive new service===========\nserviceName=" + namingEvent.getServiceName() + "\ninstance size=" + instanceList.size() + "\ninstance list=" + sb.toString() + "\n=========================================\n"); }
Example #3
Source File: NacosRegistry.java From joyrpc with Apache License 2.0 | 6 votes |
@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 #4
Source File: NacosRegistry.java From dubbo-registry-nacos with Apache License 2.0 | 6 votes |
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 #5
Source File: DubboServiceDiscoveryAutoConfiguration.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
private void subscribeEventListener(String serviceName) { if (listeningServices.add(serviceName)) { try { String group = nacosDiscoveryProperties.getGroup(); namingService.subscribe(serviceName, group, event -> { if (event instanceof NamingEvent) { NamingEvent namingEvent = (NamingEvent) event; List<ServiceInstance> serviceInstances = hostToServiceInstanceList( namingEvent.getInstances(), serviceName); dispatchServiceInstancesChangedEvent(serviceName, serviceInstances); } }); } catch (NacosException e) { ReflectionUtils.rethrowRuntimeException(e); } } }
Example #6
Source File: NacosServiceDiscovery.java From dubbo-registry-nacos with Apache License 2.0 | 5 votes |
@Override public void addServiceInstancesChangedListener(ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException { execute(namingService, service -> { service.subscribe(listener.getServiceName(), e -> { // Register Nacos EventListener if (e instanceof NamingEvent) { NamingEvent event = (NamingEvent) e; handleEvent(event, listener); } }); }); }
Example #7
Source File: NacosServiceDiscovery.java From dubbo-registry-nacos with Apache License 2.0 | 5 votes |
private void handleEvent(NamingEvent event, ServiceInstancesChangedListener listener) { String serviceName = event.getServiceName(); Collection<ServiceInstance> serviceInstances = event.getInstances() .stream() .map(NacosNamingServiceUtils::toServiceInstance) .collect(Collectors.toList()); dispatchServiceInstancesChangedEvent(serviceName, serviceInstances); }