Curator实现zookeeper路径变化监听(节点变化监听)

项目使用zookeeper,需要监控节点信息的变化(监听到变化以后如何处理,需要后续的了解。。。)。以在zookeeper中配置的属于FTP的路径的监听实现来记录。


监听器的接口

package com.lancy.zookeeper;

import org.apache.curator.framework.CuratorFramework;

public interface IZKListener {
   
    void executor(CuratorFramework client);
}

监听器的实现

package com.lancy.zookeeper.listener;

import org.apache.curator.framework.CuratorFramework;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lancy.common.ConfigCommon;
import com.lancy.zookeeper.IZKListener;

/**
 * @ClassName: FtpConfigListener
 * @Description: FTP配置
 *
 */
public class FtpConfigListener implements IZKListener {
   
    private static Logger logger = LoggerFactory.getLogger(FtpConfigListener.class);
    private String  path;

    public FtpConfigListener(String path) {
        this.path = path;
    }

    @Override
    public void executor(CuratorFramework client) {
        final PathChildrenCache cached = new PathChildrenCache(client, path, true);
        //添加节点信息变化的监听器
        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:
                        logger.info("Connection error,waiting...");
                        break;
                    case CHILD_REMOVED:
                        logger.info("Child node removed...");
                        //从Map中删除
                        ConfigCommon.FTP.remove(event.getData().getPath()
                                .substring(event.getData().getPath().lastIndexOf("/") + 1));
                        break;
                    default:
                        logger.info("Child node new added or updated...");
                        //把添加到zookeeper中的节点信息添加到Map中
                        ConfigCommon.FTP.put(
                                event.getData().getPath().substring(event.getData().getPath().lastIndexOf("/") + 1),
                                new String(event.getData().getData()));
                }
            }
        });
        try {
            cached.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        } catch (Exception e) {
            logger.error("zookeeper listener error: ", e);
        }
    }

}

zookeeper客户端框架Curator的工厂类
该类用于实例化一个Curator实例,注册zookeeper监听器和启动zookeeper监听器

package com.lancy.zookeeper;

import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.UnhandledErrorListener;
import org
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值