Search in sources :

Example 6 with MetadataInfo

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");
            }
        }
    }
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo)

Example 7 with MetadataInfo

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);
}
Also used : ServiceInfo(org.apache.dubbo.metadata.MetadataInfo.ServiceInfo) MetadataInfo(org.apache.dubbo.metadata.MetadataInfo)

Example 8 with MetadataInfo

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));
                }
            }
        }
    });
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) MetadataParamsFilter(org.apache.dubbo.metadata.MetadataParamsFilter) HashMap(java.util.HashMap) WritableMetadataService(org.apache.dubbo.metadata.WritableMetadataService) InfraAdapter(org.apache.dubbo.common.infra.InfraAdapter)

Example 9 with MetadataInfo

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();
        }
    });
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) MetadataReport(org.apache.dubbo.metadata.report.MetadataReport) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)

Example 10 with MetadataInfo

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));
    }
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) HashMap(java.util.HashMap) URL(org.apache.dubbo.common.URL) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)10 HashMap (java.util.HashMap)4 URL (org.apache.dubbo.common.URL)3 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)3 HashSet (java.util.HashSet)2 ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)2 MetadataReport (org.apache.dubbo.metadata.report.MetadataReport)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)2 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)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 Provider (org.apache.dubbo.admin.model.domain.Provider)1 InfraAdapter (org.apache.dubbo.common.infra.InfraAdapter)1 ServiceInfo (org.apache.dubbo.metadata.MetadataInfo.ServiceInfo)1