- /**
- * 这是一简单的接口类
- * @author tianji
- *
- */
public interface ZookeeperService {
void create(String path, byte[] data, boolean ephemeral);
byte[] getData(String path);
String getConfigData(String path);
void setData(String path, byte[] data) throws Exception;
void delete(String path);
List<String> getChildren(String path);
boolean checkExists(String path, String activityCategory, String pathType) throws Exception;
}
- /**
- * 这是接口的实现类
- * @author tianji
- *
- */
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
public class ZookeeperServiceImpl implements ZookeeperService{
private static final int CONNECTIONTIMEOUTMS = 15000;
private static final int SESSIONTIMEOUTMS = 60000;
privatefinal int BASESLEEPTIMEMS = 1000;
private static final int MAXRETRIES = 3;
private static final int MAXSLEEPMS = 5000;
private CuratorFramework client;
//在此笔者使用的是CuratorFramework,是一款ZooKeeper客户端工具,封装了大量的 zookeeper api
//在创建这个类时就调用此构造方法连接zookeeper服务器
//并设置相关的zookeeper参数
public ZookeeperServiceImpl(){
this.client = CuratorFrameworkFactory.builder()
.connectString(“192.168.1.111:2181”)
.connectionTimeoutMs(CONNECTIONTIMEOUTMS)
.sessionTimeoutMs(SESSIONTIMEOUTMS)
.retryPolicy(new ExponentialBackoffRetry(BASESLEEPTIMEMS, MAXRETRIES, MAXSLEEPMS))
.build();
}
//在zookeeper服务器上创建节点
//当 ephemeral 为 true 创建永久节点,为false时创建临时节点(当此连接断开时,即自动删除)
@Override
public void create(String path, byte[] data, boolean ephemeral) {
if(data == null || data.length == 0) {
data = new byte[0];
}
try {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
if(ephemeral) {
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, data);
} else {
client.create().creatingParentsIfNeeded().forPath(path, data);
}
} catch(Exception e) {
e.printStackTrace();
throw new IllegalStateException(e.getMessage());
}
}
//检查服务器上是否存在某个节点
@Override
public boolean checkExists(String path, String activityCategory, String pathType) throws Exception {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
if(client.checkExists().forPath(path) == null){
this.client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, new byte[0]);
}
if(client.checkExists().forPath(path) == null){
return false;
}else{
return true;
}
}
//获取某个节点的数据
@Override
public byte[] getData(String path) {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
try {
if(client.checkExists().forPath(path) == null){
return new byte[0];
}else{
return client.getData().forPath(path);
}
} catch(Exception e) {
throw new IllegalStateException(e.getMessage());
}
}
//设置某个节点的数据
@Override
public void setData(String path, byte[] data) {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
try {
if(data == null || data.length == 0) {
data = new byte[0];
}
client.setData().forPath(path, data);
} catch(Exception e) {
e.printStackTrace();
throw new IllegalStateException(e.getMessage());
}
}
//删除某个节点
@Override
public void delete(String path) {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
try {
client.delete().forPath(path);
} catch(Exception e) {
throw new IllegalStateException(e.getMessage());
}
}
//获取某个节点下的所有子节点
@Override
public List<String> getChildren(String path) {
if(client == null || ! client.getZookeeperClient().isConnected()){
client.start();
}
try {
return client.getChildren().forPath(path);
} catch(Exception e) {
throw new IllegalStateException(e.getMessage());
}
}
}