Search in sources :

Example 1 with NacosException

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

the class NacosDiscoveryService method getServiceInstances.

@Override
public List<ServiceDTO> getServiceInstances(String serviceId) {
    try {
        List<Instance> instances = namingService.selectInstances(serviceId, true);
        List<ServiceDTO> serviceDTOList = Lists.newLinkedList();
        instances.forEach(instance -> {
            ServiceDTO serviceDTO = this.toServiceDTO(instance, serviceId);
            serviceDTOList.add(serviceDTO);
        });
        return serviceDTOList;
    } catch (NacosException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return Collections.emptyList();
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with NacosException

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

the class NacosRegistryTest method testUnRegister.

@Test
public void testUnRegister() {
    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()));
        doNothing().when(namingService).registerInstance(serviceName, Constants.DEFAULT_GROUP, instance);
        doNothing().when(namingService).deregisterInstance(serviceName, Constants.DEFAULT_GROUP, ip, port);
    } catch (NacosException e) {
    // ignore
    }
    NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(namingService);
    nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
    nacosRegistry.register(serviceUrl);
    Set<URL> registered = nacosRegistry.getRegistered();
    assertThat(registered.contains(serviceUrl), is(true));
    assertThat(registered.size(), is(1));
    nacosRegistry.unregister(serviceUrl);
    assertThat(registered.contains(serviceUrl), is(false));
    assertThat(registered.size(), is(0));
}
Also used : NacosNamingService(com.alibaba.nacos.client.naming.NacosNamingService) NamingService(com.alibaba.nacos.api.naming.NamingService) Instance(com.alibaba.nacos.api.naming.pojo.Instance) URL(org.apache.dubbo.common.URL) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.jupiter.api.Test)

Example 3 with NacosException

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

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

the class NacosRegistryTest method testRegister.

@Test
public void testRegister() {
    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()));
        doNothing().when(namingService).registerInstance(serviceName, Constants.DEFAULT_GROUP, instance);
    } catch (NacosException e) {
    // ignore
    }
    NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(namingService);
    nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
    Set<URL> registered;
    for (int i = 0; i < 2; i++) {
        nacosRegistry.register(serviceUrl);
        registered = nacosRegistry.getRegistered();
        assertThat(registered.contains(serviceUrl), is(true));
    }
    registered = nacosRegistry.getRegistered();
    assertThat(registered.size(), is(1));
}
Also used : NacosNamingService(com.alibaba.nacos.client.naming.NacosNamingService) NamingService(com.alibaba.nacos.api.naming.NamingService) Instance(com.alibaba.nacos.api.naming.pojo.Instance) URL(org.apache.dubbo.common.URL) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.jupiter.api.Test)

Example 5 with NacosException

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

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