Java源码示例:com.alibaba.nacos.api.naming.listener.NamingEvent

示例1
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();
    }
}
 
示例2
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");
}
 
示例3
@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);
    });
}
 
示例4
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);
}
 
示例5
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);
		}
	}
}
 
示例6
@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);
            }
        });
    });
}
 
示例7
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);
}