package com.rrjc.pf.common; | |
import java.net.InetAddress; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* (a) id构成: 42位的时间前缀 + 10位的节点标识 + 12位的sequence避免并发的数字(12位不够用时强制得到新的时间前缀) | |
* 注意这里进行了小改动: snowkflake是5位的datacenter加5位的机器id; 这里变成使用10位的机器id | |
* (b) 对系统时间的依赖性非常强,需关闭ntp的时间同步功能。当检测到ntp时间调整后,将会拒绝分配id | |
*/ | |
public class IdWorker { | |
private final static Logger logger = LoggerFactory.getLogger(IdWorker.class); | |
private final long workerId; | |
private final long epoch = 1403854494756L; // 时间起始标记点,作为基准,一般取系统的最近时间 | |
private final long workerIdBits = 10L; // 机器标识位数 | |
private final long maxWorkerId = -1L ^ -1L << this.workerIdBits;// 机器ID最大值: 1023 | |
private long sequence = 0L; // 0,并发控制 | |
private final long sequenceBits = 12L; //毫秒内自增位 | |
private final long workerIdShift = this.sequenceBits; // 12 | |
private final long timestampLeftShift = this.sequenceBits + this.workerIdBits;// 22 | |
private final long sequenceMask = -1L ^ -1L << this.sequenceBits; // 4095,111111111111,12位 | |
private long lastTimestamp = -1L; | |
private IdWorker(long workerId) { | |
if (workerId > this.maxWorkerId || workerId < 0) { | |
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", this.maxWorkerId)); | |
} | |
this.workerId = workerId; | |
} | |
public synchronized long nextId() throws Exception { | |
long timestamp = IdWorker.timeGen(); | |
if (this.lastTimestamp == timestamp) { // 如果上一个timestamp与新产生的相等,则sequence加一(0-4095循环); 对新的timestamp,sequence从0开始 | |
this.sequence = this.sequence + 1 & this.sequenceMask; | |
if (this.sequence == 0) { | |
timestamp = this.tilNextMillis(this.lastTimestamp);// 重新生成timestamp | |
} | |
} else { | |
this.sequence = 0; | |
} | |
if (timestamp < this.lastTimestamp) { | |
logger.error(String.format("clock moved backwards.Refusing to generate id for %d milliseconds", (this.lastTimestamp - timestamp))); | |
throw new Exception(String.format("clock moved backwards.Refusing to generate id for %d milliseconds", (this.lastTimestamp - timestamp))); | |
} | |
this.lastTimestamp = timestamp; | |
return timestamp - this.epoch << this.timestampLeftShift | this.workerId << this.workerIdShift | this.sequence; | |
} | |
private static IdWorker flowIdWorker = new IdWorker(getworkerHostIp()); | |
public static IdWorker getFlowIdWorkerInstance() { | |
return flowIdWorker; | |
} | |
/** | |
* 等待下一个毫秒的到来, 保证返回的毫秒数在参数lastTimestamp之后 | |
*/ | |
private long tilNextMillis(long lastTimestamp) { | |
long timestamp = IdWorker.timeGen(); | |
while (timestamp <= lastTimestamp) { | |
timestamp = IdWorker.timeGen(); | |
} | |
return timestamp; | |
} | |
/** | |
* 获得系统当前毫秒数 | |
*/ | |
private static long timeGen() { | |
return System.currentTimeMillis(); | |
} | |
/** | |
* | |
* @param bytes | |
* @return int | |
*/ | |
public static int getworkerHostIp() { | |
try { | |
byte[] bytes = InetAddress.getLocalHost().getAddress(); | |
return Integer.valueOf(bytes[3] & 0xFF); | |
} catch (Exception e) { | |
logger.error(e.getMessage(),e); | |
return 1; | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
IdWorker idWorker = IdWorker.getFlowIdWorkerInstance(); | |
System.out.println(idWorker.nextId()); | |
System.out.println(idWorker.nextId()); | |
} | |
}
源地址:https://gist.github.com/xishuixixia/f0f8684805d0504289b7a40f3b327dd6 |
- 浏览: 540066 次
最新评论
-
neusoft_jerry:
好东西,mark下!感谢楼主!
记一次线上Java程序导致服务器CPU占用率过高的问题排除过程(转载) -
460544406:
[color=red][/color]66
LINUX新建用户,并指定目录,无下载和上传权限(转载) -
soho00147:
结果错误,应该是A: {1,2,3,3,4,5}B: {3,4 ...
集合操作工具类CollectionUtils -
4711363:
能把修改的源码发我一下吗?碰到一些问题,谢谢。4711363@ ...
Redis存储Tomcat集群的Session出现race condition问题解决办法 -
CurrentJ:
直接用spring-session-redis组件。
Redis存储Tomcat集群的Session出现race condition问题解决办法
相关推荐
高并发分布式系统中生成全局唯一Id汇总 数据在分片时,典型的是分库分表,就有一个全局ID生成的问题。 单纯的生成全局ID并不是...IdWorker worker2 = new IdWorker(2); System.out.println(worker2.nextId()); } }
Algorithm-idworker.zip,idworker一、动物园管理员id、动物园管理员id,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
ID生成工具类(直接复制到项目里即可使用),简单快捷,只需在需要唯一ID时new IdWorker().nextId()即可,产生的ID不重复
idworker-基于zookeeper,snowflake的分布式统一ID生成工具 是什么 idworker是一个基于zookeeper和snowflake算法的分布式统一ID生成工具,通过zookeeper自动注册机器(最多1024台),无需手动指定workerId和...
项目中的代码截取自mybatis-plus-3.5项目源码中分布式ID的实现,主要是内容是Mybatis-plus项目中IdentifierGenerator接口的两个实现类DefaultIdentifierGenerator和ImadcnIdentifierGenerator的源码,还有IdWorker工具...
工具类CookieUtils、IdWorker、JsonUtils、NumberUtils Java工具类
* * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L ) / (1000L * 60 * 60 * 24 * ...
idworker-客户端idworker的客户
整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分)
雪花算法IdWorker.java
大数据一秒生成5000000不重复ID SnowflakeIDWorker 用到 long timestamp = timeGen(); timestamp 以及上一个 timestamp 加位移.
雪花算法代码
oracle自动生成主键的工具类,就一个简单的方法。
主要介绍了利用mysql实现的雪花算法案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截)得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的...