ZookeeperFactoryLocal

Zookeeper工具类
zookeeper配置

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "zookeeper")
public class ZookeeperConfig {
    /** zookeeper服务地址 **/
    private String urls;
    /** 服务注册根路径 **/
    private String serverRoot;
    /** session过期时间 **/
    private Integer sessionTimeOut;
    /** 连接超时时间 **/
    private Integer connectionTimeOut;
    /** 重试次数 **/
    private Integer retryNum;
    /** 重试时间间隔 **/
    private Integer retryTime;

    public String getUrls() {
        return urls;
    }

    public void setUrls(String urls) {
        this.urls = urls;
    }

    public String getNettyServerRoot() {
        return nettyServerRoot;
    }

    public void setNettyServerRoot(String nettyServerRoot) {
        this.nettyServerRoot = nettyServerRoot;
    }

    public Integer getSessionTimeOut() {
        return sessionTimeOut;
    }

    public void setSessionTimeOut(Integer sessionTimeOut) {
        this.sessionTimeOut = sessionTimeOut;
    }

    public Integer getConnectionTimeOut() {
        return connectionTimeOut;
    }

    public void setConnectionTimeOut(Integer connectionTimeOut) {
        this.connectionTimeOut = connectionTimeOut;
    }

    public Integer getRetryNum() {
        return retryNum;
    }

    public void setRetryNum(Integer retryNum) {
        this.retryNum = retryNum;
    }

    public Integer getRetryTime() {
        return retryTime;
    }

    public void setRetryTime(Integer retryTime) {
        this.retryTime = retryTime;
    }
}

工具类

import net.yueyang.config.ZookeeperConfig;
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.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * zookeeper连接工厂
 */
@Component
public class ZookeeperFactoryLocal {

    private static Logger logger = LoggerFactory.getLogger(ZookeeperFactoryLocal.class);

    @Autowired
    private ZookeeperConfig zookeeperConfig;

    private CuratorFramework client;

    public CuratorFramework getClient(){
        if(Objects.isNull(this.client) || Objects.equals(client.getState(), CuratorFrameworkState.STOPPED)){
            initClient();
        }
        return client;
    }

    private void initClient() {
        logger.info("初始化zookeeper连接,url:{}",zookeeperConfig.getUrls());
        //1 重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(zookeeperConfig.getRetryTime(), zookeeperConfig.getRetryNum());
        //2 通过工厂创建连接
        this.client = CuratorFrameworkFactory.builder()
                .connectString(zookeeperConfig.getUrls()).connectionTimeoutMs(zookeeperConfig.getConnectionTimeOut())
                .sessionTimeoutMs(zookeeperConfig.getSessionTimeOut())
                .retryPolicy(retryPolicy)
                .build();
        //3 开启连接
        client.start();
        //4 挂载结束时关闭zookeeper连接
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            logger.info("关闭zookeeper连接");
            //关闭zookeeper连接
            if(Objects.nonNull(client) || Objects.equals(client.getState(), CuratorFrameworkState.STARTED)){
                client.close();
            }
        }));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大道至简@EveryDay

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

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

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

打赏作者

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

抵扣说明:

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

余额充值