Search in sources :

Example 1 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilder method build.

static ServiceDefinition build(ProcessingEnvironment processingEnv, TypeElement type) {
    ServiceDefinition serviceDefinition = new ServiceDefinition();
    serviceDefinition.setCanonicalName(type.toString());
    serviceDefinition.setCodeSource(getResourceName(type.toString()));
    // Get all super types and interface excluding the specified type
    // and then the result will be added into ServiceDefinition#getTypes()
    getHierarchicalTypes(type.asType(), Object.class).stream().map(t -> TypeDefinitionBuilder.build(processingEnv, t)).forEach(serviceDefinition.getTypes()::add);
    // Get all declared methods that will be added into ServiceDefinition#getMethods()
    getPublicNonStaticMethods(type, Object.class).stream().map(method -> MethodDefinitionBuilder.build(processingEnv, method)).forEach(serviceDefinition.getMethods()::add);
    return serviceDefinition;
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) MethodUtils.getPublicNonStaticMethods(org.apache.dubbo.metadata.annotation.processing.util.MethodUtils.getPublicNonStaticMethods) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeUtils.getResourceName(org.apache.dubbo.metadata.annotation.processing.util.TypeUtils.getResourceName) TypeElement(javax.lang.model.element.TypeElement) TypeUtils.getHierarchicalTypes(org.apache.dubbo.metadata.annotation.processing.util.TypeUtils.getHierarchicalTypes) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 2 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class MetadataTest method testRawMap.

/**
 */
@Test
public void testRawMap() {
    TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
    TypeDefinition td = builder.build(ResultWithRawCollections.class, ResultWithRawCollections.class);
    System.out.println(">> testRawMap: " + new Gson().toJson(td));
    Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections", td.getType());
    Assertions.assertEquals(2, td.getProperties().size());
    Assertions.assertEquals("java.util.Map", td.getProperties().get("map").getType());
    Assertions.assertEquals(MapTypeBuilder.class.getName(), td.getProperties().get("map").getTypeBuilderName());
    Assertions.assertEquals("java.util.List", td.getProperties().get("list").getType());
    Assertions.assertEquals(CollectionTypeBuilder.class.getName(), td.getProperties().get("list").getTypeBuilderName());
    ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
    System.out.println(">> testRawMap: " + new Gson().toJson(sd));
    Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
    Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
    boolean containsType = false;
    for (TypeDefinition type : sd.getTypes()) {
        if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections")) {
            containsType = true;
            break;
        }
    }
    Assertions.assertTrue(containsType);
}
Also used : TestService(org.apache.dubbo.metadata.definition.common.TestService) CollectionTypeBuilder(org.apache.dubbo.metadata.definition.builder.CollectionTypeBuilder) Gson(com.google.gson.Gson) MapTypeBuilder(org.apache.dubbo.metadata.definition.builder.MapTypeBuilder) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) TypeDefinition(org.apache.dubbo.metadata.definition.model.TypeDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class FailoverMetadataReportTest method testLocalDataCenterMetadataReport.

@Test
public void testLocalDataCenterMetadataReport() {
    URL url = mockURL.addParameter("strategy", "local");
    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());
    MetadataReport localReport = null, failoverReport = null;
    for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
        if (holder.url.getBackupAddress().contains(url.getAddress())) {
            localReport = holder.report;
        } else {
            failoverReport = holder.report;
        }
    }
    Assertions.assertNotNull(localReport);
    Assertions.assertNotNull(failoverReport);
    MetadataIdentifier identifier = new MetadataIdentifier("helloService", null, null, null, "test");
    ServiceDefinition definition = new ServiceDefinition();
    definition.setCanonicalName("helloService");
    report.storeProviderMetadata(identifier, definition);
    // assert local metadata report write already.
    Assertions.assertNotNull(report.getServiceDefinition(identifier));
    Assertions.assertNotNull(localReport.getServiceDefinition(identifier));
    Assertions.assertNull(failoverReport.getServiceDefinition(identifier));
    HashMap parameterMap = new HashMap();
    report.storeConsumerMetadata(identifier, parameterMap);
    // assert local metadata report write already.
    Assertions.assertEquals(parameterMap, ((MockMetadataReport) localReport).consumerMetadata.get(identifier));
    Assertions.assertNotEquals(parameterMap, ((MockMetadataReport) failoverReport).consumerMetadata.get(identifier));
    SubscriberMetadataIdentifier subscribeIdentifier = new SubscriberMetadataIdentifier("test", "1.0");
    MetadataInfo metadataInfo = new MetadataInfo(subscribeIdentifier.getApplication(), subscribeIdentifier.getRevision(), null);
    report.publishAppMetadata(subscribeIdentifier, metadataInfo);
    // assert all metadata report write already.
    Assertions.assertEquals(metadataInfo, report.getAppMetadata(subscribeIdentifier, null));
    Assertions.assertEquals(metadataInfo, localReport.getAppMetadata(subscribeIdentifier, null));
    Assertions.assertNotEquals(metadataInfo, failoverReport.getAppMetadata(subscribeIdentifier, null));
    report.registerServiceAppMapping("helloService", "test", null);
    Set<String> appNames = report.getServiceAppMapping("helloService", null, null);
    // assert local metadata report write already.
    Assertions.assertEquals(appNames, report.getServiceAppMapping("helloService", null, null));
    Assertions.assertEquals(appNames, localReport.getServiceAppMapping("helloService", null, null));
    Assertions.assertNotEquals(appNames, failoverReport.getServiceAppMapping("helloService", null, null));
    ServiceMetadataIdentifier serviceIdentifier = new ServiceMetadataIdentifier("helloService", null, null, null, "1.0", "dubbo");
    report.saveServiceMetadata(serviceIdentifier, url);
    // assert local metadata report write already.
    Assertions.assertNotNull(report.getExportedURLs(serviceIdentifier));
    Assertions.assertNotNull(localReport.getExportedURLs(serviceIdentifier));
    Assertions.assertNull(failoverReport.getExportedURLs(serviceIdentifier));
    Set<String> urls = new HashSet<>();
    urls.add(url.toFullString());
    report.saveSubscribedData(subscribeIdentifier, urls);
    // assert local metadata report write already.
    Assertions.assertEquals(new ArrayList<>(urls), report.getSubscribedURLs(subscribeIdentifier));
    Assertions.assertEquals(new ArrayList<>(urls), localReport.getSubscribedURLs(subscribeIdentifier));
    Assertions.assertNotEquals(new ArrayList<>(urls), failoverReport.getSubscribedURLs(subscribeIdentifier));
}
Also used : MetadataReport(org.apache.dubbo.metadata.report.MetadataReport) 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) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class PublishingServiceDefinitionListenerTest method testOnServiceConfigExportedEvent.

