1. 程式人生 > >zookeeper【1】配置管理

zookeeper【1】配置管理

err implement exceptio zab code hang create serial scribe

為什麽要用統一配置?

我們做項目時用到的配置比如數據庫配置等...我們都是寫死在項目裏面,如果需要更改,那麽也是的修改配置文件然後再投產上去,那麽問題來了,如果做集群的呢,有100臺機器,這時候做修改那就太不切實際了;那麽就需要用到統一配置管理啦。

采用方案

1.公共配置抽取存放於zookeeper中並落地數據庫

2.對公共配置修改後發布到zookeeper中並落地數據庫

3.對應用開啟配置實時監聽,zookeeper配置文件一旦被修改,應用可實時監聽到並獲取

實現代碼:

import java.io.Serializable;

public class Config implements
Serializable{ /** * */ private static final long serialVersionUID = 1L; private String userNm; private String userPw; public Config() { } public Config(String userNm, String userPw) { this.userNm = userNm; this.userPw = userPw; } public String getUserNm() {
return userNm; } public void setUserNm(String userNm) { this.userNm = userNm; } public String getUserPw() { return userPw; } public void setUserPw(String userPw) { this.userPw = userPw; } @Override public String toString() { return "Config [userNm=" + userNm + ", userPw=" + userPw + "]"; } }
import org.I0Itec.zkclient.ZkClient;

public class ZkConfigMag {

    private Config config;
    /**
     * 從數據庫加載配置
     */
    public Config downLoadConfigFromDB(){
        //getDB
        config = new Config("nm", "pw");
        return config;
    }

    /**
     * 配置文件上傳到數據庫
     */
    public void upLoadConfigToDB(String nm, String pw){
        if(config==null)config = new Config();
        config.setUserNm(nm);
        config.setUserPw(pw);
        //updateDB
    }

    /**
     * 配置文件同步到zookeeper
     */
    public void syncConfigToZk(){
        ZkClient zk = new ZkClient("localhost:2181");
        if(!zk.exists("/zkConfig")){
            zk.createPersistent("/zkConfig",true);
        }
        zk.writeData("/zkConfig", config);
        zk.close();
    }
}
public class ZkConfigTest {

    public static void main(String[] args) {
        ZkConfigMag mag = new ZkConfigMag();
        Config config = mag.downLoadConfigFromDB();
        System.out.println("....加載數據庫配置...."+config.toString());
        mag.syncConfigToZk();
        System.out.println("....同步配置文件到zookeeper....");

        //歇會,這樣看比較清晰
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mag.upLoadConfigToDB("ccccc", "ppppp");
        System.out.println("....修改配置文件...."+config.toString());
        mag.syncConfigToZk();
        System.out.println("....同步配置文件到zookeeper....");


    }

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

public class ZkGetConfigClient {

    private Config config;

    public Config getConfig() {
        ZkClient zk = new ZkClient("localhost:2181");
        config = (Config)zk.readData("/zkConfig");
        System.out.println("加載到配置:"+config.toString());

        //監聽配置文件修改
        zk.subscribeDataChanges("/zkConfig", new IZkDataListener(){
            @Override
            public void handleDataChange(String arg0, Object arg1)
                    throws Exception {
                config = (Config) arg1;
                System.out.println("監聽到配置文件被修改:"+config.toString());
            }

            @Override
            public void handleDataDeleted(String arg0) throws Exception {
                config = null;
                System.out.println("監聽到配置文件被刪除");
            }

        });
        return config;
    }
    public static void main(String[] args) {
        ZkGetConfigClient client = new ZkGetConfigClient();
        client.getConfig();
        System.out.println(client.config.toString());
        for(int i = 0;i<100;i++){
            System.out.println(client.config.toString());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }


}

zookeeper【1】配置管理