mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
增加IdConstants,提高Snowflake初始化性能
This commit is contained in:
parent
8e62f5ce44
commit
422f36b23e
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.28(2024-05-11)
|
||||
# 5.8.28(2024-05-13)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
||||
@ -19,6 +19,7 @@
|
||||
* 【core 】 PropertyComparator增加compareSelf构造重载(issue#3569@Github)
|
||||
* 【db 】 增加OceanBase的driver推断(pr#1217@Gitee)
|
||||
* 【http 】 HttpRequest#get不再尝试File路径(issue#I9O6DA@Gitee)
|
||||
* 【core 】 增加IdConstants,提高Snowflake初始化性能(issue#3581@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.date.SystemClock;
|
||||
import cn.hutool.core.lang.id.IdConstants;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@ -48,11 +49,11 @@ public class Snowflake implements Serializable {
|
||||
private static final long WORKER_ID_BITS = 5L;
|
||||
// 最大支持机器节点数0~31,一共32个
|
||||
@SuppressWarnings({"PointlessBitwiseExpression", "FieldCanBeLocal"})
|
||||
private static final long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS);
|
||||
public static final long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS);
|
||||
private static final long DATA_CENTER_ID_BITS = 5L;
|
||||
// 最大支持数据中心节点数0~31,一共32个
|
||||
@SuppressWarnings({"PointlessBitwiseExpression", "FieldCanBeLocal"})
|
||||
private static final long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS);
|
||||
public static final long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS);
|
||||
// 序列号12位(表示只允许workId的范围为:0-4095)
|
||||
private static final long SEQUENCE_BITS = 12L;
|
||||
// 机器节点左移12位
|
||||
@ -93,7 +94,7 @@ public class Snowflake implements Serializable {
|
||||
* 构造,使用自动生成的工作节点ID和数据中心ID
|
||||
*/
|
||||
public Snowflake() {
|
||||
this(IdUtil.getWorkerId(IdUtil.getDataCenterId(MAX_DATA_CENTER_ID), MAX_WORKER_ID));
|
||||
this(IdConstants.DEFAULT_WORKER_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,7 +103,7 @@ public class Snowflake implements Serializable {
|
||||
* @param workerId 终端ID
|
||||
*/
|
||||
public Snowflake(long workerId) {
|
||||
this(workerId, IdUtil.getDataCenterId(MAX_DATA_CENTER_ID));
|
||||
this(workerId, IdConstants.DEFAULT_DATACENTER_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cn.hutool.core.lang.id;
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
/**
|
||||
* ID相关常量
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.8.28
|
||||
*/
|
||||
public class IdConstants {
|
||||
/**
|
||||
* 默认的数据中心ID。
|
||||
* <p>此常量通过调用{@link IdUtil#getDataCenterId(long)}方法,传入{@link Snowflake#MAX_DATA_CENTER_ID}作为参数,
|
||||
* 来获取一个默认的数据中心ID。它在系统中作为一个全局配置使用,标识系统默认运行在一个最大数据中心ID限定的环境中。</p>
|
||||
*
|
||||
* @see IdUtil#getDataCenterId(long)
|
||||
* @see Snowflake#MAX_DATA_CENTER_ID
|
||||
*/
|
||||
public static final long DEFAULT_DATACENTER_ID = IdUtil.getDataCenterId(Snowflake.MAX_DATA_CENTER_ID);
|
||||
|
||||
/**
|
||||
* 默认的Worker ID生成。
|
||||
* <p>这个静态常量是通过调用IdUtil的getWorkerId方法,使用默认的数据中心ID和Snowflake算法允许的最大Worker ID来获取的。</p>
|
||||
*
|
||||
* @see IdUtil#getWorkerId(long, long) 获取Worker ID的具体实现方法
|
||||
* @see Snowflake#MAX_WORKER_ID Snowflake算法中定义的最大Worker ID
|
||||
*/
|
||||
public static final long DEFAULT_WORKER_ID = IdUtil.getWorkerId(DEFAULT_DATACENTER_ID, Snowflake.MAX_WORKER_ID);
|
||||
}
|
Loading…
Reference in New Issue
Block a user