/**
 * Test {@link ServiceConfigExportedEvent} arising
 */
@Test
public void testOnServiceConfigExportedEvent() {
    ServiceConfig<EchoService> serviceConfig = new ServiceConfig<>();
    serviceConfig.setInterface(EchoService.class);
    serviceConfig.setRef(new EchoServiceImpl());
    serviceConfig.setRegistry(new RegistryConfig("N/A"));
    serviceConfig.setProtocol(new ProtocolConfig("dubbo", NetUtils.getAvailablePort(20880 + new Random().nextInt(10000))));
    serviceConfig.export();
    String serviceDefinition = writableMetadataService.getServiceDefinition(EchoService.class.getName());
    ServiceDefinition serviceDefinitionBuild = ServiceDefinitionBuilder.build(serviceConfig.getInterfaceClass());
    assertEquals(serviceDefinition, JSON.toJSONString(serviceDefinitionBuild));
    serviceConfig.unexport();
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) Random(java.util.Random) ServiceConfig(org.apache.dubbo.config.ServiceConfig) EchoService(org.apache.dubbo.config.bootstrap.EchoService) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) EchoServiceImpl(org.apache.dubbo.config.bootstrap.EchoServiceImpl) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with ServiceDefinition

use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.

the class ServiceDefinitionBuilder method build.

/**
 * Describe a Java interface in {@link ServiceDefinition}.
 *
 * @return Service description
 */
public static ServiceDefinition build(final Class<?> interfaceClass) {
    ServiceDefinition sd = new ServiceDefinition();
    build(sd, interfaceClass);
    return sd;
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition)

Aggregations

ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)14 Test (org.junit.jupiter.api.Test)8 Gson (com.google.gson.Gson)5 TypeDefinition (org.apache.dubbo.metadata.definition.model.TypeDefinition)5 TestService (org.apache.dubbo.metadata.definition.common.TestService)4 HashMap (java.util.HashMap)2 URL (org.apache.dubbo.common.URL)2 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)2 MapTypeBuilder (org.apache.dubbo.metadata.definition.builder.MapTypeBuilder)2 FullServiceDefinition (org.apache.dubbo.metadata.definition.model.FullServiceDefinition)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)2 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)2 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 TypeElement (javax.lang.model.element.TypeElement)1 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)1