Curratorフレームの簡単な紹介と使用


もっと読む
Curratorフレームの簡単な紹介と使用
Curratorとは何ですか
  Curratorの発音͝ˌātər:"は、Zoo Keeperのkeeperです.
「Gava is to Java what Clator is to Zoo Keeper」
                                 ---Patrick Hunt,Zoo Keeper comiter
Curratorを使う
  • 接続
  • を取得する場合は、Zoo Keeper接続管理とリトライポリシーのパッケージだけが必要です.Currator接続例(Currator Frame ewark)は、Currator Frame ewark Factoryから入手できます.一つ
    CurratoFraamewarkはZoo Keeperクラスタに対応しています.コードはこのように見えます.
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3)
    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
    client.start();
    clientは必ず起動し、使用しない時はクローズします.
  • 直接Zoo Keeper
  • を呼び出すと、CurratoFrame ebookの例があると、Zoo Keeperを直接呼び出すことができます.
    client.create().forPath("/my/path", myData)
    これによりCurratorはZoo Keeper接続を管理し、接続が間違った場合は再試行操作が行われます.
    秘訣
  • スプリットロック
  • InterProcessMutex lock = new InterProcessMutex(client, lockPath);
    if ( lock.acquire(maxWait, waitUnit) ) 
    {
        try 
        {
            // do some work inside of the critical section here
        }
        finally
        {
            lock.release();
        }
    }
  • 指導選挙
  • LeaderSelectorListener listener = new LeaderSelectorListenerAdapter()
    {
        public void takeLeadership(CuratorFramework client) throws Exception
        {
            // this callback will get called when you are the leader
            // do whatever leader work you need to and only exit
            // this method when you want to relinquish leadership
        }
    }
    
    LeaderSelector selector = new LeaderSelector(client, path, listener);
    selector.autoRequeue();  // not required, but this is behavior that you will probably expect
    selector.start();
    公共タイプZKPaths
    静的な方法で、Zoo Keeper ZNode pathsを操作します.
  • get NodeFroomPath:全パスを与え、ノード名を返す.例  「/one/two/three」は、three
  • に戻ります.
  • mkdirs:path内のすべてのノードを作成します.
  • getSortedChildren:順序seqのサブセット
  • を返す.
  • makePath:pathとnodeを統合する.
  • コードの例:
    https://github.com/apache/curator/tree/master/curator-examples
    参照
    https://cwiki.apache.org/confluence/display/CURATOR/Tech+Notes
    http://curator.apache.org/index.html