dubbo源码2.7.9分析-api启动(一)

下载源码

git clone https://github.com/apache/dubbo.git dubbo

在GitHub下载源码,我下载的版本是2.7.9-SNAPSHOT

provider

先看服务提供者,找到dubbo-demo-api-provider,直接看Application类

package org.apache.dubbo.demo.provider;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.demo.DemoService;

import java.util.concurrent.CountDownLatch;

public class Application {
    public static void main(String[] args) throws Exception {
        if (isClassic(args)) {
            startWithExport();
        } else {
            startWithBootstrap();
        }
    }

    private static boolean isClassic(String[] args) {
        return args.length > 0 && "classic".equalsIgnoreCase(args[0]);
    }

    private static void startWithBootstrap() {
        ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());

        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider"))
                .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
                .service(service)
                .start()
                .await();
    }

    private static void startWithExport() throws InterruptedException {
        ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
        service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        service.export();

        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }
}

demo有两种启动方式,如果启动参数传了classic,用经典的暴露服务启动,否则用Bootstrap方式启动。这里可以看得出,原来的启动方式,服务配置(serviceConfig)和服务的暴露(service.export())耦合在一起了。

我们直接看新的DubboBootstrap启动方式,以前xml配置的内容都可以用api的方式在DubboBootstrap。先看看获取单例的方法

DubboBootstrap.getInstance()

在这个方法里面,还是先初始化了bootstrap,进入getInstance(),一个经典的双重检查获取单例方法

    public static DubboBootstrap getInstance() {
        if (instance == null) {
            synchronized (DubboBootstrap.class) {
                if (instance == null) {
                    instance = new DubboBootstrap();
                }
            }
        }
        return instance;
    }

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

构造函数做了两件事:

  • 初始化配置管理类
  • 初始化配置中心

简单来说,configManager包含了应用,提供者,消费者,注册中心,协议等等这些基本配置;environment包含环境变量,启动参数,动态配置中心的变量配置。

这里加载默认配置用到了扩展的spi机制,暂时不展开讲了。

往下:bootstrap.start(),代码不贴了,里面第一个方法是初始化

initialize()

    public void initialize() {
        if (!initialized.compareAndSet(false, true)) {
            return;
        }
        
        ApplicationModel.initFrameworkExts();

        startConfigCenter();

        loadRemoteConfigs();

        checkGlobalConfigs();

        // @since 2.7.8
        startMetadataCenter();

        initMetadataService();

        initEventListener();

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

调用框架的initialize方法,spi扩展的文件里面只有三个类,只有Environment实现了该方法

config=org.apache.dubbo.config.context.ConfigManager
environment=org.apache.dubbo.common.config.Environment
repository=org.apache.dubbo.rpc.model.ServiceRepository

初始化方法只是设置了两个成员变量

public class Environment extends LifecycleAdapter implements FrameworkExt {
    public static final String NAME = "environment";

    private final PropertiesConfiguration propertiesConfiguration;
    private final SystemConfiguration systemConfiguration;
    private final EnvironmentConfiguration environmentConfiguration;
    private final InmemoryConfiguration externalConfiguration;
    private final InmemoryConfiguration appExternalConfiguration;

    ···省略其他代码

    @Override
    public void initialize() throws IllegalStateException {
        ConfigManager configManager = ApplicationModel.getConfigManager();
        Optional<Collection<ConfigCenterConfig>> defaultConfigs = configManager.getDefaultConfigCenter();
        defaultConfigs.ifPresent(configs -> {
            for (ConfigCenterConfig config : configs) {
                this.setExternalConfigMap(config.getExternalConfiguration());
                this.setAppExternalConfigMap(config.getAppExternalConfiguration());
            }
        });

        this.externalConfiguration.setProperties(externalConfigurationMap);
        this.appExternalConfiguration.setProperties(appExternalConfigurationMap);
    }
    
}

这两个配置在初始化配置中心的时候设置

        ConfigCenterConfig configCenter = new ConfigCenterConfig();
        configCenter.setAddress("zookeeper://127.0.0.1:2181");
        configCenter.setExternalConfig(Collections.singletonMap("test.config", "test"));
        bootstrap.configCenter(configCenter);
  • propertiesConfiguration,从本地的dubbo.properties获取,可以有多个配置文件和优先级
  • systemConfiguration,简单,就是System.getProperty(key)
  • environmentConfiguration,也简单,System.getenv(key)
  • dynamicConfiguration,动态的配置中心
startConfigCenter

把Environment里面的一些配置注入的注册中心去,比如我们在启动参数指定注册中心配置,就是在这里生效了

loadRemoteConfigs

和上面的作用差不多

checkGlobalConfigs

校验配置格式,就是校验value的长度,格式

startMetadataCenter

开启元数据中心。

在这之前,一个接口有很多字段信息存在注册中心里面,实际用到的不多,增加注册中心的数据压力,新版本把一些不常用的字段放到元数据中心,可以本地存储或者远程存储。

官方元数据中心推荐用redis,我们也可以用zookeeper。

bootstrap.reportConfig(new MetadataReportConfig("zookeeper://localhost:2181"));

dubbo用ExtensionLoad加载zookeeper对应的实现类,需要在pom里面引用对应的包

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-metadata-report-zookeeper</artifactId>
</dependency>
initMetadataService
    private void initMetadataService() {
        startMetadataCenter();
        this.metadataService = getDefaultExtension();
        this.metadataServiceExporter = new ConfigurableMetadataServiceExporter(metadataService);
    }
  • 又调用了一下startMetadataCenter,不知道什么原因
  • new metadataService,默认是InMemoryWritableMetadataService,在本地内存存储元数据,然后export
  • new metadataServiceExporter = ConfigurableMetadataServiceExporter
initEventListener

把bootstrap加入事件监听队列里面。

initialize 方法结束。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容