Skip to content

Commit 3b8933f

Browse files
committed
分布式ID雪花算法更新
1 parent 1110914 commit 3b8933f

File tree

3 files changed

+386
-17
lines changed

3 files changed

+386
-17
lines changed

SpringBoot/DistributedID/src/main/java/com/example/config/DistributedIdConfig.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.hutool.core.util.RandomUtil;
44
import com.example.snow.generator.IdWorkerUpdate;
5+
import org.springframework.beans.factory.annotation.Value;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78

@@ -14,8 +15,11 @@
1415
@Configuration
1516
public class DistributedIdConfig {
1617

18+
@Value("${workId:0}")
19+
private long workerId;
20+
1721
/**
18-
* 启动随机生成数据中心ID和WordID,每次重新启动应用尾号段进行刷新
22+
* 启动创建
1923
*
2024
* @param
2125
* @return com.pcic.app.generator.IdWorker
@@ -24,8 +28,11 @@ public class DistributedIdConfig {
2428
* @date 2021/9/2 16:44
2529
*/
2630
@Bean
27-
public IdWorkerUpdate idWorkerPatch() {
28-
return new IdWorkerUpdate(RandomUtil.randomLong(0, 15), RandomUtil.randomLong(0, 15), 0L);
31+
public IdWorkerUpdate idWorkerUpdate() {
32+
// 启动随机生成工作机器ID
33+
// return new IdWorkerUpdate(RandomUtil.randomLong(0, 31), 0L);
34+
// 启动读取配置工作机器ID
35+
return new IdWorkerUpdate(workerId, 0L);
2936
}
3037

3138
}
Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
package com.example.snow;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.lang.management.ManagementFactory;
7+
import java.net.InetAddress;
8+
import java.net.NetworkInterface;
9+
import java.util.Enumeration;
10+
import java.util.regex.Pattern;
11+
12+
/**
13+
* 改良版-Twitter的SnowFlake算法<br>
14+
*
15+
* 由于跨毫秒后,最后的Sequence累加就会清零,末位为偶数
16+
* 如果ID生成不频繁,则生成的就是全是偶数
17+
* 改良版雪花算法,解决全为偶数问题,保证低并发时奇偶交替
18+
*
19+
* 时钟回拨问题直接抛出异常,过于简单
20+
* 优化如果时间偏差大小小于5ms,则等待两倍时间重试一次,加强可用性
21+
*
22+
* 根据网络MAC和IP地址及JvmID取余自动设定workerId及datacenterId
23+
* https://gitee.com/yu120/sequence
24+
*
25+
* SnowFlake的结构如下(每部分用-分开):<br>
26+
* 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br>
27+
* 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br>
28+
* 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截)
29+
* 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)
30+
* 41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br>
31+
* 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br>
32+
* 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br>
33+
* 加起来刚好64位,为一个Long型<br>
34+
* SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高
35+
* 经测试,SnowFlake每秒能够产生26万ID左右
36+
*
37+
* @author wliduo[i@dolyw.com]
38+
* @date 2021/1/15 11:31
39+
*/
40+
public class IdWorkerPatch2 {
41+
42+
/**
43+
* logger
44+
*/
45+
private static final Logger logger = LoggerFactory.getLogger(IdWorkerPatch2.class);
46+
47+
/**
48+
* 工作机器ID(0~31)
49+
*/
50+
private long workerId;
51+
52+
/**
53+
* 数据中心ID(0~31)
54+
*/
55+
private long datacenterId;
56+
57+
/**
58+
* 毫秒内序列(0~4095)
59+
*/
60+
private long sequence;
61+
62+
private static volatile InetAddress LOCAL_ADDRESS = null;
63+
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
64+
65+
/**
66+
* 构造函数
67+
*/
68+
public IdWorkerPatch2() {
69+
this.datacenterId = getDatacenterId();
70+
this.workerId = getMaxWorkerId(datacenterId);
71+
}
72+
73+
/**
74+
* 基于网卡MAC地址计算余数作为数据中心
75+
* <p>
76+
* 可自定扩展
77+
*/
78+
protected long getDatacenterId() {
79+
long id = 0L;
80+
try {
81+
NetworkInterface network = NetworkInterface.getByInetAddress(getLocalAddress());
82+
if (null == network) {
83+
id = 1L;
84+
} else {
85+
byte[] mac = network.getHardwareAddress();
86+
if (null != mac) {
87+
id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
88+
id = id % (maxDatacenterId + 1);
89+
}
90+
}
91+
} catch (Exception e) {
92+
logger.warn(" getDatacenterId: " + e.getMessage());
93+
}
94+
95+
return id;
96+
}
97+
98+
/**
99+
* 基于 MAC + PID 的 hashcode 获取16个低位
100+
* <p>
101+
* 可自定扩展
102+
*/
103+
protected long getMaxWorkerId(long datacenterId) {
104+
StringBuilder mpId = new StringBuilder();
105+
mpId.append(datacenterId);
106+
String name = ManagementFactory.getRuntimeMXBean().getName();
107+
if (name != null && name.length() > 0) {
108+
// GET jvmPid
109+
mpId.append(name.split("@")[0]);
110+
}
111+
112+
// MAC + PID 的 hashcode 获取16个低位
113+
return (mpId.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
114+
}
115+
116+
/**
117+
* 开始时间截
118+
*/
119+
private long twepoch = 1288834974657L;
120+
121+
/**
122+
* 机器ID所占的位数
123+
*/
124+
private long workerIdBits = 5L;
125+
126+
/**
127+
* 数据标识ID所占的位数
128+
*/
129+
private long datacenterIdBits = 5L;
130+
131+
/**
132+
* 支持的最大机器ID,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
133+
*/
134+
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
135+
136+
/**
137+
* 支持的最大数据标识ID,结果是31
138+
*/
139+
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
140+
141+
/**
142+
* 序列在ID中占的位数
143+
*/
144+
private long sequenceBits = 12L;
145+
146+
/**
147+
* 机器ID向左移12位
148+
*/
149+
private long workerIdShift = sequenceBits;
150+
151+
/**
152+
* 数据标识ID向左移17位(12+5)
153+
*/
154+
private long datacenterIdShift = sequenceBits + workerIdBits;
155+
156+
/**
157+
* 时间截向左移22位(5+5+12)
158+
*/
159+
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
160+
161+
/**
162+
* 生成序列的掩码,这里为4095(0b111111111111=0xfff=4095)
163+
*/
164+
private long sequenceMask = -1L ^ (-1L << sequenceBits);
165+
166+
/**
167+
* 上次生成ID的时间截
168+
*/
169+
private long lastTimestamp = -1L;
170+
171+
/**
172+
* 上一次的序列号,解决并发量小总是偶数的问题
173+
*/
174+
private long lastSequence = 0L;
175+
176+
/**
177+
* 获得下一个ID (该方法是线程安全的)
178+
*
179+
* @return SnowflakeId
180+
*/
181+
public synchronized long nextId() {
182+
long timestamp = timeGen();
183+
184+
// 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
185+
if (timestamp < lastTimestamp) {
186+
long offset = lastTimestamp - timestamp;
187+
if (offset <= 5) {
188+
try {
189+
// 时间偏差大小小于5ms,则等待两倍时间重试一次
190+
wait(offset << 1);
191+
timestamp = timeGen();
192+
if (timestamp < lastTimestamp) {
193+
// 还是小于,抛异常
194+
logger.error("clock is moving backwards. Rejecting requests until {}.", lastTimestamp);
195+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
196+
lastTimestamp - timestamp));
197+
}
198+
} catch (InterruptedException e) {
199+
logger.error("clock is moving backwards. Rejecting requests until {}.", lastTimestamp);
200+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
201+
lastTimestamp - timestamp));
202+
}
203+
} else {
204+
logger.error("clock is moving backwards. Rejecting requests until {}.", lastTimestamp);
205+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
206+
lastTimestamp - timestamp));
207+
}
208+
}
209+
210+
// 如果是同一时间生成的,则进行毫秒内序列
211+
if (lastTimestamp == timestamp) {
212+
sequence = (sequence + 1L) & sequenceMask;
213+
// 毫秒内序列溢出
214+
if (sequence == 0L) {
215+
// 阻塞到下一个毫秒,获得新的时间戳
216+
timestamp = tilNextMillis(lastTimestamp);
217+
}
218+
} else {
219+
// 时间戳改变,毫秒内序列重置
220+
sequence = 0L;
221+
// 根据上一次Sequence决定本次序列从0还是1开始,保证低并发时奇偶交替
222+
if (lastSequence == 0L) {
223+
sequence = 1L;
224+
}
225+
}
226+
227+
// 上次的序列号
228+
lastSequence = sequence;
229+
// 上次生成ID的时间截
230+
lastTimestamp = timestamp;
231+
232+
// 移位并通过或运算拼到一起组成64位的ID
233+
return ((timestamp - twepoch) << timestampLeftShift) |
234+
(datacenterId << datacenterIdShift) |
235+
(workerId << workerIdShift) |
236+
sequence;
237+
}
238+
239+
/**
240+
* 阻塞到下一个毫秒,直到获得新的时间戳
241+
*
242+
* @param lastTimestamp 上次生成ID的时间截
243+
* @return 当前时间戳
244+
*/
245+
private long tilNextMillis(long lastTimestamp) {
246+
long timestamp = timeGen();
247+
while (timestamp <= lastTimestamp) {
248+
timestamp = timeGen();
249+
}
250+
return timestamp;
251+
}
252+
253+
/**
254+
* Find first valid IP from local network card
255+
*
256+
* @return first valid local IP
257+
*/
258+
public static InetAddress getLocalAddress() {
259+
if (LOCAL_ADDRESS != null) {
260+
return LOCAL_ADDRESS;
261+
}
262+
263+
LOCAL_ADDRESS = getLocalAddress0();
264+
return LOCAL_ADDRESS;
265+
}
266+
267+
private static InetAddress getLocalAddress0() {
268+
InetAddress localAddress = null;
269+
try {
270+
localAddress = InetAddress.getLocalHost();
271+
if (isValidAddress(localAddress)) {
272+
return localAddress;
273+
}
274+
} catch (Throwable e) {
275+
logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
276+
}
277+
278+
try {
279+
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
280+
if (interfaces != null) {
281+
while (interfaces.hasMoreElements()) {
282+
try {
283+
NetworkInterface network = interfaces.nextElement();
284+
Enumeration<InetAddress> addresses = network.getInetAddresses();
285+
while (addresses.hasMoreElements()) {
286+
try {
287+
InetAddress address = addresses.nextElement();
288+
if (isValidAddress(address)) {
289+
return address;
290+
}
291+
} catch (Throwable e) {
292+
logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
293+
}
294+
}
295+
} catch (Throwable e) {
296+
logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
297+
}
298+
}
299+
}
300+
} catch (Throwable e) {
301+
logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
302+
}
303+
304+
logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
305+
return localAddress;
306+
}
307+
308+
private static boolean isValidAddress(InetAddress address) {
309+
if (address == null || address.isLoopbackAddress()) {
310+
return false;
311+
}
312+
313+
String name = address.getHostAddress();
314+
return (name != null && !"0.0.0.0".equals(name) && !"127.0.0.1".equals(name) && IP_PATTERN.matcher(name).matches());
315+
}
316+
317+
/**
318+
* 返回以毫秒为单位的当前时间
319+
*
320+
* @return 当前时间(毫秒)
321+
*/
322+
private long timeGen() {
323+
return System.currentTimeMillis();
324+
}
325+
326+
/**
327+
* 由于跨毫秒后,最后的Sequence累加就会清零,末位为偶数
328+
* 如果ID生成不频繁,则生成的就是全是偶数
329+
* 改良版雪花算法,解决全为偶数问题,保证低并发时奇偶交替
330+
*
331+
* @param args
332+
* @throws Exception
333+
*/
334+
public static void main(String[] args) throws Exception {
335+
IdWorkerPatch2 idWorkerPatch = new IdWorkerPatch2();
336+
for (int i = 0; i < 10; i++) {
337+
Thread.sleep(1000L);
338+
logger.info("{}", idWorkerPatch.nextId());
339+
}
340+
}
341+
342+
}

0 commit comments

Comments
 (0)