use of com.alibaba.nacos.api.naming.listener.Event in project spring-cloud-alibaba by alibaba.
the class NacosWatch method start.
@Override
public void start() {
if (this.running.compareAndSet(false, true)) {
EventListener eventListener = listenerMap.computeIfAbsent(buildKey(), event -> new EventListener() {
@Override
public void onEvent(Event event) {
if (event instanceof NamingEvent) {
List<Instance> instances = ((NamingEvent) event).getInstances();
Optional<Instance> instanceOptional = selectCurrentInstance(instances);
instanceOptional.ifPresent(currentInstance -> {
resetIfNeeded(currentInstance);
});
}
}
});
NamingService namingService = nacosServiceManager.getNamingService(properties.getNacosProperties());
try {
namingService.subscribe(properties.getService(), properties.getGroup(), Arrays.asList(properties.getClusterName()), eventListener);
} catch (Exception e) {
log.error("namingService subscribe failed, properties:{}", properties, e);
}
this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(this::nacosServicesWatch, this.properties.getWatchDelay());
}
}
use of com.alibaba.nacos.api.naming.listener.Event in project sofa-rpc by sofastack.
the class NacosRegistry method subscribe.
@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
String appName = config.getAppName();
if (!registryConfig.isSubscribe()) {
// registry ignored
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
}
return null;
}
if (config.isSubscribe()) {
String serviceName = NacosRegistryHelper.buildServiceName(config, config.getProtocol());
if (LOGGER.isInfoEnabled()) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_SUB, serviceName));
}
try {
if (providerObserver == null) {
providerObserver = new NacosRegistryProviderObserver();
}
ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
providerObserver.addProviderListener(config, providerInfoListener);
EventListener eventListener = new EventListener() {
@Override
public void onEvent(Event event) {
if (event instanceof NamingEvent) {
NamingEvent namingEvent = (NamingEvent) event;
List<Instance> instances = namingEvent.getInstances();
// avoid npe
if (null == instances) {
instances = new ArrayList<Instance>();
}
instances.removeIf(i -> !i.isEnabled());
providerObserver.updateProviders(config, instances);
}
}
};
namingService.subscribe(serviceName, defaultCluster, eventListener);
consumerListeners.put(config, eventListener);
List<Instance> allInstances = namingService.getAllInstances(serviceName, defaultCluster);
List<ProviderInfo> providerInfos = NacosRegistryHelper.convertInstancesToProviders(allInstances);
List<ProviderInfo> matchProviders = RegistryUtils.matchProviderInfos(config, providerInfos);
return Collections.singletonList(new ProviderGroup().addAll(matchProviders));
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
}
}
return null;
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class NacosNamingServiceTest method testUnSubscribe3.
@Test
public void testUnSubscribe3() throws NacosException {
// given
String serviceName = "service1";
List<String> clusterList = Arrays.asList("cluster1", "cluster2");
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
}
};
when(changeNotifier.isSubscribed(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2")).thenReturn(false);
// when
client.unsubscribe(serviceName, clusterList, listener);
// then
verify(changeNotifier, times(1)).deregisterListener(Constants.DEFAULT_GROUP, serviceName, "cluster1,cluster2", listener);
verify(proxy, times(1)).unsubscribe(serviceName, Constants.DEFAULT_GROUP, "cluster1,cluster2");
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class NacosNamingServiceTest method testSubscribe2.
@Test
public void testSubscribe2() throws NacosException {
// given
String serviceName = "service1";
String groupName = "group1";
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
}
};
// when
client.subscribe(serviceName, groupName, listener);
// then
verify(changeNotifier, times(1)).registerListener(groupName, serviceName, "", listener);
verify(proxy, times(1)).subscribe(serviceName, groupName, "");
}
use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.
the class NacosNamingServiceTest method testSubscribe4.
@Test
public void testSubscribe4() throws NacosException {
// given
String serviceName = "service1";
String groupName = "group1";
List<String> clusterList = Arrays.asList("cluster1", "cluster2");
EventListener listener = new EventListener() {
@Override
public void onEvent(Event event) {
}
};
// when
client.subscribe(serviceName, groupName, clusterList, listener);
// then
verify(changeNotifier, times(1)).registerListener(groupName, serviceName, "cluster1,cluster2", listener);
verify(proxy, times(1)).subscribe(serviceName, groupName, "cluster1,cluster2");
}
Aggregations