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();
}
}));
}
}