Search in sources :

Example 16 with NotifyListener

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

the class MultipleRegistry2S2RTest method testSubscription.

@Test
public void testSubscription() throws InterruptedException {
    URL serviceUrl = URL.valueOf("http2://multiple/" + SERVICE2_NAME + "?notify=false&methods=test1,test2&category=providers");
    // URL serviceUrl2 = URL.valueOf("http2://multiple2/" + SERVICE_NAME + "?notify=false&methods=test1,test2&category=providers");
    multipleRegistry.register(serviceUrl);
    String path = "/dubbo/" + SERVICE2_NAME + "/providers";
    List<String> providerList = zookeeperClient.getChildren(path);
    Assertions.assertTrue(!providerList.isEmpty());
    System.out.println(providerList.get(0));
    Assertions.assertNotNull(MultipleRegistryTestUtil.getRedisHashContent(redisServerPort, path, serviceUrl.toFullString()));
    final List<URL> list = new ArrayList<URL>();
    multipleRegistry.subscribe(serviceUrl, new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
            System.out.println("invoke notify: " + urls);
            list.clear();
            list.addAll(urls);
        }
    });
    Thread.sleep(1500);
    Assertions.assertEquals(2, list.size());
    List<Registry> serviceRegistries = new ArrayList<Registry>(multipleRegistry.getServiceRegistries().values());
    serviceRegistries.get(0).unregister(serviceUrl);
    Thread.sleep(1500);
    Assertions.assertEquals(1, list.size());
    List<URL> urls = MultipleRegistryTestUtil.getProviderURLsFromNotifyURLS(list);
    Assertions.assertEquals(1, list.size());
    Assertions.assertTrue(!"empty".equals(list.get(0).getProtocol()));
    serviceRegistries.get(1).unregister(serviceUrl);
    Thread.sleep(1500);
    Assertions.assertEquals(1, list.size());
    urls = MultipleRegistryTestUtil.getProviderURLsFromNotifyURLS(list);
    Assertions.assertEquals(1, list.size());
    Assertions.assertEquals("empty", list.get(0).getProtocol());
}
Also used : ArrayList(java.util.ArrayList) ZookeeperRegistry(org.apache.dubbo.registry.zookeeper.ZookeeperRegistry) RedisRegistry(org.apache.dubbo.registry.redis.RedisRegistry) Registry(org.apache.dubbo.registry.Registry) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 17 with NotifyListener

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

the class ZookeeperRegistry method fetchLatestAddresses.

/**
 * When zookeeper connection recovered from a connection loss, it need to fetch the latest provider list.
 * re-register watcher is only a side effect and is not mandate.
 */
private void fetchLatestAddresses() {
    // subscribe
    Map<URL, Set<NotifyListener>> recoverSubscribed = new HashMap<URL, Set<NotifyListener>>(getSubscribed());
    if (!recoverSubscribed.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Fetching the latest urls of " + recoverSubscribed.keySet());
        }
        for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                removeFailedSubscribed(url, listener);
                addFailedSubscribed(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 18 with NotifyListener

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

the class ZookeeperRegistry method doUnsubscribe.

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
    ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
    if (listeners != null) {
        ChildListener zkListener = listeners.remove(listener);
        if (zkListener != null) {
            if (ANY_VALUE.equals(url.getServiceInterface())) {
                String root = toRootPath();
                zkClient.removeChildListener(root, zkListener);
            } else {
                for (String path : toCategoriesPath(url)) {
                    zkClient.removeChildListener(path, zkListener);
                }
            }
        }
        if (listeners.isEmpty()) {
            zkListeners.remove(url);
        }
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 19 with NotifyListener

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

the class NacosRegistryTest method testIsConformRules.

@Test
public void testIsConformRules() {
    NamingService namingService = mock(NacosNamingService.class);
    URL serviceUrlWithoutCategory = URL.valueOf("nacos://127.0.0.1:3333/" + serviceInterface + "?interface=" + serviceInterface + "&notify=false&methods=test1,test2&version=1.0.0&group=default");
    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);
        String serviceNameWithoutVersion = "providers:org.apache.dubbo.registry.nacos.NacosService:default";
        String serviceName1 = "providers:org.apache.dubbo.registry.nacos.NacosService:1.0.0:default";
        List<String> serviceNames = new ArrayList<>();
        serviceNames.add(serviceNameWithoutVersion);
        serviceNames.add(serviceName1);
        ListView<String> result = new ListView<>();
        result.setData(serviceNames);
        when(namingService.getServicesOfServer(1, Integer.MAX_VALUE, registryUrl.getParameter(GROUP_KEY, Constants.DEFAULT_GROUP))).thenReturn(result);
    } catch (NacosException e) {
    // ignore
    }
    NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(namingService);
    nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
    Set<URL> registered;
    nacosRegistry.register(this.serviceUrl);
    nacosRegistry.register(serviceUrlWithoutCategory);
    registered = nacosRegistry.getRegistered();
    assertThat(registered.contains(serviceUrl), is(true));
    assertThat(registered.contains(serviceUrlWithoutCategory), is(true));
    assertThat(registered.size(), is(2));
    URL serviceUrlWithWildcard = URL.valueOf("nacos://127.0.0.1:3333/" + serviceInterface + "?interface=org.apache.dubbo.registry.nacos.NacosService" + "&notify=false&methods=test1,test2&category=providers&version=*&group=default");
    URL serviceUrlWithOutWildcard = URL.valueOf("nacos://127.0.0.1:3333/" + serviceInterface + "?interface=org.apache.dubbo.registry.nacos.NacosService" + "&notify=false&methods=test1,test2&category=providers&version=1.0.0&group=default");
    NotifyListener listener = mock(NotifyListener.class);
    nacosRegistry.subscribe(serviceUrlWithWildcard, listener);
    nacosRegistry.subscribe(serviceUrlWithOutWildcard, listener);
    Map<URL, Set<NotifyListener>> subscribed = nacosRegistry.getSubscribed();
    assertThat(subscribed.size(), is(2));
    assertThat(subscribed.get(serviceUrlWithOutWildcard).size(), is(1));
    assertThat(subscribed.size(), is(2));
    assertThat(subscribed.get(serviceUrlWithWildcard).size(), is(1));
}
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) ListView(com.alibaba.nacos.api.naming.pojo.ListView) 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 20 with NotifyListener

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

the class NacosRegistryTest method testSubscribe.

@Test
public void testSubscribe() {
    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));
}
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)

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