Search in sources :

Example 21 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class NacosRegistryTest method testUnSubscribe.

@Test
public void testUnSubscribe() {
    NamingService namingService = mock(NacosNamingService.class);
    try {
        String serviceName = "providers:org.apache.dubbo.registry.nacos.NacosService:1.0.0:default";
        String category = this.serviceUrl.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);
        URL newUrl = this.serviceUrl.addParameter(CATEGORY_KEY, category);
        newUrl = newUrl.addParameter(PROTOCOL_KEY, this.serviceUrl.getProtocol());
        newUrl = newUrl.addParameter(PATH_KEY, this.serviceUrl.getPath());
        newUrl = newUrl.addParameters(NacosNamingServiceUtils.getNacosPreservedParam(this.serviceUrl));
        String ip = newUrl.getHost();
        int port = newUrl.getPort();
        Instance instance = new Instance();
        instance.setIp(ip);
        instance.setPort(port);
        instance.setMetadata(new HashMap<>(newUrl.getParameters()));
        List<Instance> instances = new ArrayList<>();
        instances.add(instance);
        when(namingService.getAllInstances(serviceName, this.registryUrl.getParameter(GROUP_KEY, Constants.DEFAULT_GROUP))).thenReturn(instances);
    } catch (NacosException e) {
    // ignore
    }
    NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(namingService);
    nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
    NotifyListener listener = mock(NotifyListener.class);
    nacosRegistry.subscribe(serviceUrl, listener);
    Map<URL, Set<NotifyListener>> subscribed = nacosRegistry.getSubscribed();
    assertThat(subscribed.size(), is(1));
    assertThat(subscribed.get(serviceUrl).size(), is(1));
    nacosRegistry.unsubscribe(serviceUrl, listener);
    subscribed = nacosRegistry.getSubscribed();
    assertThat(subscribed.size(), is(1));
    assertThat(subscribed.get(serviceUrl).size(), is(0));
}
Also used : Set(java.util.Set) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosNamingService(com.alibaba.nacos.client.naming.NacosNamingService) NamingService(com.alibaba.nacos.api.naming.NamingService) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 22 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class RedisRegistry method doNotify.

private void doNotify(Collection<String> keys, URL url, Collection<NotifyListener> listeners) {
    if (keys == null || keys.isEmpty() || listeners == null || listeners.isEmpty()) {
        return;
    }
    long now = System.currentTimeMillis();
    List<URL> result = new ArrayList<>();
    List<String> categories = Arrays.asList(url.getParameter(CATEGORY_KEY, new String[0]));
    String consumerService = url.getServiceInterface();
    for (String key : keys) {
        if (!ANY_VALUE.equals(consumerService)) {
            String providerService = toServiceName(key);
            if (!providerService.equals(consumerService)) {
                continue;
            }
        }
        String category = toCategoryName(key);
        if (!categories.contains(ANY_VALUE) && !categories.contains(category)) {
            continue;
        }
        List<URL> urls = new ArrayList<>();
        Set<URL> toDeleteExpireKeys = new HashSet<>(expireCache.keySet());
        Map<String, String> values = redisClient.hgetAll(key);
        if (CollectionUtils.isNotEmptyMap(values)) {
            for (Map.Entry<String, String> entry : values.entrySet()) {
                URL u = URL.valueOf(entry.getKey());
                long expire = Long.parseLong(entry.getValue());
                if (!u.getParameter(DYNAMIC_KEY, true) || expire >= now) {
                    if (UrlUtils.isMatch(url, u)) {
                        urls.add(u);
                        expireCache.put(u, expire);
                        toDeleteExpireKeys.remove(u);
                    }
                }
            }
        }
        if (!toDeleteExpireKeys.isEmpty()) {
            for (URL u : toDeleteExpireKeys) {
                expireCache.remove(u);
            }
        }
        if (urls.isEmpty()) {
            urls.add(URLBuilder.from(url).setProtocol(EMPTY_PROTOCOL).setAddress(ANYHOST_VALUE).setPath(toServiceName(key)).addParameter(CATEGORY_KEY, category).build());
        }
        result.addAll(urls);
        if (logger.isInfoEnabled()) {
            logger.info("redis notify: " + key + " = " + urls);
        }
    }
    if (CollectionUtils.isEmpty(result)) {
        return;
    }
    for (NotifyListener listener : listeners) {
        notify(url, listener, result);
    }
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URL(org.apache.dubbo.common.URL) HashSet(java.util.HashSet) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 23 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class RedisRegistryTest method testSubscribeAndUnsubscribe.

@Test
public void testSubscribeAndUnsubscribe() {
    NotifyListener listener = new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
        }
    };
    redisRegistry.subscribe(SERVICE_URL, listener);
    Map<URL, Set<NotifyListener>> subscribed = redisRegistry.getSubscribed();
    assertThat(subscribed.size(), is(1));
    assertThat(subscribed.get(SERVICE_URL).size(), is(1));
    redisRegistry.unsubscribe(SERVICE_URL, listener);
    subscribed = redisRegistry.getSubscribed();
    assertThat(subscribed.get(SERVICE_URL).size(), is(0));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) List(java.util.List) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 24 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    ConcurrentMap<String, URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (Map.Entry<String, URL> entry : urls.entrySet()) {
            super.unregister(entry.getKey(), entry.getValue());
        }
    }
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<String, NotifyListener> entry : listeners.entrySet()) {
            String service = entry.getKey();
            super.unsubscribe(service, new URL("subscribe", RpcContext.getContext().getRemoteHost(), RpcContext.getContext().getRemotePort(), org.apache.dubbo.registry.RegistryService.class.getName(), getSubscribed(service)), entry.getValue());
        }
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 25 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class SimpleRegistryService method subscribe.

@Override
public void subscribe(String service, URL url, NotifyListener listener) {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("[subscribe] service: " + service + ",client:" + client);
    }
    List<URL> urls = getRegistered().get(service);
    if ((RegistryService.class.getName() + ":0.0.0").equals(service) && (urls == null || urls.size() == 0)) {
        register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), org.apache.dubbo.registry.RegistryService.class.getName(), url.getParameters()));
        List<String> rs = registries;
        if (rs != null && rs.size() > 0) {
            for (String registry : rs) {
                register(service, UrlUtils.parseURL(registry, url.getParameters()));
            }
        }
    }
    super.subscribe(service, url, listener);
    Map<String, NotifyListener> listeners = remoteListeners.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
    listeners.put(service, listener);
    urls = getRegistered().get(service);
    if (urls != null && urls.size() > 0) {
        listener.notify(urls);
    }
}
Also used : URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Aggregations

NotifyListener (org.apache.dubbo.registry.NotifyListener)51 URL (org.apache.dubbo.common.URL)43 Test (org.junit.jupiter.api.Test)29 Set (java.util.Set)28 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 List (java.util.List)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)11 HashSet (java.util.HashSet)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 EMPTY_PROTOCOL (org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL)9 LinkedHashMap (java.util.LinkedHashMap)8 Objects (java.util.Objects)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 HashMap (java.util.HashMap)7 MatcherAssert (org.hamcrest.MatcherAssert)7 Matchers (org.hamcrest.Matchers)7 AfterEach (org.junit.jupiter.api.AfterEach)7