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());
}
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;
}
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;
}
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());
}
}
}
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();
}
Aggregations