dubbo源码分析9——ServiceBean的afterPropertiesSet方法分析
ServiceBean的afterPropertiesSet方法是实现了InitializingBean,还是准备先做宏观分析,然后再做细致分析。下面先宏观分析:
public void afterPropertiesSet() throws Exception {
if (getProvider() == null) {
..............
//获取provider配置
}
if (getApplication() == null && (getProvider() == null || getProvider().getApplication() == null)) {
...............
//获取application配置
}
if (getModule() == null && (getProvider() == null || getProvider().getModule() == null)) {
...............
//获取module配置
}
if ((getRegistries() == null || getRegistries().size() == 0)
&& (getProvider() == null || getProvider().getRegistries() == null || getProvider().getRegistries().size() == 0)
&& (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().size() == 0)) {
.................
//获取注册中心的配置
}
if (getMonitor() == null
&& (getProvider() == null || getProvider().getMonitor() == null)
&& (getApplication() == null || getApplication().getMonitor() == null)) {
................
//获取monitor配置
}
if ((getProtocols() == null || getProtocols().size() == 0)
&& (getProvider() == null || getProvider().getProtocols() == null || getProvider().getProtocols().size() == 0)) {
...............
//获取protocol配置
}
if (getPath() == null || getPath().length() == 0) { //获取<dubbo:service/>的path属性,path即服务的发布路径
if (beanName != null && beanName.length() > 0
&& getInterface() != null && getInterface().length() > 0
&& beanName.startsWith(getInterface())) {
setPath(beanName); //如果没有设置path属性,则默认会以beanName作为path
}
}
if (! isDelay()) { //是否延迟暴露
export(); //进行服务暴露
}
}
通过上面的分析对整个方法在做什么有了大致的了解, 下面进行细致分析,对里面的一段段代码分别展开分析:
1 . 获取Provider配置
//当某个<dubbo:service/>没有绑定相应的<dubbo:provider/>的时候,就会触发下面的逻辑
if (getProvider() == null) {
//在spring的IOC容器中查找所有的type为ProviderConfig.class或其子类的bean,可能会有多个provider的配置
Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false);
if (providerConfigMap != null && providerConfigMap.size() > 0) {
//在spring的IOC容器中查找type为ProtocolConfig.class或其子类的bean
Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false); //存在<dubbo:provider/>但不存在<dubbo:protocol/>配置的情况,也就是说旧版本的protocol配置需要从provider中提取
if ((protocolConfigMap == null || protocolConfigMap.size() == 0)&& providerConfigMap.size() > 1) { // 兼容旧版本
List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
for (ProviderConfig config : providerConfigMap.values()) {
if (config.isDefault() != null && config.isDefault().booleanValue()) {
providerConfigs.add(config); // 当<dubbo:provider default="true"/>时,providerConfigs才会加入
}
}
//在配置provider的同时,也从默认的<dubbo:provider/>中提取protocol的配置
if (providerConfigs.size() > 0) {
setProviders(providerConfigs);
}
} else { //已存在<dubbo:protocol/>配置,则找出默认的<dubbo:provider/>配置
ProviderConfig providerConfig = null;
for (ProviderConfig config : providerConfigMap.values()) {
if (config.isDefault() == null || config.isDefault().booleanValue()) {
if (providerConfig != null) {
throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config);
}
providerConfig = config;
}
}
if (providerConfig != null) {
setProvider(providerConfig);
}
}
}
}
在这里补充一下什么是默认的<dubbo:provider/>,在dubbo配置文件中,可以有多个<dubbo:provider/>配置,如果某个<dubbo:provide/>配置的default属性为true。这个默认配置只能一个。
2.获取application配置
这个相对比较容易,只是说明几点,用户可以通过<dubbo:application/>的形式来配置application,也可以直接以bean的形式去配,这个bean对应的Class就是ApplicationConfig.class
if (getApplication() == null
&& (getProvider() == null || getProvider().getApplication() == null)) {
Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false);
if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
ApplicationConfig applicationConfig = null;
for (ApplicationConfig config : applicationConfigMap.values()) {
if (config.isDefault() == null || config.isDefault().booleanValue()) {
if (applicationConfig != null) {
throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
}
applicationConfig = config;
}
}
if (applicationConfig != null) {
setApplication(applicationConfig);
}
}
}
3. 获取Module,同上
4. 获取Registries,同上
5. 获取Monitor , 同上
6. 获取Protocols ,同上
7. 获取path(服务路径)
if (getPath() == null || getPath().length() == 0) {
if (beanName != null && beanName.length() > 0
&& getInterface() != null && getInterface().length() > 0
&& beanName.startsWith(getInterface())) {
setPath(beanName);
}
}
上面代码说明如果<dubbo:service/>没有配path属性,dubbo将会设置一个默认的path属性,默认值就是beanName,而beanName是ServiceBean实现了BeanNameAware接口,由spring的IOC容器传入进来的。通过上文对dubbo配置的解析的源码分析可知,一般情况这个path属性就是服务接口的类的全路径名。
8. 判断该服务是否延迟发布
if (! isDelay()) {
export(); //服务暴露的方法
}
<dubbo:service/>上会有一个delay的配置属性(官方的说明:)
private boolean isDelay() {
Integer delay = getDelay();
ProviderConfig provider = getProvider();
if (delay == null && provider != null) {
delay = provider.getDelay();
}
return supportedApplicationListener && (delay == null || delay.intValue() == -1);
}
上面为判断该服务是否延迟的方法的源码,说明如果<dubbo:service/>没有配delay属性或将其配置为-1都会延迟发布服务,而这个supportedApplicationListener属性在ServiceBean实现ApplicationContextAware接口的setApplicationContext方法中设置为true的。
下面再看一下setApplicationContext方法的源码,主要作用是为了兼容Spring2.0.1的版本,为了向前兼容spring的事件机制
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
SpringExtensionFactory.addApplicationContext(applicationContext);
if (applicationContext != null) {
SPRING_CONTEXT = applicationContext;
try {
Method method = applicationContext.getClass().getMethod("addApplicationListener", new Class<?>[]{ApplicationListener.class}); // 兼容Spring2.0.1
method.invoke(applicationContext, new Object[] {this});
supportedApplicationListener = true;
} catch (Throwable t) {
if (applicationContext instanceof AbstractApplicationContext) {
try {
Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", new Class<?>[]{ApplicationListener.class}); // 兼容Spring2.0.1
if (! method.isAccessible()) {
method.setAccessible(true);
}
method.invoke(applicationContext, new Object[] {this});
supportedApplicationListener = true;
} catch (Throwable t2) {
}
}
}
}
}
根据这个源码可知,通常我们的dubbo服务的配置:<dubbo:service interface="com.xxx.Yxxx" ref="userService" protocol="dubbo" retries="0" /> ,在这样的情况下 isDelay()方法返回true,所以我们的暴露将会发生在下面的方法中
public void onApplicationEvent(ApplicationEvent event) {
if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
if (isDelay() && ! isExported() && ! isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + getInterface());
}
export();
}
}
}
至此,这个初始化的方法就分析完了。下文将开始分析 处理服务暴露的方法(export()方法)。
dubbo源码分析9——ServiceBean的afterPropertiesSet方法分析的更多相关文章
- dubbo源码分析4——SPI机制_ExtensionFactory类的作用
ExtensionFactory的源码: @SPI public interface ExtensionFactory { /** * Get extension. * * @param type o ...
- Dubbo源码学习--服务发布(ServiceBean、ServiceConfig)
前面讲过Dubbo SPI拓展机制,通过ExtensionLoader实现可插拔加载拓展,本节将接着分析Dubbo的服务发布过程. 以源码中dubbo-demo模块作为切入口一步步走进Dubbo源码. ...
- Dubbo 源码分析 - 服务导出
1.服务导出过程 本篇文章,我们来研究一下 Dubbo 导出服务的过程.Dubbo 服务导出过程始于 Spring 容器发布刷新事件,Dubbo 在接收到事件后,会立即执行服务导出逻辑.整个逻辑大致可 ...
- dubbo源码分析8——服务暴露概述
从上文中可知,com.alibaba.dubbo.config.spring.ServiceBean类是负责解析<dubbo:service/>的配置的,下面是它的类图 从类图上可知它继承 ...
- dubbo源码分析3-service bean的创建与发布
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- dubbo源码分析(一)-从xml到我们认识的Java对象
项目中用的dubbo的挺多的,然后随着自己对dubbo的慢慢深入,自己也希望能够了解dubbo的底层实现,这半年来一直在看dubbo的源码,有点断断续续的,于是准备写一个dubbo源码系列的分析文章, ...
- dubbo源码分析6-telnet方式的管理实现
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- dubbo源码分析1-reference bean创建
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- dubbo源码分析2-reference bean发起服务方法调用
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
随机推荐
- 16.Linux-LCD驱动(详解)
在上一节LCD层次分析中,得出写个LCD驱动入口函数,需要以下4步: 1) 分配一个fb_info结构体: framebuffer_alloc(); 2) 设置fb_info 3) 设置硬件相关的操作 ...
- VirtualBox使用入门
VirtualBox使用入门 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能玩虚拟机的人多少是懂点运维的,因此我就不跟大家介绍啥事虚拟化了.关于虚拟化产品大家用的应该也都大同小异 ...
- 运维监控-Open-Falcon介绍
运维监控-Open-Falcon介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Open-Falcon 介绍 监控系统是整个运维环节,乃至整个产品生命周期中最重要的一环,事 ...
- Linux防火墙开放端口
# vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT -A INPUT - ...
- CM记录-部署cdh5.3.3集群
1.安装操作系统,保证联网环境,本文以CentOS 6.8为操作系统(略) 2.wget下载安装包(以5.3.3为例) #mkdir /usr/cdh ---新建cm安装目录 #cd /usr/cdh ...
- 四.HashSet原理及实现学习总结
在上一篇博文(HashMap原理及实现学习总结)详细总结了HashMap的实现过程,对于HashSet而言,它是基于HashMap来实现的,底层采用HashMap来保存元素.所以如果对HashMap比 ...
- Keil5创建GPIO
软件仿真如下图 Main.c内容 #include "stm32f10x.h" int main(void) { GPIO_InitTypeDef GPIO_InitStructu ...
- [译]Debug ASP.NET Core 2.0源代码
原文 首先你的VS必须为VS 2017 15.3或以上版本. 打开你的Startup类,在ConfigureServices方法上设置个断点,按F5 Debug应用. 在Call Stack(调用堆栈 ...
- 利用PHP连接数据库——实现用户数据的增删改查的整体操作实例
main页面(主页面) <table width="100%" border="1" cellpadding="0" cellspac ...
- tensorflow的特征工程函数
1. # creates a real valued column for dense numeric data tf.contrib.layers.real_valued_column( co ...