天天看點

ZooKeeper 筆記(3) 實戰應用之【統一配置管理】

ZooKeeper 筆記(3) 實戰應用之【統一配置管理】

大型應用通常會按業務拆分成一個個業務子系統,這些大大小小的子應用,往往會使用一些公用的資源,比如:需要檔案上傳、下載下傳時,各子應用都會通路公用的Ftp伺服器。如果把Ftp Server的連接配接IP、端口号、使用者名、密碼等資訊,配置在各子應用中,然後這些子應用再部署到伺服器叢集中的N台Server上,突然有一天,Ftp伺服器要換IP或端口号,那麼問題來了?不要緊張,不是問 挖掘機哪家強:),而是如何快速的把這一堆已經線上上運作的子應用,通通換掉相應的配置,而且還不能停機。

要解決這個問題,首先要從思路上做些改變:

1、公用配置不應該分散存放到各應用中,而是應該抽出來,統一存儲到一個公用的位置(最容易想到的辦法,放在db中,或統一的分布式cache server中,比如Redis,或其它類似的統一存儲,比如ZooKeeper中)

2、對這些公用配置的添加、修改,應該有一個統一的配置管理中心應用來處理(這個也好辦,做一個web應用來對這些配置做增、删、改、查即可)

3、當公用配置變化時,子應用不需要重新部署(或重新啟動),就能使用新的配置參數(比較容易想到的辦法有二個:一是釋出/訂閱模式,子應用主動訂閱公用配置的變化情況,二是子應用每次需要取配置時,都實時去取最新配置)

由于配置資訊通常不大,比較适合存放在ZooKeeper的Node中。主要處理邏輯的序列圖如下:

ZooKeeper 筆記(3) 實戰應用之【統一配置管理】

解釋一下:

考慮到所有存儲系統中,資料庫還是比較成熟可靠的,是以這些配置資訊,最終在db中存儲一份。

剛開始時,配置管理中心從db中加載公用配置資訊,然後同步寫入ZK中,然後各子應用從ZK中讀取配置,并監聽配置的變化(這在ZK中通過Watcher很容易實作)。

如果配置要修改,同樣也先在配置管理中心中修改,然後持久化到DB,接下來同步更新到ZK,由于各子應用會監聽資料變化,是以ZK中的配置變化,會實時傳遞到子應用中,子應用當然也無需重新開機。

示例代碼:

ZooKeeper 筆記(3) 實戰應用之【統一配置管理】

這裡設計了幾個類,以模拟文中開頭的場景:

FtpConfig對應FTP Server的公用配置資訊,

ConfigManager對應【統一配置中心應用】,裡面提供了幾個示例方法,包括:從db加載配置,修改db中的配置,将配置同步到ZK

ClientApp對應子系統,同樣也提供了幾個示例方法,包括擷取ZK的配置,檔案上傳,檔案下載下傳,業務方法執行

ConfigTest是單元測試檔案,用于內建測試剛才這些類

為了友善,還有一個ZKUtil的小工具類

package yjmyzz.test;

import org.I0Itec.zkclient.ZkClient;

public class ZKUtil {

    public static final String FTP_CONFIG_NODE_NAME = "/config/ftp";

    public static ZkClient getZkClient() {
        return new ZkClient("localhost:2181,localhost:2182,localhost:2183");
    }


}
      

FtpConfig代碼如下:

package yjmyzz.test;

import java.io.Serializable;

/**
 * Created by jimmy on 15/6/27.
 */
public class FtpConfig implements Serializable {

    /**
     * 端口号
     */
    private int port;

    /**
     * ftp主機名或IP
     */
    private String host;

    /**
     * 連接配接使用者名
     */
    private String user;

    /**
     * 連接配接密碼
     */
    private String password;

    public FtpConfig() {

    }

    public FtpConfig(int port, String host, String user, String password) {
        this.port = port;
        this.host = host;
        this.user = user;
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String toString() {
        return user + "/" + password + "@" + host + ":" + port;
    }
}
      

ConfigManager代碼如下:

package yjmyzz.test;


import com.fasterxml.jackson.core.JsonProcessingException;
import org.I0Itec.zkclient.ZkClient;


public class ConfigManager {

    private FtpConfig ftpConfig;


    /**
     * 模拟從db加載初始配置
     */
    public void loadConfigFromDB() {
        //query config from database
        //TODO...
        ftpConfig = new FtpConfig(21, "192.168.1.1", "test", "123456");
    }


