Search in sources :

Example 6 with Instance

use of com.alibaba.nacos.api.naming.pojo.Instance 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 7 with Instance

use of com.alibaba.nacos.api.naming.pojo.Instance 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)

Example 8 with Instance

use of com.alibaba.nacos.api.naming.pojo.Instance 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 9 with Instance

use of com.alibaba.nacos.api.naming.pojo.Instance in project dubbo by alibaba.

the class NacosRegistry method doRegister.

@Override
public void doRegister(URL url) {
    final String serviceName = getServiceName(url);
    final Instance instance = createInstance(url);
    /**
     *  namingService.registerInstance with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
     *  default {@link DEFAULT_GROUP}
     *
     * in https://github.com/apache/dubbo/issues/5978
     */
    execute(namingService -> namingService.registerInstance(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), instance));
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 10 with Instance

use of com.alibaba.nacos.api.naming.pojo.Instance in project dubbo by alibaba.

the class NacosRegistry method createInstance.

private Instance createInstance(URL url) {
    // Append default category if absent
    String category = url.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);
    URL newURL = url.addParameter(CATEGORY_KEY, category);
    newURL = newURL.addParameter(PROTOCOL_KEY, url.getProtocol());
    newURL = newURL.addParameter(PATH_KEY, url.getPath());
    newURL = newURL.addParameters(NacosNamingServiceUtils.getNacosPreservedParam(getUrl()));
    String ip = url.getHost();
    int port = url.getPort();
    Instance instance = new Instance();
    instance.setIp(ip);
    instance.setPort(port);
    instance.setMetadata(new HashMap<>(newURL.getParameters()));
    return instance;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) URL(org.apache.dubbo.common.URL)

Aggregations

Instance (com.alibaba.nacos.api.naming.pojo.Instance)15 NacosException (com.alibaba.nacos.api.exception.NacosException)7 URL (org.apache.dubbo.common.URL)7 NamingService (com.alibaba.nacos.api.naming.NamingService)6 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 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)3 ListView (com.alibaba.nacos.api.naming.pojo.ListView)2 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)2 Constants (com.alibaba.nacos.api.common.Constants)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)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