Search in sources :

Example 6 with DefaultServiceInstance

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

the class ZookeeperServiceDiscoveryTest method testRegistration.

@Test
public void testRegistration() throws InterruptedException {
    DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());
    CountDownLatch latch = new CountDownLatch(1);
    // Add Listener
    discovery.addServiceInstancesChangedListener(new ServiceInstancesChangedListener(Sets.newSet(SERVICE_NAME), discovery) {

        @Override
        public void onEvent(ServiceInstancesChangedEvent event) {
            latch.countDown();
        }
    });
    discovery.register(serviceInstance);
    latch.await();
    List<ServiceInstance> serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertTrue(serviceInstances.contains(serviceInstance));
    assertEquals(asList(serviceInstance), serviceInstances);
    Map<String, String> metadata = new HashMap<>();
    // metadata.put("message", "Hello,World");
    serviceInstance.setMetadata(metadata);
    discovery.update(serviceInstance);
    serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertEquals(serviceInstance, serviceInstances.get(0));
    discovery.unregister(serviceInstance);
    serviceInstances = discovery.getInstances(SERVICE_NAME);
    assertTrue(serviceInstances.isEmpty());
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) HashMap(java.util.HashMap) ServiceInstancesChangedEvent(org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceInstancesChangedListener(org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener) Test(org.junit.jupiter.api.Test)

Example 7 with DefaultServiceInstance

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

the class CuratorFrameworkUtils method build.

public static ServiceInstance build(org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> instance) {
    String name = instance.getName();
    String host = instance.getAddress();
    int port = instance.getPort();
    ZookeeperInstance zookeeperInstance = instance.getPayload();
    DefaultServiceInstance serviceInstance = new DefaultServiceInstance(instance.getId(), name, host, port);
    serviceInstance.setMetadata(zookeeperInstance.getMetadata());
    return serviceInstance;
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ZookeeperInstance(org.apache.dubbo.registry.zookeeper.ZookeeperInstance)

Example 8 with DefaultServiceInstance

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

the class DubboBootstrap method createServiceInstance.

private ServiceInstance createServiceInstance(String serviceName, String host, int port) {
    this.serviceInstance = new DefaultServiceInstance(serviceName, host, port);
    setMetadataStorageType(serviceInstance, getMetadataType());
    ExtensionLoader<ServiceInstanceCustomizer> loader = ExtensionLoader.getExtensionLoader(ServiceInstanceCustomizer.class);
    // FIXME, sort customizer before apply
    loader.getSupportedExtensionInstances().forEach(customizer -> {
        // customizes
        customizer.customize(this.serviceInstance);
    });
    return this.serviceInstance;
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstanceCustomizer(org.apache.dubbo.registry.client.ServiceInstanceCustomizer)

Example 9 with DefaultServiceInstance

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

the class ServiceInstancePortCustomizer method customize.

@Override
public void customize(ServiceInstance serviceInstance) {
    if (serviceInstance.getPort() != null) {
        return;
    }
    Collection<ProtocolConfig> protocols = ApplicationModel.getConfigManager().getProtocols();
    if (CollectionUtils.isEmpty(protocols)) {
        throw new IllegalStateException("We should have at least one protocol configured at this point.");
    }
    Stream<ProtocolConfig> protocolStream = protocols.stream();
    ProtocolConfig protocolConfig = protocolStream.filter(protocol -> "rest".equals(protocol.getName())).findFirst().orElseGet(() -> protocolStream.findFirst().get());
    if (serviceInstance instanceof DefaultServiceInstance) {
        DefaultServiceInstance instance = (DefaultServiceInstance) serviceInstance;
        if (protocolConfig.getPort() != null) {
            instance.setPort(protocolConfig.getPort());
        }
    }
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig)

Example 10 with DefaultServiceInstance

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

Aggregations

DefaultServiceInstance (org.apache.dubbo.registry.client.DefaultServiceInstance)16 Test (org.junit.jupiter.api.Test)8 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 URL (org.apache.dubbo.common.URL)2 ServiceInstancesChangedEvent (org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)2 Gson (com.google.gson.Gson)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)1 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)1 ServiceInstanceCustomizer (org.apache.dubbo.registry.client.ServiceInstanceCustomizer)1 ServiceInstancesChangedListener (org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)1 ZookeeperInstance (org.apache.dubbo.registry.zookeeper.ZookeeperInstance)1