Apache Curator Path Cache Watcher

可以监控某一路径的直接子结点(一级子结点)变化,add,update,delete。
利用此特性可以很方便的监控集群中的所有结点,当然也就很方便的可以实现简单的key.hashCode()%serverCount式的分布式计算,还可以实现简单的定制规则的负载均衡。
1.run ChildrenListener

2.run CLTest

[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.collonn.javaUtilMvn.zookeeper.curator.PathCache;  
  2.   
  3. public class CLTest {  
  4.     public static void main(String[] args) throws Exception {  
  5.         CLClient01.main(null);  
  6.         CLClient02.main(null);  
  7.         CLClient03.main(null);  
  8.     }  
  9. }  


[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.collonn.javaUtilMvn.zookeeper.curator.PathCache;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.framework.recipes.cache.ChildData;  
  7. import org.apache.curator.framework.recipes.cache.PathChildrenCache;  
  8. import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;  
  9. import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;  
  10. import org.apache.curator.retry.ExponentialBackoffRetry;  
  11. import org.apache.curator.utils.EnsurePath;  
  12.   
  13. import java.util.List;  
  14.   
  15. public class ChildrenListener {  
  16.     public static final String C_PATH = "/TestPath";  
  17.     public static final String CHARSET = "UTF-8";  
  18.   
  19.     public static void main(String[] args) {  
  20.         try {  
  21.             new Thread(new Runnable() {  
  22.                 @Override  
  23.                 public void run() {  
  24.                     try{  
  25.                         String zookeeperConnectionString = "127.0.0.1:2181";  
  26.                         RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  27.                         CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  28.                         client.start();  
  29.   
  30.                         //ensure path of /test  
  31.                         new EnsurePath(C_PATH).ensure(client.getZookeeperClient());  
  32.   
  33.                         final PathChildrenCache pathChildrenCache = new PathChildrenCache(client, C_PATH, true);  
  34.                         pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {  
  35.                             @Override  
  36.                             public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {  
  37.                                 System.out.println("================== catch children change ==================");  
  38.                                 System.out.println("===" + event.getType() + "," + event.getData().getPath() + "," + event.getData().getData());  
  39.                                 List<ChildData> childDataList = pathChildrenCache.getCurrentData();  
  40.                                 if (childDataList != null && childDataList.size() > 0) {  
  41.                                     System.out.println("===all children as:");  
  42.                                     for (ChildData childData : childDataList) {  
  43.                                         System.out.println("==" + childData.getPath() + "," + new String(childData.getData(), "UTF-8"));  
  44.                                     }  
  45.                                 }  
  46.                             }  
  47.                         });  
  48.                         pathChildrenCache.start();  
  49.   
  50.                         Thread.sleep(Integer.MAX_VALUE);  
  51.                         client.close();  
  52.                     }catch (Exception e){  
  53.                         e.printStackTrace();  
  54.                     }  
  55.                 }  
  56.             }).start();  
  57.         }catch (Exception e){  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61. }  


[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.collonn.javaUtilMvn.zookeeper.curator.PathCache;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.retry.ExponentialBackoffRetry;  
  7. import org.apache.zookeeper.CreateMode;  
  8. import org.apache.zookeeper.data.Stat;  
  9.   
  10. import java.util.Random;  
  11.   
  12. public class CLClient01 {  
  13.     public static final String C_PATH_SUB = ChildrenListener.C_PATH + "/dog";  
  14.   
  15.     public static void main(String[] args) {  
  16.         new Thread(new Runnable() {  
  17.             @Override  
  18.             public void run() {  
  19.                 try {  
  20.                     String zookeeperConnectionString = "127.0.0.1:2181";  
  21.                     RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  22.                     CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  23.                     client.start();  
  24.   
  25.                     Random random = new Random();  
  26.                     Thread.sleep(1000 * random.nextInt(3));  
  27.   
  28.                     Stat stat = client.checkExists().forPath(C_PATH_SUB);  
  29.                     if(stat == null){  
  30.                         client.create().withMode(CreateMode.EPHEMERAL).forPath(C_PATH_SUB, "dogData".getBytes(ChildrenListener.CHARSET));  
  31.                     }  
  32.   
  33.                     Thread.sleep(1000 * random.nextInt(3));  
  34.                     client.close();  
  35.                 } catch (Exception e) {  
  36.                     e.printStackTrace();  
  37.                 }  
  38.             }  
  39.         }).start();  
  40.     }  
  41. }  


[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.collonn.javaUtilMvn.zookeeper.curator.PathCache;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.retry.ExponentialBackoffRetry;  
  7. import org.apache.zookeeper.CreateMode;  
  8. import org.apache.zookeeper.data.Stat;  
  9.   
  10. import java.nio.charset.Charset;  
  11. import java.util.Random;  
  12.   
  13. public class CLClient02 {  
  14.     public static final String C_PATH_SUB = ChildrenListener.C_PATH + "/cat";  
  15.   
  16.     public static void main(String[] args) {  
  17.         new Thread(new Runnable() {  
  18.             @Override  
  19.             public void run() {  
  20.                 try {  
  21.                     String zookeeperConnectionString = "127.0.0.1:2181";  
  22.                     RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  23.                     CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  24.                     client.start();  
  25.   
  26.                     Random random = new Random();  
  27.                     Thread.sleep(1000 * random.nextInt(3));  
  28.   
  29.                     Stat stat = client.checkExists().forPath(C_PATH_SUB);  
  30.                     if(stat == null){  
  31.                         client.create().withMode(CreateMode.EPHEMERAL).forPath(C_PATH_SUB, "catData".getBytes(Charset.forName(ChildrenListener.CHARSET)));  
  32.                     }  
  33.   
  34.                     Thread.sleep(1000 * random.nextInt(3));  
  35.                     client.close();  
  36.                 } catch (Exception e) {  
  37.                     e.printStackTrace();  
  38.                 }  
  39.             }  
  40.         }).start();  
  41.     }  
  42. }  


[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.collonn.javaUtilMvn.zookeeper.curator.PathCache;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.retry.ExponentialBackoffRetry;  
  7. import org.apache.zookeeper.CreateMode;  
  8. import org.apache.zookeeper.data.Stat;  
  9.   
  10. import java.nio.charset.Charset;  
  11. import java.util.Random;  
  12.   
  13. public class CLClient03 {  
  14.     public static final String C_PATH_SUB = ChildrenListener.C_PATH + "/rabbit";  
  15.   
  16.     public static void main(String[] args) {  
  17.         new Thread(new Runnable() {  
  18.             @Override  
  19.             public void run() {  
  20.                 try {  
  21.                     String zookeeperConnectionString = "127.0.0.1:2181";  
  22.                     RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  23.                     CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  24.                     client.start();  
  25.   
  26.                     Random random = new Random();  
  27.                     Thread.sleep(1000 * random.nextInt(3));  
  28.   
  29.                     Stat stat = client.checkExists().forPath(C_PATH_SUB);  
  30.                     if(stat == null){  
  31.                         client.create().withMode(CreateMode.EPHEMERAL).forPath(C_PATH_SUB, "rabbitData".getBytes(Charset.forName(ChildrenListener.CHARSET)));  
  32.                     }  
  33.   
  34.                     Thread.sleep(1000 * random.nextInt(3));  
  35.                     client.close();  
  36.                 } catch (Exception e) {  
  37.                     e.printStackTrace();  
  38.                 }  
  39.             }  
  40.         }).start();  
  41.     }  
  42. }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值