zookeeper跨叢集資料拷貝的例子

來源:互聯網
上載者:User

有這麼一個情境,多套測試的zookeeper叢集之間資料拷貝,按之前的理解,如果可以的話,直接拷貝zk的data檔案可以解決問題。
但是最近碰到這麼一個事情,只是無意中刪除了一個叢集某一個路徑下的資料,由於data資料並不可讀,不能有選擇的copy。所以,在這情境下,只能通過代碼的方式解決了。
 
我所用到的2個情境:
1. 誤刪除資料,從其它zookeeper叢集拷貝
2. 搭建測試環境,直接從線上匯入部分節點資料到本地
 
zookeeper管理node,基本和我們的檔案系統一致,這個需求就變得非常簡單,可以直接轉換為遞迴一個路徑,然後在新的叢集下建立。簡單的實現如下:
 
import com.metaboy.common.zk.dao.ZkDaoImpl;
import com.netflix.curator.framework.CuratorFramework;
import com.netflix.curator.framework.CuratorFrameworkFactory;
import com.netflix.curator.retry.RetryNTimes;
 
import java.util.List;
 
/**
 * @author yuxiong.wangy
 *         Date: 14-8-14
 *         Time: 下午7:33
 */
public class ZookeeperDemo {
    protected static CuratorFramework client_src;
    protected static CuratorFramework client_dst;
    protected static String  namespace_src;
    protected static String  namespace_dst;
    protected static String zkRoot_src = “/app”;
 
    public static void main(String[] args) throws InterruptedException {
        String zkConnectionStr_src = “*.*.*.*:2181,*.*.*.*:2181,*.*.*.*:2181”;
        client_src = getZKClient(zkConnectionStr_src,namespace_src,60000);
        String zkConnectionStr_dst = “*.*.*.*:2181,*.*.*.*:2181,*.*.*.*:2181”;
        client_dst = getZKClient(zkConnectionStr_dst,namespace_dst,60000);
 
        copyDataRecursion(zkRoot_src);
    }
 
    /*
     *       遞迴拷貝資料
     */
    public static void  copyDataRecursion(String parent){
        List<String> groups = ZkDaoImpl.getChildren(client_src, parent);
        if(groups.size() > 0){
            for(String group: groups){
                String path = parent+”/”+group;
 
                if(ZkDaoImpl.getData(client_src, path) != null){
                    ZkDaoImpl.createPersistentFile(client_dst, path, ZkDaoImpl.getData(client_src, path));
                    System.out.println(“[” + path + “]:” + ZkDaoImpl.getData(client_src,path));
                }else{
                    ZkDaoImpl.createPersistentFile(client_dst, path);
                    System.out.println(“[” + path + “]:”);
                }
 
                if(ZkDaoImpl.getChildren(client_src,path).size() > 0){
                    copyDataRecursion(path);
                }
            }
        }else {
            ZkDaoImpl.createPersistentFile(client_dst,parent,ZkDaoImpl.getData(client_src,parent));
        }
    }
 
    public static CuratorFramework getZKClient(String zkConnectionStr, String namespace, int sessionTimeout) throws InterruptedException {
        int connectTimeout = 60000;
        int retry = 3;
        int retryTimeout = 10000;
        CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkConnectionStr)
                .retryPolicy(new RetryNTimes(retry, retryTimeout)).connectionTimeoutMs(connectTimeout)
                .sessionTimeoutMs(sessionTimeout).namespace(namespace).build();
        client.start();
        client.getZookeeperClient().blockUntilConnectedOrTimedOut();
        return client;
    }
}
 
ZkDaoImpl封裝一個對ZK的操作的集合,類似下面這種,將原生的方法封裝了下,使用上是差不多:
    /**
     * 擷取檔案資料
     *
     * @param path
     *            檔案路徑
     * @return 檔案內容
     */
    public static String getData(CuratorFramework client, String path) {
        try {
            return new String(client.getData().forPath(path));
        } catch (Exception e) {
            throw new ZkException(ZkErrors.GET_DATA_EXCEPTION, “path:” + path, e);
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.