    /**
     * 模拟更新DB中的配置
     *
     * @param port
     * @param host
     * @param user
     * @param password
     */
    public void updateFtpConfigToDB(int port, String host, String user, String password) {
        if (ftpConfig == null) {
            ftpConfig = new FtpConfig();
        }
        ftpConfig.setPort(port);
        ftpConfig.setHost(host);
        ftpConfig.setUser(user);
        ftpConfig.setPassword(password);
        //write to db...
        //TODO...
    }

    /**
     * 将配置同步到ZK
     */
    public void syncFtpConfigToZk() throws JsonProcessingException {
        ZkClient zk = ZKUtil.getZkClient();
        if (!zk.exists(ZKUtil.FTP_CONFIG_NODE_NAME)) {
            zk.createPersistent(ZKUtil.FTP_CONFIG_NODE_NAME, true);
        }
        zk.writeData(ZKUtil.FTP_CONFIG_NODE_NAME, ftpConfig);
        zk.close();
    }


}
      

ClientApp類如下:

package yjmyzz.test;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.concurrent.TimeUnit;


public class ClientApp {

    FtpConfig ftpConfig;


    private FtpConfig getFtpConfig() {
        if (ftpConfig == null) {
            //首次擷取時,連接配接zk取得配置,并監聽配置變化
            ZkClient zk = ZKUtil.getZkClient();
            ftpConfig = (FtpConfig) zk.readData(ZKUtil.FTP_CONFIG_NODE_NAME);
            System.out.println("ftpConfig => " + ftpConfig);

            zk.subscribeDataChanges(ZKUtil.FTP_CONFIG_NODE_NAME, new IZkDataListener() {

                @Override
                public void handleDataChange(String s, Object o) throws Exception {
                    System.out.println("ftpConfig is changed !");
                    System.out.println("node:" + s);
                    System.out.println("o:" + o.toString());
                    ftpConfig = (FtpConfig) o;//重新加載FtpConfig
                }

                @Override
                public void handleDataDeleted(String s) throws Exception {
                    System.out.println("ftpConfig is deleted !");
                    System.out.println("node:" + s);
                    ftpConfig = null;
                }
            });
        }
        return ftpConfig;

    }

    /**
     * 模拟程式運作
     *
     * @throws InterruptedException
     */
    public void run() throws InterruptedException {

        getFtpConfig();

        upload();

        download();
    }

    public void upload() throws InterruptedException {
        System.out.println("正在上傳檔案...");
        System.out.println(ftpConfig);
        TimeUnit.SECONDS.sleep(10);
        System.out.println("檔案上傳完成...");
    }


    public void download() throws InterruptedException {
        System.out.println("正在下載下傳檔案...");
        System.out.println(ftpConfig);
        TimeUnit.SECONDS.sleep(10);
        System.out.println("檔案下載下傳完成...");
    }


}
      

最終測試一把:

package yjmyzz.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;

/**
 * Created by jimmy on 15/6/27.
 */
public class ConfigTest {


    @Test
    public void testZkConfig() throws JsonProcessingException, InterruptedException {

        ConfigManager cfgManager = new ConfigManager();
        ClientApp clientApp = new ClientApp();

        //模拟【配置管理中心】初始化時,從db加載配置初始參數
        cfgManager.loadConfigFromDB();
        //然後将配置同步到ZK
        cfgManager.syncFtpConfigToZk();

        //模拟用戶端程式運作
        clientApp.run();

        //模拟配置修改
        cfgManager.updateFtpConfigToDB(23, "10.6.12.34", "newUser", "newPwd");
        cfgManager.syncFtpConfigToZk();

        //模拟用戶端自動感覺配置變化
        clientApp.run();

    }


}
      

輸出如下:

ftpConfig => test/[email protected]:21

正在上傳檔案...

test/[email protected]:21

檔案上傳完成...

正在下載下傳檔案...

檔案下載下傳完成...

...

ftpConfig is changed !

node:/config/ftp

o:newUser/[email protected]:23

newUser/[email protected]:23

從測試結果看,子應用在不重新開機的情況下,已經自動感覺到了配置的變化,皆大歡喜。最後提一句:明白這個思路後,文中的ZK,其實換成Redis也可以,【統一配置中心】修改配置後,同步到Redis緩存中,然後子應用也不用搞什麼監聽這麼複雜,直接從redis中實時取配置就可以了。具體用ZK還是Redis,這個看個人喜好。

  

作者:菩提樹下的楊過

出處:http://yjmyzz.cnblogs.com

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。