Search in sources :

Example 1 with Event

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());
    }
}
Also used : ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) SmartLifecycle(org.springframework.context.SmartLifecycle) AtomicLong(java.util.concurrent.atomic.AtomicLong) NacosDiscoveryProperties(com.alibaba.cloud.nacos.NacosDiscoveryProperties) List(java.util.List) ObjectProvider(org.springframework.beans.factory.ObjectProvider) Event(com.alibaba.nacos.api.naming.listener.Event) Instance(com.alibaba.nacos.api.naming.pojo.Instance) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Map(java.util.Map) DisposableBean(org.springframework.beans.factory.DisposableBean) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Optional(java.util.Optional) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) NacosServiceManager(com.alibaba.cloud.nacos.NacosServiceManager) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) NamingService(com.alibaba.nacos.api.naming.NamingService) Optional(java.util.Optional) NamingService(com.alibaba.nacos.api.naming.NamingService) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) List(java.util.List) EventListener(com.alibaba.nacos.api.naming.listener.EventListener)

Example 2 with Event

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;
}
Also used : ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) Instance(com.alibaba.nacos.api.naming.pojo.Instance) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) NacosException(com.alibaba.nacos.api.exception.NacosException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) EventListener(com.alibaba.nacos.api.naming.listener.EventListener)

Example 3 with Event

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");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 4 with Event

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, "");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 5 with Event

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");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Aggregations

Event (com.alibaba.nacos.api.naming.listener.Event)29 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)28 Test (org.junit.Test)26 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 NamingService (com.alibaba.nacos.api.naming.NamingService)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 Properties (java.util.Properties)2 NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)1 NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 AbstractEventListener (com.alibaba.nacos.api.naming.listener.AbstractEventListener)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Arrays (java.util.Arrays)1