5.Java操作zookeeper客户端Curator
Curator客户端
POM
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
YML
curator:
#重试次数
retryCount: 5
#每次重试间隔的时间
elapsedTimeMs: 5000
#zookeeper地址,集群逗号分隔
connectUrls: 192.168.0.104:2181 #192.168.0.100:2181,192.168.0.103:2181
sessionTimeOutMs: 60000
connectionTimeOutMs: 5000
Java配置文件
CuratorConf.java
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CuratorConf {
@Autowired
CuratorProperties curatorProperties;
@Bean
public CuratorFramework curatorFramework(){
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
curatorProperties.getConnectUrls(),
curatorProperties.getSessionTimeOutMs(),
curatorProperties.getConnectionTimeOutMs(),
new RetryNTimes(curatorProperties.getRetryCount(), curatorProperties.getElapsedTimeMs())
);
curatorFramework.start();
return curatorFramework;
}
}
CuratorProperties.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class CuratorProperties {
private int retryCount;
private int elapsedTimeMs;
private String connectUrls;
private int sessionTimeOutMs;
private int connectionTimeOutMs;
}
Demo
基本操作
@Autowired
CuratorFramework curatorFramework;
@Test
void create() throws Exception {
//添加持久节点,并且设置值为abc
curatorFramework.create().forPath("/curator-node4","abc".getBytes());
//更新节点下的数据
curatorFramework.setData().forPath("/curator-node4","bcd".getBytes());
//获得节点下的数据,如果没有数据获得ip
byte[] bytes = curatorFramework.getData().forPath("/curator-node4");
System.out.println( new String( bytes));
//创建时,如果父节点未创建,则先创建父节点
curatorFramework.create().creatingParentContainersIfNeeded().forPath("/curator-node5/curator-node5-1");
//节点删除
//guaranteed()接口是一个保障措施,只要客户端会话有效,那么Curator会在后台持续进行删除操作,直到删除节点成功。
curatorFramework.delete().forPath("/curator-node");
curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath("/curator-node5/curator-node5-1");
//创建临时序号节点,当线程执行完,该节点删除
String s = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/es-node");
System.out.println(s);
}
监听
CuratorCache cache = CuratorCache.build(curatorFramework, "/watchNode");
CuratorCacheListener listener = CuratorCacheListener.builder()
.forCreates(node -> System.out.println(String.format("创建节点: [%s]", node)))
.forChanges((oldNode, node) -> System.out.println(String.format("修改节点数据. Old: [%s] New: [%s]", oldNode, node)))
.forDeletes(oldNode -> System.out.println(String.format("删除节点. Old value: [%s]", oldNode)))
.forInitialized(() -> System.out.println("监听器初始化"))
.build();
// register the listener
cache.listenable().addListener(listener);
// the cache must be started
cache.start();
//防止监听关闭
System.in.read();
读写锁
InterProcessReadWriteLock lock=new InterProcessReadWriteLock(curatorFramework,"/lock");
InterProcessReadWriteLock.ReadLock readLock = lock.readLock();//获得读锁
readLock.acquire();//尝试获得读锁,如果最小节点是写锁,则阻塞等待
//获得锁,开始业务
readLock.release();//释放读锁
InterProcessReadWriteLock.WriteLock writeLock = lock.writeLock();//获得写锁
writeLock.acquire();//尝试获得写锁,如果节点中有锁,则阻塞等待
//获得锁,开始业务
writeLock.release();//释放写锁