Dubbo源码分析

Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题。相比其他RPC框架,dubbo更加灵活,可以做到方法级别的服务治理,粒度更细。

Dubbo作用域隔离

Dubbo 3.x支持在一个进程中启动多个dubbo实例,通过ApplicationModel进行隔离。

利用父委派机制/双亲委派模型进行类的加载,比如加载ModuleConfig,ModuleModel会先委派父级ApplicationModel,ApplicationModel会委派给它的父级FrameworkModel,FrameworkModel加载不了,然后会重新交给ApplicationModel,ApplicationModel也加载不了,又再次递回给ModuleModel去加载。

父委派/双亲委派模型:某个特定的类加载器在接到加载类的请求时,首先将加载任务委派给父类加载器,依次递归到顶级父类,如果父类加载器可以完成类加载任务,就成功返回;如果父类加载器无法完成加载任务,再反向交给子类加载器去加载,依次反向递归,直到某个子类加载器完成加载任务。

Dubbo源码分析_第1张图片

Dubbo源码分析_第2张图片

dubbo通过作用域ScopeModel对不同范围的配置数据进行作用域管理:

Dubbo源码分析_第3张图片

Dubbo如何实现双亲委派

new ServiceConfig() --> new AbstractServiceConfig() --> new AbstractServiceConfig()  --> new AbstractInterfaceConfig() --> new AbstractMethodConfig() --> ApplicationModel.defaultModel().getDefaultModule()

Dubbo源码分析_第4张图片

public class FrameworkModel extends ScopeModel {

    public FrameworkModel() {
        super(null, ExtensionScope.FRAMEWORK, false);
        this.setInternalId(String.valueOf(index.getAndIncrement()));
        // register FrameworkModel instance early
        synchronized (globalLock) {
            allInstances.add(this);
            resetDefaultFrameworkModel();
        }
        initialize(); // 初始化framework范围的配置
    }

    @Override
    protected void initialize() {
        super.initialize();

        TypeDefinitionBuilder.initBuilders(this); // 定义作用域类型
        // 定义当前作用域范围内存仓库
        serviceRepository = new FrameworkServiceRepository(this);
        // 扩展点
        ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
        Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
        for (ScopeModelInitializer initializer : initializers) {
            initializer.initializeFrameworkModel(this);
        }

        internalApplicationModel = new ApplicationModel(this, true);
        internalApplicationModel.getApplicationConfigManager().setApplication(
            new ApplicationConfig(internalApplicationModel, CommonConstants.DUBBO_INTERNAL_APPLICATION));
        internalApplicationModel.setModelName(CommonConstants.DUBBO_INTERNAL_APPLICATION);
    }

    // Get or create default framework model by DCL
    public static FrameworkModel defaultModel() {
        FrameworkModel instance = defaultInstance;
        if (instance == null) {
            synchronized (globalLock) {
                resetDefaultFrameworkModel();
                if (defaultInstance == null) {
                    defaultInstance = new FrameworkModel();
                }
                instance = defaultInstance;
            }
        }
        Assert.notNull(instance, "Default FrameworkModel is null");
        return instance;
    }

    // Get or create default application model by DCL
    public ApplicationModel defaultApplication() {
        ApplicationModel appModel = this.defaultAppModel;
        if (appModel == null) {
            // check destroyed before acquire inst lock, avoid blocking during destroying
            checkDestroyed();
            resetDefaultAppModel();
            if ((appModel = this.defaultAppModel) == null) {
                synchronized (instLock) {
                    if (this.defaultAppModel == null) {
                        this.defaultAppModel = newApplication();
                    }
                    appModel = this.defaultAppModel;
                }
            }
        }
        Assert.notNull(appModel, "Default ApplicationModel is null");
        return appModel;
    }

    public ApplicationModel newApplication() {
        return new ApplicationModel(this);
    }
}
public class ApplicationModel extends ScopeModel {
    private volatile ModuleModel defaultModule;
    // internal module index is 0, default module index is 1
    private AtomicInteger moduleIndex = new AtomicInteger(0);
    private Object moduleLock = new Object();

