[monitor] 支持网站监控类型,降低雪花算法UUID最大值解决ts json解析大数问题

This commit is contained in:
tomsun28
2021-12-03 13:41:17 +08:00
parent b632114ebd
commit 7a8e6d41cd
10 changed files with 138 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ public class SnowFlakeIdGenerator {
private final static SnowFlakeIdWorker ID_WORKER;
static {
ID_WORKER = new SnowFlakeIdWorker(1, 0);
ID_WORKER = new SnowFlakeIdWorker(0);
}
public static long generateId() {

View File

@@ -2,6 +2,7 @@ package com.usthe.common.util;
/**
* 雪花算法生成器实例
* 注意 由于前端JS TS 在json解析大数会造成精度丢失 UUID 不能超过 900719925474099116位
* @author from https://www.cnblogs.com/vchar/p/14857677.html
* @date 2021/11/10 10:58
*/
@@ -15,12 +16,12 @@ public class SnowFlakeIdWorker {
/**
* 机器 ID 所占的位数
*/
private static final long WORKER_ID_BITS = 5L;
private static final long WORKER_ID_BITS = 2L;
/**
* 数据标识 ID 所占的位数
*/
private static final long DATA_CENTER_ID_BITS = 5L;
private static final long DATA_CENTER_ID_BITS = 4L;
/**
* 支持的最大机器ID最大为31

View File

@@ -0,0 +1,22 @@
package com.usthe.common.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author tom
* @date 2021/12/3 13:28
*/
class SnowFlakeIdGeneratorTest {
@Test
void generateId() {
// 注意 由于前端JS TS 在json解析大数会造成精度丢失 UUID 不能超过 900719925474099116位
for (int i = 0; i < 1000; i++) {
long id = SnowFlakeIdGenerator.generateId();
Assertions.assertTrue(id < 9007199254740991L);
}
}
}