开源客户端Curator 使用(下)

一 新建curatorupdatedata
1 代码
package com.cakin.zookerper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.data.Stat;
public class curatorupdatedata {
     public static void main(String[] args) throws Exception {;
           RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
           CuratorFramework client = CuratorFrameworkFactory
                     .builder()
                     .connectString("192.168.0.110:2181")
                     .sessionTimeoutMs(5000)
                     .connectionTimeoutMs(5000)
                     .retryPolicy(retryPolicy)
                     .build();
           client.start();
           
           Stat stat = new Stat();
          client.getData().storingStatIn(stat).forPath("/cakin");
          client.setData().withVersion(stat.getVersion()).forPath("/cakin", "update".getBytes());
           Thread.sleep(Long.MAX_VALUE);
     }
}
2 运行结果
/cakin节点数据被修改为update

二 新建curatorcheckexit
1 代码
package com.cakin.zookerper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.data.Stat;
public class curatorcheckexit {
     public static void main(String[] args) throws Exception {;
           RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
           CuratorFramework client = CuratorFrameworkFactory
                     .builder()
                     .connectString("192.168.0.110:2181")
                     .sessionTimeoutMs(5000)
                     .connectionTimeoutMs(5000)
                     .retryPolicy(retryPolicy)
                     .build();
           client.start();
           
           Stat stat = client.checkExists().forPath("/cakin");
           System.out.println(stat);
           
           Thread.sleep(Long.MAX_VALUE);
     }
}
2 运行结果
42949672981,47244640268,1515239231022,1515287642275,2,4,0,0,6,0,42949672994

三 新建curatorasyncall
1 代码
package com.cakin.zookerper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.api.CuratorEventType;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.data.Stat;
public class curatorasyncall {
     public static void main(String[] args) throws Exception {;
           ExecutorService es = Executors.newFixedThreadPool(5);
           RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
           CuratorFramework client = CuratorFrameworkFactory
                     .builder()
                     .connectString("192.168.0.110:2181")
                     .sessionTimeoutMs(5000)
                     .connectionTimeoutMs(5000)
                     .retryPolicy(retryPolicy)
                     .build();
           client.start();
           
           client.checkExists().inBackground(new BackgroundCallback() {
                
                public void processResult(CuratorFramework arg0, CuratorEvent arg1) throws Exception {
                     // TODO Auto-generated method stub
                     Stat stat = arg1.getStat();
                     System.out.println(stat);
                     
                     Object o = arg1.getContext();
                     System.out.println(o);
                     System.out.println(arg1.getPath());
                     System.out.println(arg1.getChildren());
                }
           },"123",es).forPath("/cakin");
           
           Thread.sleep(Long.MAX_VALUE);
     }
}
2 运行结果
42949672981,47244640268,1515239231022,1515287642275,2,4,0,0,6,0,42949672994
123
/cakin
null

四 新建curatoranodelistener
1 编辑pom.xml
     </dependency>
           <dependency>
         <groupId>org.apache.curator</groupId>
         <artifactId>curator-recipes</artifactId>
         <version>2.8.0</version>
     </dependency>
2 代码
package com.cakin.zookerper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.RetryUntilElapsed;
public class curatoranodelistener {
     public static void main(String[] args) throws Exception {;
           RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
           CuratorFramework client = CuratorFrameworkFactory
                     .builder()
                     .connectString("192.168.0.110:2181")
                     .sessionTimeoutMs(5000)
                     .connectionTimeoutMs(5000)
                     .retryPolicy(retryPolicy)
                     .build();
           client.start();
           final NodeCache cache = new NodeCache(client, "/cakin");
           cache.start();
           cache.getListenable().addListener(new NodeCacheListener() {
                
                public void nodeChanged() throws Exception {
                     byte[] ret =cache.getCurrentData().getData();
                     System.out.println("new date:"+new String(ret));
                }
           });
           
           Thread.sleep(Long.MAX_VALUE);
     }
}
3 运行结果
new date:update
new date:data chage

五 新建curatoranodechildlistener
1 代码
package com.cakin.zookerper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
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.RetryUntilElapsed;
public class curatoranodechildlistener {
    public static void main(String[] args) throws Exception {;
        RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
        CuratorFramework client = CuratorFrameworkFactory
                .builder()
                .connectString("192.168.0.110:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .build();
        client.start();
        final PathChildrenCache cache= new PathChildrenCache(client, "/FirstZnode",true);
        cache.start();
        cache.getListenable().addListener(new PathChildrenCacheListener() {
            
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                switch(event.getType()){
                case CHILD_ADDED:
                    System.out.println("CHILD_ADDED"+event.getData().getPath());
                    break;
                case CHILD_REMOVED:
                    System.out.println("CHILD_REMOVED"+event.getData().getPath());
                    break;
                case CHILD_UPDATED:
                    System.out.println("CHILD_UPDATED"+event.getData().getPath());
                    break;
                default:
                    break;
                }
                
            }
        });
        
        Thread.sleep(Long.MAX_VALUE);
    }
}
2 运行结果
CHILD_ADDED/FirstZnode/node4
CHILD_ADDED/FirstZnode/node2
CHILD_ADDED/FirstZnode/node3
CHILD_ADDED/FirstZnode/node1
CHILD_ADDED/FirstZnode/node5
CHILD_UPDATED/FirstZnode/node4
CHILD_REMOVED/FirstZnode/node5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值