Search in sources :

Example 11 with Instance

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

the class NacosServiceDiscovery method doRegister.

@Override
public void doRegister(ServiceInstance serviceInstance) {
    execute(namingService, service -> {
        Instance instance = toInstance(serviceInstance);
        appendPreservedParam(instance);
        service.registerInstance(instance.getServiceName(), group, instance);
    });
}
Also used : NacosNamingServiceUtils.toInstance(org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 12 with Instance

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

the class NacosRegistry method doUnregister.

@Override
public void doUnregister(final URL url) {
    execute(namingService -> {
        String serviceName = getServiceName(url);
        Instance instance = createInstance(url);
        namingService.deregisterInstance(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), instance.getIp(), instance.getPort());
    });
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 13 with Instance

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

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

the class NacosServiceDiscovery method unregister.

@Override
public void unregister(ServiceInstance serviceInstance) throws RuntimeException {
    execute(namingService, service -> {
        Instance instance = toInstance(serviceInstance);
        service.deregisterInstance(instance.getServiceName(), group, instance);
    });
}
Also used : NacosNamingServiceUtils.toInstance(org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 15 with Instance

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

the class NacosNamingServiceUtils method toInstance.

/**
 * Convert the {@link ServiceInstance} to {@link Instance}
 *
 * @param serviceInstance {@link ServiceInstance}
 * @return non-null
 * @since 2.7.5
 */
public static Instance toInstance(ServiceInstance serviceInstance) {
    Instance instance = new Instance();
    instance.setInstanceId(serviceInstance.getId());
    instance.setServiceName(serviceInstance.getServiceName());
    instance.setIp(serviceInstance.getHost());
    instance.setPort(serviceInstance.getPort());
    instance.setMetadata(serviceInstance.getMetadata());
    instance.setEnabled(serviceInstance.isEnabled());
    instance.setHealthy(serviceInstance.isHealthy());
    return instance;
}
Also used : ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) Instance(com.alibaba.nacos.api.naming.pojo.Instance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance)

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