}
private void zk() throws Exception {
//任意位置的SharedCount,只要使用相同的path,都可以得到这个计数值。
String path = “/path/count”;
CuratorFramework client = initClient(path);
//分布式集群中存在的某一处计数。
SharedCount baseCount = new SharedCount(client, path, 0);
baseCount.start();
//baseCount.setCount(0);
baseCount.addListener(new SharedCountListener() {
@Override
public void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception {
System.out.println(“countHasChanged最新=” + newCount);
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
//System.out.println(TAG + “状态变化:” + client.toString());
}
});
List sharedCountList = new ArrayList();
//开启COUNT个线程,模拟对分布式中SharedCount的不同节点的赋值操作。
ExecutorService service = Executors.newFixedThreadPool(COUNT);
for (int i = 0; i < COUNT; i++) {
SharedCount count = new SharedCount(client, path, 0);
count.start();
sharedCountList.add(count);
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep((long) (Math.random() * TIME));
setValue(count);
} catch (Exception e) {
e.printStackTrace();
}
}
};
service.submit(runnable);
}
service.shutdown();
service.awaitTermination(TIME, TimeUnit.SECONDS);
//使用完成后要记得关闭。
for (SharedCount c : sharedCountList) {
c.close();
}
baseCount.close();
}
private void setValue(SharedCount count) throws Exception {
System.out.println(“-------------”);
int oldCount = count.getCount();
System.out.println(“当前值=” + oldCount);
System.out.println(“当前VersionedValue version=” + count.getVersionedValue().getVersion());
System.out.println(“当前VersionedValue value=” + count.getVersionedValue().getValue());
int n = (int) (Math.random() * 1000);
System.out.println(“尝试赋值:” + n);
/**
-
trySetCount设置计数器和setCount不同,setCount强制更新计数器的值。
-
trySetCount第一个参数提供当前的VersionedValue,如果期间其它client更新了计数值,
-
你的更新可能不成功,但是这时你的client更新了最新的值,
-
若失败可再尝试更新。
-
只有当此client最后读取的值与zookeeper中的值相等时,才能更新.
-
调用此方法,不成功时,会刷新本地缓存值.
-
之后调用getCount()就是新值.
*/
//boolean b = count.trySetCount(count.getVersionedValue(), n);
//System.out.println(n + “更新?” + b);
//或者使用setCount强制更新计数器的值。
//此方法强制更新计数器值。
count.setCount(n);
}
private CuratorFramework initClient(String path) throws Exception {
CuratorFramework client = makeClient();
client.start();
boolean b = isPathExist(client, path);
//如果不存在这个路径,stat为null,创建新的节点路径。
if (!b) {
String s = client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(path);
System.out.println("创建 " + s);
} else {
System.out.println(“已存在:” + path + “,不需重复创建”);
}
return client;
}
//检测是否存在该路径。
private boolean isPathExist(CuratorFramework client, String path) {
boolean b = false;
//检测是否存在该路径。
try {
Stat stat = client.checkExists().forPath(path);
b = stat == null ? false : true;
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
private CuratorFramework makeClient() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(getAddress())
.sessionTimeoutMs(10 * 1000)
.connectionTimeoutMs(20 * 1000)
.retryPolicy(retryPolicy)
.build();
return client;
}
private String getAddress() {
String ip = “127.0.0.1”;
return ip + “:2181,” + ip + “:2182,” + ip + “:2183”;
}
}
输出:
最后
最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2020-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。
还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,这里我也免费分享给大家也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
一起互勉~
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
hnsalB3k-1714520574174)]
[外链图片转存中…(img-Nfm1inR3-1714520574175)]
一起互勉~
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!