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