Curator_Start
package test.ygy.curator;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by guoyao on 2017/8/27.
*/
public class Curator_Start {
private static final Logger log = LoggerFactory.getLogger(Curator_Start.class);
public static final String CONNECT_STRING="192.168.150.130:2181,192.168.150.132:2181,192.168.150.133:2181";
public static final String CONNECT_STRING_YGY = "www.ygy.com:2181";
public static final int SESSION_TIMEOUT_MS = 5000;
public static final int CONNECTION_TIMEOUT_MS = 5000 ;
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework curatorFramework=CuratorFrameworkFactory.builder()
.connectString(CONNECT_STRING)
.sessionTimeoutMs(SESSION_TIMEOUT_MS)
.connectionTimeoutMs(CONNECTION_TIMEOUT_MS)
.namespace("isolation")
.retryPolicy(retryPolicy).build();
curatorFramework.start();
log.warn(" 启动成功");
Thread.sleep(10000000);
curatorFramework.close();
}
public static CuratorFrameworkFactory.Builder getYGYBuilder() {
return CuratorFrameworkFactory.builder()
.connectString(CONNECT_STRING_YGY)
.sessionTimeoutMs(SESSION_TIMEOUT_MS)
.retryPolicy(new ExponentialBackoffRetry(1000,3));
}
public static CuratorFrameworkFactory.Builder getBaseBuilder() {
return CuratorFrameworkFactory.builder()
.connectString(CONNECT_STRING)
.sessionTimeoutMs(SESSION_TIMEOUT_MS)
.retryPolicy(new ExponentialBackoffRetry(1000,3));
}
}
Curator_Crud
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by guoyao on 2017/8/27.
*/
public class Curator_Crud {
private static final Logger log=LoggerFactory.getLogger(Curator_Crud.class);
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getBaseBuilder().build();
cf.start();
log.warn(" zookeeper 启动成功");
Stat stat=cf.setData()
.withVersion(-1)
.forPath("/test", "new data ".getBytes());
log.warn(" updated stat = " + stat);
Thread.sleep(2000);
cf.close();
}
}
Cruator_BackGround
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by guoyao on 2017/8/27.
*/
public class Cruator_BackGround {
private static final Logger log =LoggerFactory.getLogger(Cruator_BackGround.class);
public static void main(String[] agrs) throws Exception {
ExecutorService executorService=Executors.newFixedThreadPool(2);
CountDownLatch countDownLatch=new CountDownLatch(4);
CuratorFramework cf=Curator_Start.getBaseBuilder().build();
cf.start();
log.warn(" zookeeper 启动成功");
Thread.sleep(10000);
//使用线程池
cf.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.inBackground(
(x, y) -> { //x ,y : CuratorFramework client 客户端 CuratorEvent event事件
log.warn(" event = " + y);
log.warn(" execcutors threadName = " + Thread.currentThread().getName());
countDownLatch.countDown();
}
,executorService).forPath("/test", "test back".getBytes());
//00:28:38.572 [pool-1-thread-1] WARN test.ygy.curator.Cruator_BackGround - event = CuratorEventImpl{type=CREATE, resultCode=0, path='/test', name='/test', children=null, context=null, stat=null, data=null, watchedEvent=null, aclList=null}
//00:28:38.572 [pool-1-thread-1] WARN test.ygy.curator.Cruator_BackGround - execcutors threadName = pool-1-thread-1
cf.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.inBackground(
(x, y) -> { //x ,y : CuratorFramework client 客户端 CuratorEvent event事件
log.warn(" event = " + y);
log.warn(" execcutors threadName = " + Thread.currentThread().getName());
countDownLatch.countDown();
}
,executorService).forPath("/test", "test back".getBytes());
//00:28:38.621 [pool-1-thread-2] WARN test.ygy.curator.Cruator_BackGround - event = CuratorEventImpl{type=CREATE, resultCode=-110, path='/test', name='null', children=null, context=null, stat=null, data=null, watchedEvent=null, aclList=null}
//00:28:38.622 [pool-1-thread-2] WARN test.ygy.curator.Cruator_BackGround - execcutors threadName = pool-1-thread-2
//不使用线程池,默认使用main-EventThread ,多个事件默认时,是严格按照顺序执行。
cf.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.inBackground(
(x, y) -> { //x ,y : CuratorFramework client 客户端 CuratorEvent event事件
log.warn(" event = " + y);
log.warn(" execcutors threadName = " + Thread.currentThread().getName());
countDownLatch.countDown();
}
).forPath("/test", "test back".getBytes());
//00:28:38.622 [main-EventThread] WARN test.ygy.curator.Cruator_BackGround - event = CuratorEventImpl{type=CREATE, resultCode=-110, path='/test', name='null', children=null, context=null, stat=null, data=null, watchedEvent=null, aclList=null}
//00:28:38.622 [main-EventThread] WARN test.ygy.curator.Cruator_BackGround - execcutors threadName = main-EventThread
cf.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.inBackground(
(x, y) -> { //x ,y : CuratorFramework client 客户端 CuratorEvent event事件
log.warn(" event = " + y);
log.warn(" execcutors threadName = " + Thread.currentThread().getName());
countDownLatch.countDown();
}
).forPath("/test", "test back".getBytes());
//00:28:38.623 [main-EventThread] WARN test.ygy.curator.Cruator_BackGround - event = CuratorEventImpl{type=CREATE, resultCode=-110, path='/test', name='null', children=null, context=null, stat=null, data=null, watchedEvent=null, aclList=null}
//00:28:38.623 [main-EventThread] WARN test.ygy.curator.Cruator_BackGround - execcutors threadName = main-EventThread
countDownLatch.await();
Thread.sleep(10000);
executorService.shutdown();
cf.close();
}
}
Curator_NodeCacheListener
package test.ygy.curator
import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.recipes.cache.NodeCache
import org.apache.zookeeper.CreateMode
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.concurrent.CountDownLatch
public class Curator_NodeCacheListener {
private static final Logger log=LoggerFactory.getLogger(Curator_NodeCacheListener.class)
public static void main(String[] args) throws Exception {
CountDownLatch countDownLatch=new CountDownLatch(1)
CuratorFramework cf=Curator_Start.getYGYBuilder().build()
cf.start()
log.warn(" zookeeper 启动成功")
//创建一个节点
cf.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/test", "init".getBytes())
//nodeCache client :客户端 path : 节点路径 dataIsCompressed : 是否压缩
NodeCache nodeCache=new NodeCache(cf, "/test", false)
nodeCache.start(true)
// nodeCache 可以监听指定节点数据变化,与节点是否存在
nodeCache.getListenable().addListener(
()->{
log.warn("data changed to " + new String(nodeCache.getCurrentData().getData()))
countDownLatch.countDown()
}
)
//设置新值,
cf.setData().forPath("/test", " new data ".getBytes())
countDownLatch.await()
cf.delete().forPath("/test")
Thread.sleep(20000)
cf.close()
}
}
Curator_PathChildrenListener
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by guoyao on 2017/8/29.
*/
public class Curator_PathChildrenListener {
private static final Logger log=LoggerFactory.getLogger(Curator_PathChildrenListener.class);
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getYGYBuilder().build();
cf.start();
log.warn(" zookeepeer 启动成功");
/**
* @param client the client 客服端
* @param path path to watch 节点路径
* @param cacheData if true, node contents are cached in addition to the stat 是否需要获取节点数据
* @param dataIsCompressed if true, data in the path is compressed 是否压缩
* @param executorService Closeable ExecutorService to use for the PathChildrenCache's background thread
*/
PathChildrenCache pathChildrenCache=new PathChildrenCache(
cf,
"/test",
true
);
pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
pathChildrenCache.getListenable().addListener(
new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case INITIALIZED:
log.warn(" INITIALIZED 初始化");
break;
case CHILD_ADDED:
log.warn(" CHILD_ADDED 新增子节点 " + event.getData());
break;
case CHILD_REMOVED:
log.warn(" CHILD_REMOVED 移除子节点" + event.getData());
break;
case CHILD_UPDATED:
log.warn(" CHILD_UPDATED 修改子节点数据" + event.getData());
break;
case CONNECTION_LOST:
log.warn(" CONNECTION_LOST 确认失去连接");
break;
case CONNECTION_SUSPENDED:
log.warn(" CONNECTION_SUSPENDED 连接挂起,可能失去连接");
break;
case CONNECTION_RECONNECTED:
log.warn(" CONNECTION_RECONNECTED 重新获取连接");
break;
default:
break;
}
}
}
);
Thread.sleep(2000);
cf.create().withMode(CreateMode.EPHEMERAL).forPath("/test/child1", "test child1".getBytes());
Thread.sleep(2000);
cf.create().withMode(CreateMode.EPHEMERAL).forPath("/test/child2", "test child2".getBytes());
Thread.sleep(2000);
cf.create().withMode(CreateMode.EPHEMERAL).forPath("/test/child3", "test child3".getBytes());
Thread.sleep(2000);
cf.setData().forPath("/test/child2", "new child2 data".getBytes());
Thread.sleep(2000);
cf.delete().forPath("/test/child3");
Thread.sleep(20000);
cf.close();
}
}
Cruator_LeaderSelector
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.apache.curator.utils.EnsurePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by guoyao on 2017/8/28.
*/
public class Cruator_LeaderSelector {
private static final Logger log=LoggerFactory.getLogger(Cruator_LeaderSelector.class);
private static final String MASTER_PATH="/curator-master";
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getBaseBuilder().build();
cf.start();
log.warn(" zookeeper 启动成功");
new EnsurePath(MASTER_PATH+"/AAAA").ensure(cf.getZookeeperClient());
LeaderSelector leaderSelector = new LeaderSelector(
cf
, MASTER_PATH
, new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
log.warn(" i am master ");
Thread.sleep(3000);
log.warn(" off master ");
}
});
leaderSelector.autoRequeue();
leaderSelector.start();
Thread.sleep(200000000);
cf.close();
}
}
Curator_Atomic
package test.ygy.curator
import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.recipes.atomic.AtomicValue
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger
import org.apache.curator.retry.RetryNTimes
import org.slf4j.Logger
import org.slf4j.LoggerFactory
public class Curator_Atomic {
private static final Logger log=LoggerFactory.getLogger(Curator_Atomic.class)
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getBaseBuilder().build()
cf.start()
log.warn(" zookeeper 启动成功")
DistributedAtomicInteger distributedAtomicInteger=
new DistributedAtomicInteger(cf, "/test/atomic", new RetryNTimes(3, 2000))
AtomicValue<Integer> increment1=distributedAtomicInteger.increment()
log.warn(" atomic post inc1 = " + increment1.postValue())
log.warn(" atomic pre inc1 = " + increment1.preValue())
//22:32:31.193 [main] WARN test.ygy.curator.Curator_Atomic - atomic post inc1 = 1
//22:32:31.193 [main] WARN test.ygy.curator.Curator_Atomic - atomic pre inc1 = 0
AtomicValue<Integer> increment2=distributedAtomicInteger.increment()
log.warn(" atomic post inc2 = " + increment2.postValue())
log.warn(" atomic pre inc2 = " + increment2.preValue())
//22:32:31.212 [main] WARN test.ygy.curator.Curator_Atomic - atomic post inc2 = 2
//22:32:31.212 [main] WARN test.ygy.curator.Curator_Atomic - atomic pre inc2 = 1
Thread.sleep(20000)
cf.close()
}
}
Cruator_InterLock
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by guoyao on 2017/8/28.
*/
public class Cruator_InterLock {
private static final Logger log=LoggerFactory.getLogger(Cruator_InterLock.class);
private static int index = 0 ;
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getBaseBuilder().build();
cf.start();
log.warn(" zookeeper 启动成功");
ExecutorService executorService=Executors.newFixedThreadPool(100);
CountDownLatch countDownLatch=new CountDownLatch(100);
InterProcessMutex interProcessMutex=new InterProcessMutex(cf,"/test/lock");
for (int i=0; i < 100; i++) {
executorService.execute(()->{
try {
Thread.sleep(1000);
interProcessMutex.acquire();
index++;
interProcessMutex.release();
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
countDownLatch.countDown();
}
});
}
executorService.shutdown();
countDownLatch.await();
log.warn(" index add 100 times = " + index);
}
}
Curator_Barrier
package test.ygy.curator
import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.api.CuratorWatcher
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger
import org.apache.curator.framework.recipes.barriers.DistributedBarrier
import org.apache.curator.retry.RetryNTimes
import org.apache.zookeeper.WatchedEvent
import org.apache.zookeeper.data.Stat
import org.slf4j.Logger
import org.slf4j.LoggerFactory
public class Curator_Barrier {
private static final Logger log=LoggerFactory.getLogger(Curator_Barrier.class)
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getBaseBuilder().build()
cf.start()
log.warn(" zookeeper 启动成功")
DistributedBarrier distributedBarrier = new DistributedBarrier(cf,"/test/barries")
DistributedAtomicInteger distributedAtomicInteger=
new DistributedAtomicInteger(cf, "/test/ato", new RetryNTimes(3, 2000))
//cf.create().creatingParentsIfNeeded().forPath("/test/barries")
Stat stat1=cf.checkExists().usingWatcher(new CuratorWatcher() {
@Override
public void process(WatchedEvent event) throws Exception {
}
}).forPath("/test/barries")
log.warn(" check stat1 = " + stat1)
for(int i = 0
new Thread(
()->{
try {
distributedBarrier.setBarrier()
Thread.sleep(1000)
log.warn(Thread.currentThread().getName() + " is waiting")
distributedAtomicInteger.increment()
distributedBarrier.waitOnBarrier()
//do work
Thread.sleep(2000)
log.warn(Thread.currentThread().getName() + " is running")
} catch (Exception e) {
e.printStackTrace()
}
}
," thread -- " + i).start()
}
int index = 0
while (distributedAtomicInteger.get().postValue() < 10) {
//等待所有任务进入
if (index < distributedAtomicInteger.get().postValue()) {
log.warn(" 当前已备注数为 " + distributedAtomicInteger.get().postValue())
index = distributedAtomicInteger.get().postValue()
}
}
Stat stat2=cf.checkExists().usingWatcher(new CuratorWatcher() {
@Override
public void process(WatchedEvent event) throws Exception {
}
}).forPath("/test/barries")
log.warn(" check stat2 = " + stat2)
distributedBarrier.removeBarrier()
Stat stat3=cf.checkExists().usingWatcher(new CuratorWatcher() {
@Override
public void process(WatchedEvent event) throws Exception {
}
}).forPath("/test/barries")
log.warn(" check stat3 = " + stat3)
Thread.sleep(100000)
cf.close()
}
}
Curator_Barray_Double
package test.ygy.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by guoyao on 2017/8/30.
*/
public class Curator_Barray_Double {
private static final Logger log=LoggerFactory.getLogger(Curator_Barray_Double.class);
public static void main(String[] args) throws Exception {
CuratorFramework cf=Curator_Start.getYGYBuilder().build();
cf.start();
log.warn(" zookeeper 启动成功");
for(int i = 0 ; i < 10 ; i ++) {
new Thread(
()->{
try {
log.warn(Thread.currentThread().getName() + " is waiting");
DistributedDoubleBarrier distributedDoubleBarrier = new DistributedDoubleBarrier(cf,"/test/barries",5);
distributedDoubleBarrier.enter();
Thread.sleep(1000);
log.warn(Thread.currentThread().getName() + " is working");
distributedDoubleBarrier.leave();
log.warn(Thread.currentThread().getName() + " is out");
} catch (Exception e) {
e.printStackTrace();
}
}
," thread -- " + i).start();
}
Thread.sleep(100000);
cf.close();
}
}