Search in sources :

Example 11 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class ServiceInstancesChangedListener method onEvent.

/**
 * On {@link ServiceInstancesChangedEvent the service instances change event}
 *
 * @param event {@link ServiceInstancesChangedEvent}
 */
public synchronized void onEvent(ServiceInstancesChangedEvent event) {
    logger.info("Received instance notification, serviceName: " + event.getServiceName() + ", instances: " + event.getServiceInstances().size());
    String appName = event.getServiceName();
    allInstances.put(appName, event.getServiceInstances());
    if (logger.isDebugEnabled()) {
        logger.debug(event.getServiceInstances().toString());
    }
    Map<String, List<ServiceInstance>> revisionToInstances = new HashMap<>();
    Map<String, Set<String>> localServiceToRevisions = new HashMap<>();
    Map<Set<String>, List<URL>> revisionsToUrls = new HashMap();
    Map<String, List<URL>> tmpServiceUrls = new HashMap<>();
    for (Map.Entry<String, List<ServiceInstance>> entry : allInstances.entrySet()) {
        List<ServiceInstance> instances = entry.getValue();
        for (ServiceInstance instance : instances) {
            String revision = getExportedServicesRevision(instance);
            if (DEFAULT_REVISION.equals(revision)) {
                logger.info("Find instance without valid service metadata: " + instance.getAddress());
                continue;
            }
            List<ServiceInstance> subInstances = revisionToInstances.computeIfAbsent(revision, r -> new LinkedList<>());
            subInstances.add(instance);
            MetadataInfo metadata = revisionToMetadata.get(revision);
            if (metadata == null) {
                metadata = getMetadataInfo(instance);
                logger.info("MetadataInfo for instance " + instance.getAddress() + "?revision=" + revision + " is " + metadata);
                if (metadata != null) {
                    revisionToMetadata.put(revision, metadata);
                } else {
                }
            }
            if (metadata != null) {
                parseMetadata(revision, metadata, localServiceToRevisions);
                ((DefaultServiceInstance) instance).setServiceMetadata(metadata);
            }
        // else {
        // logger.error("Failed to load service metadata for instance " + instance);
        // Set<String> set = localServiceToRevisions.computeIfAbsent(url.getServiceKey(), k -> new TreeSet<>());
        // set.add(revision);
        // }
        }
        localServiceToRevisions.forEach((serviceKey, revisions) -> {
            List<URL> urls = revisionsToUrls.get(revisions);
            if (urls != null) {
                tmpServiceUrls.put(serviceKey, urls);
            } else {
                urls = new ArrayList<>();
                for (String r : revisions) {
                    for (ServiceInstance i : revisionToInstances.get(r)) {
                        urls.add(i.toURL());
                    }
                }
                revisionsToUrls.put(revisions, urls);
                tmpServiceUrls.put(serviceKey, urls);
            }
        });
    }
    this.serviceUrls = tmpServiceUrls;
    this.notifyAddressChanged();
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) URL(org.apache.dubbo.common.URL) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class SofaRegistryServiceDiscovery method handleRegistryData.

private List<ServiceInstance> handleRegistryData(String dataId, UserData userData, ServiceInstancesChangedListener listener, CountDownLatch latch) {
    try {
        List<String> datas = getUserData(dataId, userData);
        List<ServiceInstance> serviceInstances = new ArrayList<>(datas.size());
        for (String serviceData : datas) {
            SofaRegistryInstance sri = gson.fromJson(serviceData, SofaRegistryInstance.class);
            DefaultServiceInstance serviceInstance = new DefaultServiceInstance(sri.getId(), dataId, sri.getHost(), sri.getPort());
            serviceInstance.setMetadata(sri.getMetadata());
            serviceInstances.add(serviceInstance);
        }
        if (null != listener) {
            listener.onEvent(new ServiceInstancesChangedEvent(dataId, serviceInstances));
        }
        return serviceInstances;
    } finally {
        if (null != latch) {
            latch.countDown();
        }
    }
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ArrayList(java.util.ArrayList) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)

Example 13 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance 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 14 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance 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)

Example 15 with ServiceInstance

use of org.apache.dubbo.registry.client.ServiceInstance in project dubbo by alibaba.

the class EtcdServiceDiscoveryTest method testUnRegistry.

@Test
public void testUnRegistry() throws Exception {
    ServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest2Service", "127.0.0.1", 8080);
    Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    etcdServiceDiscovery.register(serviceInstance);
    Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    etcdServiceDiscovery.unregister(serviceInstance);
    Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)16 DefaultServiceInstance (org.apache.dubbo.registry.client.DefaultServiceInstance)11 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 ServiceInstancesChangedEvent (org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 LinkedList (java.util.LinkedList)3 ServiceInstancesChangedListener (org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 URL (org.apache.dubbo.common.URL)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)2 NacosNamingServiceUtils.toInstance (org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toInstance)2 Gson (com.google.gson.Gson)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 Application (com.netflix.discovery.shared.Application)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1