Search in sources :

Example 6 with NacosException

use of com.alibaba.nacos.api.exception.NacosException 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 7 with NacosException

use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.

the class NacosNamingServiceUtils method createNamingService.

/**
 * Create an instance of {@link NamingService} from specified {@link URL connection url}
 *
 * @param connectionURL {@link URL connection url}
 * @return {@link NamingService}
 * @since 2.7.5
 */
public static NacosNamingServiceWrapper createNamingService(URL connectionURL) {
    Properties nacosProperties = buildNacosProperties(connectionURL);
    NamingService namingService;
    try {
        namingService = NacosFactory.createNamingService(nacosProperties);
    } catch (NacosException e) {
        if (logger.isErrorEnabled()) {
            logger.error(e.getErrMsg(), e);
        }
        throw new IllegalStateException(e);
    }
    return new NacosNamingServiceWrapper(namingService);
}
Also used : NacosNamingServiceWrapper(org.apache.dubbo.registry.nacos.NacosNamingServiceWrapper) NamingService(com.alibaba.nacos.api.naming.NamingService) Properties(java.util.Properties) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 8 with NacosException

use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.

the class NacosServiceDiscovery method addServiceInstancesChangedListener.

@Override
public void addServiceInstancesChangedListener(ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException {
    execute(namingService, service -> {
        listener.getServiceNames().forEach(serviceName -> {
            try {
                service.subscribe(serviceName, e -> {
                    // Register Nacos EventListener
                    if (e instanceof NamingEvent) {
                        NamingEvent event = (NamingEvent) e;
                        handleEvent(event, listener);
                    }
                });
            } catch (NacosException e) {
                e.printStackTrace();
            }
        });
    });
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 9 with NacosException

use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.

the class NacosRegistry method subscribeEventListener.

private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener) throws NacosException {
    ConcurrentMap<NotifyListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
    EventListener nacosListener = listeners.computeIfAbsent(listener, k -> {
        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);
            }
        };
        return eventListener;
    });
    namingService.subscribe(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), nacosListener);
}
Also used : Arrays(java.util.Arrays) INTERFACE_KEY(org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY) NacosInstanceManageUtil(org.apache.dubbo.registry.nacos.util.NacosInstanceManageUtil) URLBuilder(org.apache.dubbo.common.URLBuilder) URL(org.apache.dubbo.common.URL) EMPTY_PROTOCOL(org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Map(java.util.Map) PROVIDERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY) FailbackRegistry(org.apache.dubbo.registry.support.FailbackRegistry) ROUTERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY) ListView(com.alibaba.nacos.api.naming.pojo.ListView) Collection(java.util.Collection) DEFAULT_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NacosServiceName.valueOf(org.apache.dubbo.registry.nacos.NacosServiceName.valueOf) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Constants(com.alibaba.nacos.api.common.Constants) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) NAME_SEPARATOR(org.apache.dubbo.registry.nacos.NacosServiceName.NAME_SEPARATOR) Registry(org.apache.dubbo.registry.Registry) UrlUtils(org.apache.dubbo.common.utils.UrlUtils) HashMap(java.util.HashMap) NotifyListener(org.apache.dubbo.registry.NotifyListener) StringUtils(org.apache.dubbo.common.utils.StringUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Lists(com.google.common.collect.Lists) CONFIGURATORS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY) NacosException(com.alibaba.nacos.api.exception.NacosException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ADMIN_PROTOCOL(org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL) PROTOCOL_KEY(org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) PATH_KEY(org.apache.dubbo.common.constants.CommonConstants.PATH_KEY) CONSUMERS_CATEGORY(org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY) NacosNamingServiceUtils(org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils) CATEGORY_KEY(org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY) TimeUnit(java.util.concurrent.TimeUnit) GROUP_KEY(org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY) ANY_VALUE(org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE) VERSION_KEY(org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY) Collections(java.util.Collections) NamingService(com.alibaba.nacos.api.naming.NamingService) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 10 with NacosException

use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.

the class NacosDynamicConfigurationTest method setUp.

@BeforeAll
public static void setUp() {
    String urlForDubbo = "nacos://" + "127.0.0.1:8848" + "/org.apache.dubbo.nacos.testService";
    // timeout in 15 seconds.
    URL url = URL.valueOf(urlForDubbo).addParameter(SESSION_TIMEOUT_KEY, 15000);
    config = new NacosDynamicConfiguration(url);
    try {
        nacosClient = NacosFactory.createConfigService("127.0.0.1:8848");
    } catch (NacosException e) {
        e.printStackTrace();
    }
}
Also used : URL(org.apache.dubbo.common.URL) NacosException(com.alibaba.nacos.api.exception.NacosException) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)10 NamingService (com.alibaba.nacos.api.naming.NamingService)7 Instance (com.alibaba.nacos.api.naming.pojo.Instance)7 URL (org.apache.dubbo.common.URL)7 NacosNamingService (com.alibaba.nacos.client.naming.NacosNamingService)5 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 NotifyListener (org.apache.dubbo.registry.NotifyListener)3 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)2 ListView (com.alibaba.nacos.api.naming.pojo.ListView)2 Constants (com.alibaba.nacos.api.common.Constants)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1