时间戳: 41位 接近 70年的毫秒数据
序列号: 某一个机器 可以在某一个毫秒可以产生4000多个值(4000多个唯一id)
public class SnowFlake {
//下面两个每个5位,加起来就是10位的工作机器id
private long workerId; //工作id
private long datacenterId; //数据id
//12位的序列号
private long sequence;
/**
* @param workerId 机器id
* @param datacenterId 集群id
* datacenterId + workerId 一起定义 机器id
* @param sequence
*/
public SnowFlake(long workerId, long datacenterId, long sequence) {
// sanity check for workerId
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
System.out.printf