记得上下班打卡 | git大法好,push需谨慎

Commit f3973380 authored by 张国柄's avatar 张国柄

添加ID生成器IDGenSnow;

parent 65b18e18
package com.liquidnet.commons.lang.util;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class IDGenSnow {
private static final long twepoch = 1605456000000L;
private static final long workerIdBits = 5L;
private static final long dataCenterIdBits = 5L;
//// 最大支持机器节点数0~31,一共32个
// 最大支持数据中心节点数0~31,一共32个
@SuppressWarnings({"PointlessBitwiseExpression", "FieldCanBeLocal"})
private static final long maxWorkerId = -1L ^ (-1L << workerIdBits);
@SuppressWarnings({"PointlessBitwiseExpression", "FieldCanBeLocal"})
private static final long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits);
// 序列号12位
private static final long sequenceBits = 12L;
// 机器节点左移12位
private static final long workerIdShift = sequenceBits;
// 数据中心节点左移17位
private static final long dataCenterIdShift = sequenceBits + workerIdBits;
// 时间毫秒数左移22位
private static final long timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
@SuppressWarnings({"PointlessBitwiseExpression", "FieldCanBeLocal"})
private static final long sequenceMask = -1L ^ (-1L << sequenceBits);// 4095
private static final long workerId;
private static final long dataCenterId;
private static final boolean useSystemClock;
private static long sequence = 0L;
private static long lastTimestamp = -1L;
static {
workerId = getMachineNum() & maxWorkerId;
dataCenterId = 1;
useSystemClock = true;
}
/* ---------------------------------------------------------------------------------------- */
public static synchronized Long nextId() {
long timestamp = genTime();
if (timestamp < lastTimestamp) {
if (lastTimestamp - timestamp < 2000) {
// 容忍2秒内的回拨,避免NTP校时造成的异常
timestamp = lastTimestamp;
} else {
// 如果服务器时间有问题(时钟后退) 报错。
throw new IllegalStateException(String.format("Clock moved backwards. Refusing to generate id for %sms", lastTimestamp - timestamp));
}
}
if (timestamp == lastTimestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence;
}
private static long genTime() {
return useSystemClock ? SystemClock.now() : System.currentTimeMillis();
}
private static long tilNextMillis(long lastTimestamp) {
long timestamp = genTime();
// 循环直到操作系统时间戳变化
while (timestamp == lastTimestamp) {
timestamp = genTime();
}
if (timestamp < lastTimestamp) {
// 如果发现新的时间戳比上次记录的时间戳数值小,说明操作系统时间发生了倒退,报错
throw new IllegalStateException(
String.format("Clock moved backwards. Refusing to generate id for %sms", lastTimestamp - timestamp));
}
return timestamp;
}
private static long getMachineNum(){
long machinePiece;
StringBuilder sb = new StringBuilder();
Enumeration<NetworkInterface> e = null;
try {
e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
sb.append(ni.toString());
}
} catch (SocketException e1) {
e1.printStackTrace();
}
machinePiece = sb.toString().hashCode();
System.out.printf("\n###MachineNum=%s, MachineCodeString=%s\n\n", machinePiece, sb);
return machinePiece;
}
/* ---------------------------------------------------------------------------------------- */
public long getWorkerId(long id) {
return id >> workerIdShift & ~(-1L << workerIdBits);
}
public long getDataCenterId(long id) {
return id >> dataCenterIdShift & ~(-1L << dataCenterIdBits);
}
public long getGenerateDateTime(long id) {
return (id >> timestampLeftShift & ~(-1L << 41L)) + twepoch;
}
/* ---------------------------------------------------------------------------------------- */
}
package com.liquidnet.commons.lang.util;
import java.sql.Timestamp;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SystemClock {
/** 时钟更新间隔,单位毫秒 */
private final long period;
/** 现在时刻的毫秒数 */
private volatile long now;
/**
* 构造
* @param period 时钟更新间隔,单位毫秒
*/
public SystemClock(long period) {
this.period = period;
this.now = System.currentTimeMillis();
scheduleClockUpdating();
}
/**
* 开启计时器线程
*/
private void scheduleClockUpdating() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true);
return thread;
});
scheduler.scheduleAtFixedRate(() -> now = System.currentTimeMillis(), period, period, TimeUnit.MILLISECONDS);
}
/**
* @return 当前时间毫秒数
*/
private long currentTimeMillis() {
return now;
}
//------------------------------------------------------------------------ static
/**
* 单例
* @author Looly
*
*/
private static class InstanceHolder {
public static final SystemClock INSTANCE = new SystemClock(1);
}
/**
* 单例实例
* @return 单例实例
*/
private static SystemClock instance() {
return InstanceHolder.INSTANCE;
}
/**
* @return 当前时间
*/
public static long now() {
return instance().currentTimeMillis();
}
/**
* @return 当前时间字符串表现形式
*/
public static String nowDate() {
return new Timestamp(instance().currentTimeMillis()).toString();
}
}
...@@ -25,11 +25,6 @@ ...@@ -25,11 +25,6 @@
<artifactId>liquidnet-common-cache-redis</artifactId> <artifactId>liquidnet-common-cache-redis</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.liquidnet</groupId>
<artifactId>liquidnet-common-cache-redisson</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId> <artifactId>spring-boot-starter-data-mongodb</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment