淘寶動態配置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);
    }

到此,就可以取出你想要的數據了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章