Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The principle and function of MetadataReportService in dubbo

Shulou Source: shulou.com Published: 2022-06-02 07:53:01 11月02日 Update

This article introduces the relevant knowledge of "the principle and function of MetadataReportService in dubbo". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the MetadataReportService of dubbo.

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

The constructor of MetadataReportService acquires the corresponding MetadataReport; through metadataReportURL, which defines that the publishProvider method is mainly to execute the metadataReport.storeProviderMetadata method, and that the publishConsumer method is mainly to execute the metadataReport.storeConsumerMetadata method. It also provides a static method to create the MetadataReportService using the singleton mode with double locks.

Example

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

The testInstance method here validates the singleton pattern, then verifies the scenarios of noInterfaceName, wrongInterface, containInterface for publishProvider, and finally verifies publishConsumer.

Summary

The constructor of MetadataReportService acquires the corresponding MetadataReport; through metadataReportURL, which defines that the publishProvider method is mainly to execute the metadataReport.storeProviderMetadata method, and that the publishConsumer method is mainly to execute the metadataReport.storeConsumerMetadata method. It also provides a static method to create the MetadataReportService using the singleton mode with double locks.

This is the end of the introduction of "the principle and function of MetadataReportService in dubbo". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Method pattern verification function principle content at the same time more knowledge static constructor practical successful next dilemma scenario example actual summary situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Microsoft Shulou Tech Info Shulou Information