内容简介:本文主要研究一下dubbo的MetadataReportServicedubbo-2.7.2/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.javadubbo-2.7.2/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apach
序
本文主要研究一下dubbo的MetadataReportService
MetadataReportService
dubbo-2.7.2/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/integration/MetadataReportService.java
public class MetadataReportService { protected final Logger logger = LoggerFactory.getLogger(getClass()); private static volatile MetadataReportService metadataReportService; private static Object lock = new Object(); private MetadataReportFactory metadataReportFactory = ExtensionLoader.getExtensionLoader(MetadataReportFactory.class).getAdaptiveExtension(); MetadataReport metadataReport; URL metadataReportUrl; MetadataReportService(URL metadataReportURL) { if (METADATA_REPORT_KEY.equals(metadataReportURL.getProtocol())) { String protocol = metadataReportURL.getParameter(METADATA_REPORT_KEY, DEFAULT_DIRECTORY); metadataReportURL = URLBuilder.from(metadataReportURL) .setProtocol(protocol) .removeParameter(METADATA_REPORT_KEY) .build(); } this.metadataReportUrl = metadataReportURL; metadataReport = metadataReportFactory.getMetadataReport(this.metadataReportUrl); } public static MetadataReportService instance(Supplier<URL> metadataReportUrl) { if (metadataReportService == null) { synchronized (lock) { if (metadataReportService == null) { URL metadataReportURLTmp = metadataReportUrl.get(); if (metadataReportURLTmp == null) { return null; } metadataReportService = new MetadataReportService(metadataReportURLTmp); } } } return metadataReportService; } public void publishProvider(URL providerUrl) throws RpcException { //first add into the list // remove the individul param providerUrl = providerUrl.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, Constants.BIND_PORT_KEY, TIMESTAMP_KEY); try { String interfaceName = providerUrl.getParameter(INTERFACE_KEY); if (StringUtils.isNotEmpty(interfaceName)) { Class interfaceClass = Class.forName(interfaceName); FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(interfaceClass, providerUrl.getParameters()); metadataReport.storeProviderMetadata(new MetadataIdentifier(providerUrl.getServiceInterface(), providerUrl.getParameter(VERSION_KEY), providerUrl.getParameter(GROUP_KEY), PROVIDER_SIDE, providerUrl.getParameter(APPLICATION_KEY)), fullServiceDefinition); return; } logger.error("publishProvider interfaceName is empty . providerUrl: " + providerUrl.toFullString()); } catch (ClassNotFoundException e) { //ignore error logger.error("publishProvider getServiceDescriptor error. providerUrl: " + providerUrl.toFullString(), e); } } public void publishConsumer(URL consumerURL) throws RpcException { consumerURL = consumerURL.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY, Constants.BIND_PORT_KEY, TIMESTAMP_KEY); metadataReport.storeConsumerMetadata(new MetadataIdentifier(consumerURL.getServiceInterface(), consumerURL.getParameter(VERSION_KEY), consumerURL.getParameter(GROUP_KEY), CONSUMER_SIDE, consumerURL.getParameter(APPLICATION_KEY)), consumerURL.getParameters()); } }
- MetadataReportService的构造器通过metadataReportURL来获取对应的MetadataReport;它定义了publishProvider方法主要是执行metadataReport.storeProviderMetadata方法;还定义了publishConsumer方法主要是执行metadataReport.storeConsumerMetadata方法;同时还提供了一个静态方法使用双重检锁的单例模式创建MetadataReportService
实例
dubbo-2.7.2/dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java
public class MetadataReportServiceTest { URL url = URL.valueOf("JTest://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vic"); MetadataReportService metadataReportService1; @BeforeEach public void before() { metadataReportService1 = MetadataReportService.instance(() -> url); } @Test public void testInstance() { MetadataReportService metadataReportService2 = MetadataReportService.instance(new Supplier<URL>() { @Override public URL get() { return url; } }); Assertions.assertSame(metadataReportService1, metadataReportService2); Assertions.assertEquals(metadataReportService1.metadataReportUrl, url); } @Test public void testPublishProviderNoInterfaceName() { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpubprovder&side=provider"); metadataReportService1.publishProvider(publishUrl); Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; Assertions.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); } @Test public void testPublishProviderWrongInterface() { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.0&application=vicpu&interface=ccc&side=provider"); metadataReportService1.publishProvider(publishUrl); Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; Assertions.assertTrue(!jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); } @Test public void testPublishProviderContainInterface() throws InterruptedException { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.3&application=vicpubp&interface=org.apache.dubbo.metadata.integration.InterfaceNameTestService&side=provider"); metadataReportService1.publishProvider(publishUrl); Thread.sleep(300); Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; Assertions.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getProviderKey(publishUrl))); String value = jTestMetadataReport4Test.store.get(JTestMetadataReport4Test.getProviderKey(publishUrl)); FullServiceDefinition fullServiceDefinition = toServiceDefinition(value); Map<String,String> map = fullServiceDefinition.getParameters(); Assertions.assertEquals(map.get("application"), "vicpubp"); Assertions.assertEquals(map.get("version"), "1.0.3"); Assertions.assertEquals(map.get("interface"), "org.apache.dubbo.metadata.integration.InterfaceNameTestService"); } @Test public void testPublishConsumer() throws InterruptedException { URL publishUrl = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":4444/org.apache.dubbo.TestService?version=1.0.x&application=vicpubconsumer&side=consumer"); metadataReportService1.publishConsumer(publishUrl); Thread.sleep(300); Assertions.assertTrue(metadataReportService1.metadataReport instanceof JTestMetadataReport4Test); JTestMetadataReport4Test jTestMetadataReport4Test = (JTestMetadataReport4Test) metadataReportService1.metadataReport; Assertions.assertTrue(jTestMetadataReport4Test.store.containsKey(JTestMetadataReport4Test.getConsumerKey(publishUrl))); String value = jTestMetadataReport4Test.store.get(JTestMetadataReport4Test.getConsumerKey(publishUrl)); Gson gson = new Gson(); Map<String, String> map = gson.fromJson(value, Map.class); Assertions.assertEquals(map.get("application"), "vicpubconsumer"); Assertions.assertEquals(map.get("version"), "1.0.x"); } private FullServiceDefinition toServiceDefinition(String urlQuery) { Gson gson = new Gson(); return gson.fromJson(urlQuery, FullServiceDefinition.class); } }
- 这里的testInstance方法验证了单例模式;之后对publishProvider验证了noInterfaceName、wrongInterface、containInterface的场景;最后验证了publishConsumer
小结
MetadataReportService的构造器通过metadataReportURL来获取对应的MetadataReport;它定义了publishProvider方法主要是执行metadataReport.storeProviderMetadata方法;还定义了publishConsumer方法主要是执行metadataReport.storeConsumerMetadata方法;同时还提供了一个静态方法使用双重检锁的单例模式创建MetadataReportService
doc
以上所述就是小编给大家介绍的《聊聊dubbo的MetadataReportService》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Modern PHP(中文版)
Josh Lockhart / 安道 / 中国电力出版社 / 2015-9 / 39
PHP正在重生,不过所有PHP在线教程都过时了,很难体现这一点。通过这本实用的指南,你会发现,借助面向对象、命名空间和不断增多的可重用的组件库,PHP已经成为一门功能完善的成熟语言。 本书作者Josh Lockhart是“PHP之道”的发起人,这是个受欢迎的新方案,鼓励开发者使用PHP最佳实践。Josh通过实践揭示了PHP语言的这些新特性。你会学到关于应用架构、规划、数据库、安全、测试、调试......一起来看看 《Modern PHP(中文版)》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
HSV CMYK 转换工具
HSV CMYK互换工具