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