使用Apache Curator監控Zookeeper的Node和Path的狀態

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   java   ar   

  1.Zookeeper經常被我們用來做組態管理,配置的管理在分布式應用環境中很常見,例如同一個應用系統需要多台 PC Server 運行,但是它們啟動並執行應用系統的某些配置項是相同的,如果要修改這些相同的配置項,那麼就必須同時修改每台運行這個應用系統的 PC Server,這樣非常麻煩而且容易出錯。像這樣的配置資訊完全可以交給 Zookeeper 來管理,將配置資訊儲存在 Zookeeper 的某個目錄節點中,然後將所有需要修改的應用機器監控配置資訊的狀態,一旦配置資訊發生變化,每台應用機器就會收到 Zookeeper 的通知,然後從 Zookeeper 擷取新的配置資訊應用到系統中。

 

                                                                                       

 

  我們通過Curator是如何?的呢? 那就是NodeCache,關於如何?,後面代碼給出說明。

 

  假如我們有多個服務儲存在Zookeeper的/services下,例如/services/service1,/services/service2......在service1,servce2下,儲存的有服務的ip,連接埠等資訊。如果我們需要增加服務,或者某個服務不可用了,從Zookeeper中刪除了,或者我們修改了某個Service下的ip和連接埠值,我們有必要第一時間內收到通知,已進行相應的處理,這時候可以怎麼辦呢? Curator提供了一個pathChildrenCache來滿足我們的需求。下面我們給出代碼來說明兩個的用法.

 

 

package com.hupengcool.cache;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.cache.ChildData;import org.apache.curator.framework.recipes.cache.NodeCache;import org.apache.curator.framework.recipes.cache.NodeCacheListener;import org.apache.curator.framework.recipes.cache.PathChildrenCache;import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.utils.CloseableUtils;import org.apache.curator.utils.EnsurePath;import java.util.List;/** * Created by hupeng on 2014/9/19. */public class Cache {    public static PathChildrenCache pathChildrenCache(CuratorFramework client, String path, Boolean cacheData) throws Exception {        final PathChildrenCache cached = new PathChildrenCache(client, path, cacheData);        cached.getListenable().addListener(new PathChildrenCacheListener() {             @Override            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {                PathChildrenCacheEvent.Type eventType = event.getType();                switch (eventType) {                    case CONNECTION_RECONNECTED:                        cached.rebuild();                        break;                    case CONNECTION_SUSPENDED:                    case CONNECTION_LOST:                        System.out.println("Connection error,waiting...");                        break;                    default:                        System.out.println("PathChildrenCache changed : {path:" + event.getData().getPath() + " data:" +                                new String(event.getData().getData()) + "}");                }            }        });        return cached;    }    public static NodeCache nodeCache(CuratorFramework client, String path) {        final NodeCache cache = new NodeCache(client, path);        cache.getListenable().addListener(new NodeCacheListener() {            @Override            public void nodeChanged() throws Exception {                System.out.println("NodeCache changed, data is: " + new String(cache.getCurrentData().getData()));            }        });        return cache;    }    public static void main(String[] args) throws Exception {        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1", retryPolicy);        client.start();        EnsurePath ensurePath = client.newNamespaceAwareEnsurePath("/create/test");        ensurePath.ensure(client.getZookeeperClient());        /**         * pathChildrenCache         */        PathChildrenCache cache = pathChildrenCache(client, "/create", true);        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);        List<ChildData> datas = cache.getCurrentData();        for (ChildData data : datas) {            System.out.println("pathcache:{" + data.getPath() + ":" + new String(data.getData())+"}");        }        /**         *    NodeCache         */        NodeCache nodeCache = nodeCache(client, "/create/test");        nodeCache.start(true);        client.setData().forPath("/create/test", "1111".getBytes());        System.out.println(new String(nodeCache.getCurrentData().getData()));        Thread.sleep(10000);        CloseableUtils.closeQuietly(cache);        CloseableUtils.closeQuietly(client);    }}

 

 

不足之處,請指正。(*^__^*) ,Thanks

 

使用Apache Curator監控Zookeeper的Node和Path的狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.