Dubbo源码阅读-服务启动

本文主要探讨了Dubbo服务的两种启动方式,重点关注使用Bootstrap启动的过程。详细分析了启动时的核心类关系,包括DubboBootstrap、ApplicationModel及其相关配置类。服务启动涉及初始化、服务暴露与元信息、服务引用及等待关闭等关键步骤。后续文章将对服务导出和引入进行深入剖析。
摘要由CSDN通过智能技术生成

1.概述

dubbo服务启动包括两种方式:dubbo可以有两种启动方式,一种是用bootstrap启动,一种直接用export启动,这里介绍bootstrap启动。

2.源码

2.1 核心类关系

Dubbo核心类关系(回头在补充成类图):
DubboBootstrap
ApplicationModel
  Environment
  ServiceRepository
  ConfigManager
    ApplicationConfig
    MonitorConfig
    ModuleConfig
    MetricsConfig
    SslConfig
    ConfigCenterConfig
    MetadataReportConfig
    ProviderConfig
    ConsumerConfig
    ProtocolConfig
    RegistryConfig
    ServiceConfigBase
    ReferenceConfigBase

2.2 源码

dubbo的bootstrap启动其中包括几个步骤:

1.初始化

2.暴露dubbo服务&暴露元信息

3.引用服务

4.等待关闭

其中服务导出/暴露、服务引入等将在其他文章中单独分析

 DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider"))
                .registry(new RegistryConfig("zookeeper://1270.0.1:2181"))
                .service(service)
                .start()
                .await();
    /**
     * 构造
     */
    private DubboBootstrap() {
        configManager = ApplicationModel.getConfigManager();
        environment = ApplicationModel.getEnvironment();

        DubboShutdownHook.getDubboShutdownHook().register();
        ShutdownHookCallbacks.INSTANCE.addCallback(new ShutdownHookCallback() {
            @Override
            public void callback() throws Throwable {
                DubboBootstrap.this.destroy();
            }
        });
    }

    /**
     * 启动
     */
    public DubboBootstrap start() {
        if (started.compareAndSet(false, true)) {
            ready.set(false);
            initialize();
            if (logger.isInfoEnabled()) {
                logger.info(NAME + " is starting...");
            }
            // 1. export Dubbo Services 暴露dubbo服务
            exportServices();

            // Not only provider register
            if (!isOnlyRegisterProvider() || hasExportedServices()) {
                // 2. export MetadataService
                exportMetadataService();
                //3. Register the local ServiceInstance if required
                registerServiceInstance();
            }

            referServices();
            if (asyncExportingFutures.size() > 0) {
                new Thread(() -> {
                    try {
                        this.awaitFinish();
                    } catch (Exception e) {
                        logger.warn(NAME + " exportAsync occurred an exception.");
                    }
                    ready.set(true);
                    if (logger.isInfoEnabled()) {
                        logger.info(NAME + " is ready.");
                    }
                }).start();
            } else {
                ready.set(true);
                if (logger.isInfoEnabled()) {
                    logger.info(NAME + " is ready.");
                }
            }
            if (logger.isInfoEnabled()) {
                logger.info(NAME + " has started.");
            }
        }
        return this;
    }

先贴下代码,回头在细化


    /**
     * Initialize
     */
    private void initialize() {
        if (!initialized.compareAndSet(false, true)) {
            return;
        }

        ApplicationModel.initFrameworkExts();

        startConfigCenter();

        useRegistryAsConfigCenterIfNecessary();

        loadRemoteConfigs();

        checkGlobalConfigs();

        initMetadataService();

        initEventListener();

        if (logger.isInfoEnabled()) {
            logger.info(NAME + " has been initialized!");
        }
    }

DubboBootstrap.exportServices();


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

 

3.总结

先简单过下代码和系统设计,回头细化后在补充

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值