In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze the integration of Dubbo and SpringBoot from the source code and how to start Dubbo? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In the analysis of Reference, it is mentioned that Dubbo will automatically quote configuration classes. You should pay attention to DubboBootstrapApplicationListener here:
RegisterCommonBeans (BeanDefinitionRegistry registry) {registerInfrastructureBean (registry, ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class); registerInfrastructureBean (registry, DubboConfigAliasPostProcessor.BEAN_NAME, DubboConfigAliasPostProcessor.class); registerInfrastructureBean (registry, DubboLifecycleComponentApplicationListener.BEAN_NAME, DubboLifecycleComponentApplicationListener.class); registerInfrastructureBean (registry, DubboBootstrapApplicationListener.BEAN_NAME, DubboBootstrapApplicationListener.class) RegisterInfrastructureBean (registry, DubboConfigDefaultPropertyValueBeanPostProcessor.BEAN_NAME, DubboConfigDefaultPropertyValueBeanPostProcessor.class);}
Public class DubboBootstrapApplicationListener extends OneTimeExecutionApplicationContextEventListener implements Ordered {private final DubboBootstrap dubboBootstrap; public DubboBootstrapApplicationListener () {/ / here is a singleton this.dubboBootstrap = DubboBootstrap.getInstance ();} / / listening event @ Override public void onApplicationContextEvent (ApplicationContextEvent event) {if (event instanceof ContextRefreshedEvent) {onContextRefreshedEvent ((ContextRefreshedEvent) event) / / context refresh completion event} else if (event instanceof ContextClosedEvent) {onContextClosedEvent ((ContextClosedEvent) event); / / close event}} private void onContextRefreshedEvent (ContextRefreshedEvent event) {dubboBootstrap.start ();} private void onContextClosedEvent (ContextClosedEvent event) {dubboBootstrap.stop ();}}
DubboBootstrapApplicationListener implements event listening for Spring. When the context refresh is complete, dubboBootstrap.start () is executed:
Public DubboBootstrap start () {/ / CAS if (started.compareAndSet (false, true)) {ready.set (false); / / initialize initialize (); / / Dubbo service export exportServices (); if (! isOnlyRegisterProvider () | | hasExportedServices ()) {exportMetadataService () RegisterServiceInstance ();} / / Service reference referServices ();} return this;}
First, take a look at the export of exportServices () service:
Private void exportServices () {configManager.getServices () .forEach (sc-> {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);}});}
The data in the loop configManager.getServices () is exported in turn, so where does the Service come from? :
Public Collection getServices () {
Return getConfigs (getTagName (ServiceConfigBase.class))
}
Finally, the actual call:
Protected Map getConfigsMap (String configType) {
Return (Map) read (()-> configsCache.getOrDefault (configType, emptyMap ()
}
GetTagName (ServiceConfigBase.class) gets the label of the class, in this case service. We have previously analyzed Dubbo's DubboService annotations to register ServiceBean as well as the original class. Let's take a look at the class structure diagram of ServiceBean, which is a subclass of ServiceConfigBase, so how did it get into configsCache?
Let's take a look at AbstractConfig, the parent class of ServiceConfigBase, which has a method for PostConstruct annotations. Check the official explanation: PostConstruct annotations are used on methods that require dependency injection to be completed to perform any initialization. This method must be called before the class is put into use. All classes that support dependency injection must support this annotation. Even if the class does not require any resource injection, a method annotated with PostConstruct must be called. This means that all ServiceBean will call the addIntoConfigManager method after they are initialized by Spring.
@ PostConstruct public void addIntoConfigManager () {ApplicationModel.getConfigManager () .addConfig (this);}
The actual effect of the addIntoConfigManager method is to cache the configsCache:
Protected void addConfig (AbstractConfig config, boolean unique) {if (config = = null) {return;} write (()-> {Map configsMap = configsCache.computeIfAbsent (getTagName (config.getClass ()), type-> newMap ()); addIfAbsent (config, configsMap, unique);});}
Therefore, any class in Dubbo that implements the AbstractConfig interface will eventually be cached in configManager after it is initialized by Spring.
Summary:
1. The Bean that implements the AbstractConfig interface in Dubbo will be put into configManager after it is initialized by Spring.
2. By implementing the listening event of Spring, Dubbo initializes and starts the Dubbo service when the context refresh is complete.
3. Starting the boot class dubboBootstrap is a singleton.
At this point, the startup analysis of the Dubbo container is complete.
This is the answer to the question about how to integrate Dubbo and SpringBoot from the source code analysis and how to start Dubbo. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.