zookeeper实现程序高可用

<curator.version>5.2.1</curator.version>
<!--Curator-->
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>${curator.version}</version>
</dependency>

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>${curator.version}</version>
</dependency>

几个关键代码:

package com.xxx.common.zk;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.Setter;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Date 2023/8/25 14:10
 */
@Setter
public class ZkClient {

    private final Logger logger = LoggerFactory.getLogger(ZkClient.class);

    private CuratorFramework client;
    private String zookeeperServer;
    private int sessionTimeoutMs;
    private int connectionTimeoutMs;
    private int baseSleepTimeMs = 1000;
    private int maxRetries = 3;

    LeaderSelector selector = null;

    public ZkClient(String zookeeperServer, int connectionTimeoutMs, int sessionTimeoutMs) {
        this.zookeeperServer = zookeeperServer;
        this.sessionTimeoutMs = sessionTimeoutMs;
        this.connectionTimeoutMs = connectionTimeoutMs;
        init();
    }

    public void init() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
        client = CuratorFrameworkFactory.builder()
                .connectString(zookeeperServer)
                .retryPolicy(retryPolicy)
                .sessionTimeoutMs(sessionTimeoutMs)
                .connectionTimeoutMs(connectionTimeoutMs)
                .build();

        client.start();
        try {
            assert client.getState().equals(CuratorFrameworkState.STARTED);
            client.blockUntilConnected();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public CuratorFramework getClient() {
        return client;
    }

    public List<String> getChildren(String path) {
        List<String> childrenList = new ArrayList<>();
        try {
            childrenList = client.getChildren().forPath(path);
        } catch (Exception e) {
            logger.error("##获取子节点出错##", e);
            e.printStackTrace();
        }

        return childrenList;
    }

    public int getChildrenCount(String path) {
        return getChildren(path).size();
    }

    public List<String> getInstances() {
        return getChildren("/services");
    }

    public int getInstanceCount() {
        return getInstances().size();
    }

    /**
     * 监听某个路径,几个服务竞争用此方法
     * @param path
     */
    public void listenerPath(String path, String id, LeaderSelectorListener listener){
        try {
            selector = new LeaderSelector(client, path, listener);
            selector.setId(id);
            selector.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判定是不是leader
     * @return
     */
    public boolean isLeader(){

        return selector.hasLeadership();
    }

    /**
     * 获取leader里面存的data,获取leader状态的服务器用此方法
     * @return
     */
    public String getLeader(String path) {

        List<String> childrenList;
        try {
            childrenList = client.getChildren().forPath(path);
            System.out.println("childrenList " + childrenList.size());
            childrenList.sort(new ChildrenComparator());
            String leaderPath = path + "/" + childrenList.get(0);
            return new String(client.getData().forPath(leaderPath));
        } catch (Exception e) {
            logger.error("##获取子节点数据出错##", e);
            e.printStackTrace();
        }
        return null;
    }

    public void stop() {
        if(selector != null){
            CloseableUtils.closeQuietly(selector);
        }

        if(client != null){
            CloseableUtils.closeQuietly(client);
        }
    }

    static class ChildrenComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            int oo1 = getSequential(o1);
            int oo2 = getSequential(o2);
            return Integer.compare(oo1, oo2);
        }
    }

    private static int getSequential(String s){
        return Integer.parseInt(s.substring(s.lastIndexOf("-") + 1));
    }

    /**
     * 创建临时节点,临时节点名是备份队列的名字,便于master做故障恢复
     * @param path
     */
    public void createPath(String path) {
        try {
            String s = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path);
            logger.info("######创建临时节点["+s+"]成功######");
        }catch (Exception e){
            logger.error("######创建临时节点["+path+"]失败######", e);
        }
    }
}

package com.xxx.etl.master.common;

import java.lang.management.ManagementFactory;
import java.util.concurrent.CountDownLatch;
import lombok.Getter;
import lombok.Setter;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.utils.CloseableUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Date 2023/8/21 20:25
 */
@Setter
@Getter
public class LeaderSelectorListenerImpl implements LeaderSelectorListener {
    private static Logger log = LogManager.getLogger(LeaderSelectorListenerImpl.class);

    private CountDownLatch countDownLatch;

    public LeaderSelectorListenerImpl(CountDownLatch countDownLatch){
        this.countDownLatch = countDownLatch;
    }
    // 被选举为Leader时调用
    @Override
    public void takeLeadership(CuratorFramework client) throws Exception {
        log.info("##获取到了leader权##");
        countDownLatch.countDown();
        while(true); //阻塞,一直持有leader,不加的话会释放
    }

    // 当连接状态发生变化时调用
    @Override
    public void stateChanged(CuratorFramework client, ConnectionState newState) {
        if(!newState.equals(ConnectionState.CONNECTED)){
            //状态改变,直接杀掉
            log.info("##连接新状态: {}##", newState.name());
            CloseableUtils.closeQuietly(client);
            try {
                //杀掉程序,写个shell脚本再拉起来
                String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
                Runtime.getRuntime().exec("kill -9 " + pid);
                log.warn("## 即将关闭Master程序(当前节点) ##");
                System.exit(0);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

 上面是两个核心代码:

下面是使用方式:

/**
     * zk的启动,监听等
     */
    public static boolean zkStart(){
        //连接信息
        ZkClient zkClient = new ZkClient(PropertiesService.getProperty("ETL.LOG.ZOOKEEPER"),
                Integer.parseInt(PropertiesService.getProperty("zk.connectionTimeoutMs")),
                Integer.parseInt(PropertiesService.getProperty("zk.sessionTimeoutMs")));
        //添加监听器
        CountDownLatch isLeader = new CountDownLatch(1);
        LeaderSelectorListener listener = new LeaderSelectorListenerImpl(isLeader);
        log.info("##等待获取leader##");
        try {
            //主机ip作为存储的data
            String hostAddress = InetAddress.getLocalHost().getHostAddress() + ":" + System.getProperty("rmi.port");
            zkClient.listenerPath(PATH, hostAddress, listener);
            isLeader.await();//上面获取执行权后才执行下面的步骤
            return zkClient.isLeader();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

使用的时候 if(zkStart()) {

}

有问题留言交流,互相学习。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早退的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值