public final class SnowflakeId18 extends Object
特性: 1.支持自定义允许时间回拨的范围
2.解决跨毫秒起始值每次为0开始的情况(避免末尾必定为偶数,而不便于取余使用问题)
3.解决高并发场景中获取时间戳性能问题
4.支撑根据IP末尾数据作为workerId 5.时间回拨方案思考:1024个节点中分配10个点作为时间回拨序号(连续10次时间回拨的概率较小)
| 构造器和说明 |
|---|
SnowflakeId18(long dataCenterId) |
SnowflakeId18(long dataCenterId,
boolean clock,
boolean randomSequence) |
SnowflakeId18(long dataCenterId,
long workerId,
boolean clock,
long timeOffset,
boolean randomSequence)
基于Snowflake创建分布式ID生成器
|
public SnowflakeId18(long dataCenterId)
public SnowflakeId18(long dataCenterId,
boolean clock,
boolean randomSequence)
public SnowflakeId18(long dataCenterId,
long workerId,
boolean clock,
long timeOffset,
boolean randomSequence)
dataCenterId - 数据中心ID,数据范围为0~255workerId - 工作机器ID,数据范围为0~3clock - true表示解决高并发下获取时间戳的性能问题timeOffset - 允许时间回拨的毫秒量,建议5msrandomSequence - true表示使用毫秒内的随机序列(超过范围则取余)public Long nextId()
public static byte getLastIPAddress()
eg:192.168.1.30->30
Copyright © 2021. All rights reserved.