    public ApplicationModel(FrameworkModel frameworkModel) {
        this(frameworkModel, false);
    }

    public ApplicationModel(FrameworkModel frameworkModel, boolean isInternal) {
        super(frameworkModel, ExtensionScope.APPLICATION, isInternal);
        Assert.notNull(frameworkModel, "FrameworkModel can not be null");
        this.frameworkModel = frameworkModel;
        frameworkModel.addApplication(this);
        initialize(); // 初始化application范围的配置
    }

    @Override
    protected void initialize() {
        super.initialize();
        internalModule = new ModuleModel(this, true);
        // 定义当前作用域范围内存仓库
        this.serviceRepository = new ServiceRepository(this);
        // 扩展点
        ExtensionLoader extensionLoader = this.getExtensionLoader(ApplicationInitListener.class);
        Set listenerNames = extensionLoader.getSupportedExtensions();
        for (String listenerName : listenerNames) {
            extensionLoader.getExtension(listenerName).init();
        }

        initApplicationExts();

        ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
        Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
        for (ScopeModelInitializer initializer : initializers) {
            initializer.initializeApplicationModel(this);
        }
    }

    public static ApplicationModel defaultModel() {
        // should get from default FrameworkModel, avoid out of sync
        return FrameworkModel.defaultModel().defaultApplication();
    }

    // Get or create module model by DCL
    public ModuleModel getDefaultModule() {
        if (defaultModule == null) {
            synchronized (moduleLock) {
                if (defaultModule == null) {
                    defaultModule = findDefaultModule();
                    if (defaultModule == null) {
                        defaultModule = this.newModule();
                    }
                }
            }
        }
        return defaultModule;
    }

    public ModuleModel newModule() {
        return new ModuleModel(this);
    }
}

public class ModuleModel extends ScopeModel {
    public ModuleModel(ApplicationModel applicationModel) {
        this(applicationModel, false);
    }

    public ModuleModel(ApplicationModel applicationModel, boolean isInternal) {
        super(applicationModel, ExtensionScope.MODULE, isInternal);
        Assert.notNull(applicationModel, "ApplicationModel can not be null");
        this.applicationModel = applicationModel;
        applicationModel.addModule(this, isInternal);

        initialize();
 
        // notify application check state
        ApplicationDeployer applicationDeployer = applicationModel.getDeployer();
        if (applicationDeployer != null) {
            applicationDeployer.notifyModuleChanged(this, DeployState.PENDING);
        }
    }

    @Override
    protected void initialize() {
        super.initialize();
        this.serviceRepository = new ModuleServiceRepository(this);

        initModuleExt();

        ExtensionLoader initializerExtensionLoader = this.getExtensionLoader(ScopeModelInitializer.class);
        Set initializers = initializerExtensionLoader.getSupportedExtensionInstances();
        for (ScopeModelInitializer initializer : initializers) {
            initializer.initializeModuleModel(this);
        }
    }
}

Dubbo配置

  • dubbo/config目录:记录dubbo外部化配置信息
  • dubbo/xxxxxService:记录dubbo发布的服务以及协议地址
  • dubbo/mapping:记录接口和application名称的映射关系
  • dubbo/metadata:dubbo 2.7版本之后将服务的元数据信息分离出来记录在这里
  • dubbo/services:记录application实例信息和对应的ip地址,dubbo 3.x版本支持多实例

Dubbo源码分析_第5张图片

Dubbo扩展点

加载 -> 实例化 -> 注入 -> 缓存 -> 提供访问入口

获取扩展点

ExtensionLoader balanceExtensionLoader = ApplicationContext.defaultModel().getExtensionLoader(LoadBalance.class);
System.out.println(balanceExtensionLoader.getSupportedExtensions());

LoadBalance loadBalance = ApplicationModel.defaultModel().getExtensionLoader(LoadBalance.class).getExtension("myloadbalance");
System.out.println(loadBalance);

你可能感兴趣的:(dubbo,java,分布式)