zookeeper zk 创建节点

import com.ctrip.framework.apollo.ConfigService;
import com.dianping.cat.Cat;
import com.weimob.artemis.http.constant.Constants;
import com.weimob.artemis.http.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 要写注释呀
 */
@Slf4j
@Component
public class ZkPointHandler {
    @Value("${zookeeper.url}")
    public String zookeeperUrl;
    @Value("${dubbo.protocol.port}")
    private String protocolPort;

    @PostConstruct
    public void create() {
        Cat.logEvent("ZKCurator init zk address", zookeeperUrl);
        ZkClient zkc = new ZkClient(new ZkConnection(zookeeperUrl), 5000);
        zkc.setZkSerializer(new MyZkSerializer());
        //获取本地的ip地址
        String localIp = IpUtils.getLocalIps();
        if (!zkc.exists(Constants.ARTEMIS_ZK_HTTP_SERVICE)) {
            zkc.createPersistent(Constants.ARTEMIS_ZK_HTTP_SERVICE);
        }

        //创建临时节点
        String path = Constants.ARTEMIS_ZK_HTTP_SERVICE + "/" + localIp + ":" + protocolPort;
        if (!zkc.exists(path)) {
            zkc.createEphemeral(path);
        }
        //防止节点丢失,重新创建
        ScheduledExecutorService checkZkPoint = Executors.newSingleThreadScheduledExecutor();
        checkZkPoint.scheduleWithFixedDelay(this::checkPoint, 1, 1, TimeUnit.MINUTES);
    }


    public List<String> getAddressList() {
        String zkAddress = ConfigService.getConfig("zookeeper").getProperty("zookeeper.url", "");
        log.info("zkAddress {}", zkAddress);
        ZkClient zkc = new ZkClient(new ZkConnection(zkAddress), 5000);
        //防止zk没有父节点
        if (!zkc.exists(Constants.ARTEMIS_ZK_HTTP_SERVICE)) {
            zkc.createPersistent(Constants.ARTEMIS_ZK_HTTP_SERVICE);
        }
        //从zk获取当前节点数
        List<String> list = zkc.getChildren(Constants.ARTEMIS_ZK_HTTP_SERVICE);
        Cat.logEvent(" artmis  zk info from list", list.toString());
        return list;
    }

    private void checkPoint() {
        ZkClient zkc = new ZkClient(new ZkConnection(zookeeperUrl), 5000);
        zkc.setZkSerializer(new MyZkSerializer());
        //获取本地的ip地址
        String localIp = IpUtils.getLocalIps();
        if (!zkc.exists(Constants.ARTEMIS_ZK_HTTP_SERVICE)) {
            zkc.createPersistent(Constants.ARTEMIS_ZK_HTTP_SERVICE);
        }

        //创建临时节点
        String path = Constants.ARTEMIS_ZK_HTTP_SERVICE + "/" + localIp + ":" + protocolPort;
        if (!zkc.exists(path)) {
            zkc.createEphemeral(path);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值