Zookeeper全局唯一ID生成方案解析
作者:codedot
这篇文章主要介绍了Zookeeper全局唯一ID生成方案解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
系统唯一ID生成分案有很多种,例如:数据库 auto_increment,UUID,Redis生成ID(Redis原子操作INCR和INCRBY),Twiitter的snowflake算法,ZooKeeper生成ID,MongoDb的ObjectId,下面我们就看一下ZooKeeper实现分布式系统唯一ID。
public int idGen() throws Exception {
String zkAddress = "127.0.0.1:2181";
String idNode = "/id";
//重试策略
RetryPolicy retry = new RetryNTimes(3, 2000);
//创建连接客户端
CuratorFramework client = CuratorFrameworkFactory.builder().
connectString(zkAddress).
sessionTimeoutMs(5000).
connectionTimeoutMs(10000).
retryPolicy(retry).
build();
//启动客户端
client.start();
if (null == client.checkExists().forPath(idNode)) {
client.create().withMode(CreateMode.PERSISTENT)
.forPath(idNode);
}
Stat stat = client.setData().withVersion(-1).forPath(idNode);
return stat.getVersion();
}
注意:换了ZooKeeper,数据就要从0开始,还没有直接可以修改指定数字那里开始,只能写程序一点点的创建,直到达到你要想的数据 ,这是它最大的弊端。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- SpringCloud用Zookeeper搭建配置中心的方法
- springcloud集成zookeeper的方法示例
- SpingBoot+Dubbo+Zookeeper实现简单分布式开发的应用详解
- 关于idea+centos7+zookeeper报错connectionloss,timeout问题
- 如何用python 操作zookeeper
- SpringBoot中dubbo+zookeeper实现分布式开发的应用详解
- 基于Spring Cloud Zookeeper实现服务注册与发现
- 2020最新IDEA SpringBoot整合Dubbo的实现(zookeeper版)
- SpringBoot系列教程之dubbo和Zookeeper集成方法
- Python通过zookeeper实现分布式服务代码解析
- 在Java中操作Zookeeper的示例代码详解
- SpringCloud使用Zookeeper作为注册中心
