RPC -dubbo 服务导出实现(-)

在阅读此文章之前,我希望阅读者对Spring 扩展机制的有一定的了解,比如:自定义标签与Spring整合, InitializingBean 接口,ApplicationContextAware,BeanNameAware,

BeanFactory 接口所起到的作用 ;从来没了解过的,请看我之前的关于Spring的博客 

开始正题

(一)onApplicationEvent (事件监听)

dubbo 服务导出的方法是在 com.alibaba.dubbo.config.spring.ServiceBean 类中,dubbo 服务的导出过程始于在Spring 容器发生刷新事件,那么如何感知到Spring 容器发生刷新

事件呢? ~~ 得益于Spring提供的 ApplicationListener 接口,看如下代码实现:

public void onApplicationEvent(ApplicationEvent event) {
    //在Spring bean 刷新后进行服务的导出;
        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();
            }
        }
    }

实现该接口需要实现 onApplicationEvent 方法;事件的监听,在有event 事件发生后,Spring会自动进行触发此方法;

在Spring 容器发生刷新事件后进行导出 export();   这一步就是最先开始的地方;

(二)检查参数,组装 URL

public synchronized void export() {
        if (provider != null) {
        //是否进行导出的操作,用于在我们配置了<dubbo:provider export="false" />的时候,本地进行操作,不进行服务暴露的时候
            if (export == null) {
                export = provider.getExport();
            }
            if (delay == null) {
                delay = provider.getDelay();
            }
        }
        if (export != null && ! export.booleanValue()) {
            return;
        }
        //延时导出,线程睡眠,在高版本中,此方法改变成了schedule
        if (delay != null && delay > 0) {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(delay);
                    } catch (Throwable e) {
                    }
                    doExport();
                }
            });
            thread.setDaemon(true);
            thread.setName("DelayExportServiceThread");
            thread.start();
        } else {
            doExport();
        }
    }        

以上的方法就是进行了<dubbo:provider> 标签属性 export 与delay 的操作判断;

我们继续进行  doExport();

待续。。。

关于此篇文章受益于 :http://dubbo.apache.org/zh-cn/docs/source_code_guide/export-service.html   dubbo官网文档

猜你喜欢

转载自www.cnblogs.com/iscys/p/10177089.html