use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class ServiceInstanceMetadataUtils method calInstanceRevision.
public static void calInstanceRevision(ServiceDiscovery serviceDiscovery, ServiceInstance instance) {
String registryCluster = serviceDiscovery.getUrl().getParameter(ID_KEY);
if (registryCluster == null) {
return;
}
MetadataInfo metadataInfo = WritableMetadataService.getDefaultExtension().getMetadataInfos().get(registryCluster);
if (metadataInfo != null) {
String existingInstanceRevision = instance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME);
if (!metadataInfo.calAndGetRevision().equals(existingInstanceRevision)) {
instance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, metadataInfo.calAndGetRevision());
if (existingInstanceRevision != null) {
// skip the first registration.
instance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
}
}
}
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class InMemoryWritableMetadataService method exportURL.
@Override
public boolean exportURL(URL url) {
String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
String[] clusters = registryCluster.split(",");
for (String cluster : clusters) {
MetadataInfo metadataInfo = metadataInfos.computeIfAbsent(cluster, k -> {
return new MetadataInfo(ApplicationModel.getName());
});
metadataInfo.addService(new ServiceInfo(url));
}
metadataSemaphore.release();
return addURL(exportedServiceURLs, url);
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class ServiceInstanceMetadataCustomizer method customize.
@Override
public void customize(ServiceInstance serviceInstance) {
ExtensionLoader<MetadataParamsFilter> loader = ExtensionLoader.getExtensionLoader(MetadataParamsFilter.class);
Set<MetadataParamsFilter> paramsFilters = loader.getSupportedExtensionInstances();
WritableMetadataService localMetadataService = WritableMetadataService.getDefaultExtension();
// pick the first interface metadata available.
// FIXME, check the same key in different urls has the same value
MetadataInfo metadataInfo = localMetadataService.getMetadataInfos().values().iterator().next();
MetadataInfo.ServiceInfo serviceInfo = metadataInfo.getServices().values().iterator().next();
Map<String, String> allParams = new HashMap<>(serviceInfo.getUrl().getParameters());
// load instance params users want to load.
// TODO, duplicate logic with that in ApplicationConfig
Set<InfraAdapter> adapters = ExtensionLoader.getExtensionLoader(InfraAdapter.class).getSupportedExtensionInstances();
if (CollectionUtils.isNotEmpty(adapters)) {
Map<String, String> inputParameters = new HashMap<>();
inputParameters.put(APPLICATION_KEY, ApplicationModel.getName());
for (InfraAdapter adapter : adapters) {
Map<String, String> extraParameters = adapter.getExtraAttributes(inputParameters);
if (CollectionUtils.isNotEmptyMap(extraParameters)) {
extraParameters.forEach(allParams::putIfAbsent);
}
}
}
if (CollectionUtils.isEmpty(paramsFilters)) {
serviceInstance.getMetadata().putAll(allParams);
return;
}
paramsFilters.forEach(filter -> {
String[] included = filter.instanceParamsIncluded();
if (included == null) {
serviceInstance.getMetadata().putAll(allParams);
} else {
for (String p : included) {
if (allParams.get(p) != null) {
serviceInstance.getMetadata().put(p, allParams.get(p));
}
}
}
});
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class RemoteMetadataServiceImpl method publishMetadata.
public void publishMetadata(String serviceName) {
Map<String, MetadataInfo> metadataInfos = localMetadataService.getMetadataInfos();
metadataInfos.forEach((registryCluster, metadataInfo) -> {
if (!metadataInfo.hasReported()) {
SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(serviceName, metadataInfo.calAndGetRevision());
metadataInfo.getExtendParams().put(REGISTRY_CLUSTER_KEY, registryCluster);
MetadataReport metadataReport = getMetadataReports().get(registryCluster);
if (metadataReport == null) {
metadataReport = getMetadataReports().entrySet().iterator().next().getValue();
}
logger.info("Publishing metadata to " + metadataReport.getClass().getSimpleName());
if (logger.isDebugEnabled()) {
logger.debug(metadataInfo.toString());
}
metadataReport.publishAppMetadata(identifier, metadataInfo);
metadataInfo.markReported();
}
});
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class FailoverMetadataReportTest method testReadWriteAllMetadataReport.
@Test
public void testReadWriteAllMetadataReport() {
URL url = mockURL.addParameter("strategy", "all");
FailoverMetadataReport report = getFailoverReport(url);
Assertions.assertNotNull(report.getProxyReports(), "metadata reports should not be null.");
Assertions.assertEquals(2, report.getProxyReports().size(), "expect 2 metadata report, actual " + report.getProxyReports().size());
MetadataIdentifier identifier = new MetadataIdentifier("helloService", null, null, null, "test");
ServiceDefinition definition = new ServiceDefinition();
definition.setCanonicalName("helloService");
report.storeProviderMetadata(identifier, definition);
Assertions.assertNotNull(report.getServiceDefinition(identifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getServiceDefinition(identifier));
}
HashMap parameterMap = new HashMap();
report.storeConsumerMetadata(identifier, parameterMap);
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(parameterMap, ((MockMetadataReport) holder.report).consumerMetadata.get(identifier));
}
SubscriberMetadataIdentifier subscribeIdentifier = new SubscriberMetadataIdentifier("test", "1.0");
MetadataInfo metadataInfo = new MetadataInfo(subscribeIdentifier.getApplication(), subscribeIdentifier.getRevision(), null);
report.publishAppMetadata(subscribeIdentifier, metadataInfo);
Assertions.assertEquals(metadataInfo, report.getAppMetadata(subscribeIdentifier, null));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(metadataInfo, holder.report.getAppMetadata(subscribeIdentifier, null));
}
report.registerServiceAppMapping("helloService", "test", null);
Set<String> appNames = report.getServiceAppMapping("helloService", null, null);
Assertions.assertEquals(appNames, report.getServiceAppMapping("helloService", null, null));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(appNames, holder.report.getServiceAppMapping("helloService", null, null));
}
ServiceMetadataIdentifier serviceIdentifier = new ServiceMetadataIdentifier("helloService", null, null, null, "1.0", "dubbo");
report.saveServiceMetadata(serviceIdentifier, url);
Assertions.assertNotNull(report.getExportedURLs(serviceIdentifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getExportedURLs(serviceIdentifier));
}
report.saveSubscribedData(subscribeIdentifier, new HashSet<>());
Assertions.assertNotNull(report.getSubscribedURLs(subscribeIdentifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getSubscribedURLs(subscribeIdentifier));
}
}
Aggregations