• curator zookeeper监控。SpiderWatcher


    import java.util.List;
    
    import org.apache.curator.RetryPolicy;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    
    public class SpiderWatcher implements Watcher {
        CuratorFramework client = null;
        List<String> list1 = null;
    
        public SpiderWatcher() {
            String connectString = "192.168.14.139:2181";
            int sessionTimeoutMs = 30000;
            int connectionTimeoutMs = 30000;
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 3);
            client = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
            client.start();
            try {
                list1 = client.getChildren().usingWatcher(this).forPath("/dddd");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void process(WatchedEvent arg0) {
            try {
                List<String> list2 = client.getChildren().usingWatcher(this).forPath("/dddd");
                for (String znode : list1) {
                    if (!list2.contains(znode)) {
                        System.out.println("删除");
                    }
                }
                for (String znode : list2) {
                    if (!list1.contains(znode)) {
                        System.out.println("新增");
                    }
                }
                list1 = list2;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void stayAlive() {
            for (;;) {
            }
        }
    
        public static void main(String[] args) throws Exception {
            SpiderWatcher spiderWatcher = new SpiderWatcher();
            spiderWatcher.stayAlive();
        }
    }
  • 相关阅读:
    mpstat 查看多核CPU负载状态
    redis pipeset发布订阅
    sqlalchemyorm学生签到 成绩记录查询系统
    ORM数据库命令操作包装实例对象学习
    Python Mysql数据库操作
    redis hash操作 list列表操作
    pymysqlsqlalchemyorm
    ss命令用来显示处于活动状态的套接字信息。
    8月20日学习日志
    8月22日学习日志
  • 原文地址:https://www.cnblogs.com/sysout/p/5463498.html
Copyright © 2020-2023  润新知