Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the reference object of Invoker in the Dubbo interceptor

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to get the reference object of Invoker in the Dubbo interceptor". In the daily operation, I believe that many people have doubts about how to get the reference object of Invoker in the Dubbo interceptor. The editor consulted all kinds of materials and sorted out a simple and useful method of operation. I hope it will be helpful to answer the doubt of "how to get the reference object of Invoker in the Dubbo interceptor"! Next, please follow the editor to study!

This paper is based on Dubbo 2.7.8.

When we customize the Dubbo interceptor, sometimes we want to get the proxy object (the proxy object here refers to the actual object in the Spring container, which may have been proxied by Cglib or Jdk), here provides a simple and feasible solution. Version 2.7.5 and above are currently available

Let's first take a look at the call chain exported by the Dubbo service:

/ / Export all services private void exportServices () {configManager.getServices () .forEach (sc-> {/ / TODO, compatible with ServiceConfig.export () ServiceConfig serviceConfig = (ServiceConfig) sc; serviceConfig.setBootstrap (this); if (exportAsync) {ExecutorService executor = executorRepository.getServiceExporterExecutor () Future future = executor.submit (()-> {sc.export (); exportedServices.add (sc);}); asyncExportingFutures.add (future);} else {sc.export (); exportedServices.add (sc);}}) } / / whether you need to export if (! shouldExport ()) {return;} checkAndUpdateSubConfigs (); doExport (); exported ();} protected synchronized void doExport () {if (exported) {return;} exported = true If (StringUtils.isEmpty (path)) {path = interfaceName;} doExportUrls ();} private void doExportUrls () {/ / here is the key point ServiceRepository repository = ApplicationModel.getServiceRepository (); ServiceDescriptor serviceDescriptor = repository.registerService (getInterfaceClass ()) Repository.registerProvider (getUniqueServiceName (), ref, serviceDescriptor, this, serviceMetadata) } / / Registration provider public void registerProvider (String serviceKey, Object serviceInstance, ServiceDescriptor serviceModel, ServiceConfigBase serviceConfig, ServiceMetadata serviceMetadata) {ProviderModel providerModel = new ProviderModel (serviceKey, serviceInstance, serviceModel, serviceConfig, serviceMetadata) / / key is serviceKey providers.putIfAbsent (serviceKey, providerModel); providersWithoutGroup.putIfAbsent (keyWithoutGroup (serviceKey), providerModel);}

You can see that the service information is registered with ServiceRepository when the Dubbo service is exported, and we can get the serviceKey through Invocation's getTargetServiceUniqueName in Filter.

The results are obvious:

You can get the proxy object directly in the following ways:

ProviderModel providerModel = ApplicationModel.getServiceRepository (). LookupExportedService (invocation.getTargetServiceUniqueName ()); System.out.println (providerModel.getServiceInstance (). GetClass ()); at this point, the study on "how to get the reference object of Invoker in the Dubbo interceptor" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report