zookeeper-curator

Curator教程:https://www.jianshu.com/p/70151fc0ef5d

Curator包含了几个包:
  • curator-framework:对zookeeper的底层api的一些封装
  • curator-client:提供一些客户端的操作,例如重试策略等
  • curator-recipes:封装了一些高级特性,如:Cache事件监听、选举、分布式锁、分布式计数器、分布式Barrier等
spring:
  application:
    name: zookeeper
zookeeper:
  server: 127.0.0.1:2181
  znode: /kexin_zookeeper/test
  client_id: zk_client
package com.kexin.zookeeper.ha;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author KeXin
 * @Date 2018/7/24 上午10:38
 **/
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "zookeeper")
public class ZkClientConfig {
    @Getter
    @Setter
    private String server;
    @Getter
    @Setter
    private String znode;
    @Getter
    @Setter
    private String client_id;

    // 是否是leader 默认为false
    public static boolean isLeader = false;

    @Bean
    public ZkClient getZkClient() throws Exception {

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client = CuratorFrameworkFactory
                .builder()
                .connectString(server)
                .connectionTimeoutMs(50000)
                .sessionTimeoutMs(60000)
                .retryPolicy(retryPolicy)
                .namespace("testZK")    //创建名称空间,指定后本client所创建的所有节点都在此目录下
                .build();

        LeaderLatch leaderLatch = new LeaderLatch(client,znode,client_id,LeaderLatch.CloseMode.NOTIFY_LEADER);
        leaderLatch.addListener(new LeaderLatchListener() {
            @Override
            public void isLeader() {
                log.info("this client is the leader");
                isLeader = true;
            }

            @Override
            public void notLeader() {
                log.info("this client is not the leader");
                isLeader = false;
            }
        });
        ZkClient zkClient = new ZkClient(client,leaderLatch);
        zkClient.startZKClient();
        return zkClient;
    }
}
package com.kexin.zookeeper.ha;

import lombok.Getter;
import lombok.Setter;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderLatch;

/**
 * @Author KeXin
 * @Date 2018/7/24 上午10:34
 **/
public class ZkClient {
    @Getter
    @Setter
    private CuratorFramework client;

    @Getter
    @Setter
    private LeaderLatch leaderLatch;

    public ZkClient(CuratorFramework client, LeaderLatch leaderLatch){
        this.client = client;
        this.leaderLatch = leaderLatch;
    }
    /**
     * 启动客户端
     *
     * @throws Exception
     */
    public void startZKClient() throws Exception {
        client.start();
        leaderLatch.start();
    }

    /**
     * 关闭客户端
     *
     * @throws Exception
     */
    public void closeZKClient() throws Exception {
        leaderLatch.close();
        client.close();
    }

    /**
     * 判断是否变为领导者
     *
     * @return
     */
    public boolean hasLeadership() {
        return leaderLatch.hasLeadership() && ZkClientConfig.isLeader;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小黄鸭and小黑鸭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值