1. 程式人生 > >淘寶動態配置diamond-client 原始碼講解

淘寶動態配置diamond-client 原始碼講解

上一篇文章應該可以通過main方法,獲得存在diamond裡的值了。

那麼從現在開始講解一下diamond-client的原始碼。

1.DefaultDiamondManager

    com.taobao.diamond.manager.impl.DefaultDiamondManager

public DefaultDiamondManager(String group, String dataId, ManagerListener managerListener) {
        this.dataId = dataId;
        this.group = group;

        diamondSubscriber = DiamondClientFactory.getSingletonDiamondSubscriber();

        this.managerListeners.add(managerListener);
        ((DefaultSubscriberListener) diamondSubscriber.getSubscriberListener()).addManagerListeners(this.dataId,
            this.group, this.managerListeners);
        diamondSubscriber.addDataId(this.dataId, this.group);
        diamondSubscriber.start();

    }

2.開始初始化的工作

    com.taobao.diamond.client.impl.DefaultDiamondSubscriber

/**
     * 啟動DiamondSubscriber:<br>
     * 1.阻塞主動獲取所有的DataId配置資訊<br>
     * 2.啟動定時執行緒定時獲取所有的DataId配置資訊<br>
     */
    public synchronized void start() {
        if (isRun) {
            return;
        }

        if (null == scheduledExecutor || scheduledExecutor.isTerminated()) {
            scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        }

        localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);
        serverAddressProcessor = new ServerAddressProcessor(this.diamondConfigure, this.scheduledExecutor);
        serverAddressProcessor.start();

        this.snapshotConfigInfoProcessor =
                new SnapshotConfigInfoProcessor(this.diamondConfigure.getFilePath() + "/" + SNAPSHOT_DIR);
        // 設定domainNamePos值
        randomDomainNamePos();
        initHttpClient();

        // 初始化完畢
        isRun = true;

        if (log.isInfoEnabled()) {
            log.info("當前使用的域名有:" + this.diamondConfigure.getDomainNameList());
        }

        if (MockServer.isTestMode()) {
            bFirstCheck = false;
        }
        else {
            // 設定輪詢間隔時間
            this.diamondConfigure.setPollingIntervalTime(Constants.POLLING_INTERVAL_TIME);
        }
        // 輪詢
        rotateCheckConfigInfo();

        addShutdownHook();
    }

      這個方法裡主要做了2件事情

      2.1.獲取伺服器列表

 public synchronized void start() {
        if (isRun) {
            return;
        }
        isRun = true;
        initHttpClient();
        if (this.diamondConfigure.isLocalFirst()) {
            acquireServerAddressFromLocal();
        }
        else {
            synAcquireServerAddress();
            asynAcquireServerAddress();
        }

    }

      2.2.監測dataId是否變化

/**
     * 迴圈探測配置資訊是否變化,如果變化,則再次向DiamondServer請求獲取對應的配置資訊
     */
    private void rotateCheckConfigInfo() {
        scheduledExecutor.schedule(new Runnable() {
            public void run() {
                if (!isRun) {
                    log.warn("DiamondSubscriber不在執行狀態中,退出查詢迴圈");
                    return;
                }
                try {
                    checkLocalConfigInfo();
                    checkDiamondServerConfigInfo();
                    checkSnapshot();
                }
                catch (Exception e) {
                    e.printStackTrace();
                    log.error("迴圈探測發生異常", e);
                }
                finally {
                    rotateCheckConfigInfo();
                }
            }

        }, bFirstCheck ? 60 : diamondConfigure.getPollingIntervalTime(), TimeUnit.SECONDS);
        bFirstCheck = false;
    }

3.獲取diamond儲存的值

public String getAvailableConfigureInfomation(String dataId, String group, long timeout) {
        // 嘗試先從本地和網路獲取配置資訊
        try {
            String result = getConfigureInfomation(dataId, group, timeout);
            if (result != null && result.length() > 0) {
                return result;
            }
        }
        catch (Throwable t) {
            log.error(t.getMessage(), t);
        }

        // 測試模式不使用本地dump
        if (MockServer.isTestMode()) {
            return null;
        }
        return getSnapshotConfiginfomation(dataId, group);
    }

到此,就可以取出你想要的資料了。