diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java index 3076dcc7..ef8cea91 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeIDGenImpl.java @@ -19,7 +19,7 @@ public boolean init() { static private final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class); - private final long twepoch = 1288834974657L; + private final long twepoch; private final long workerIdBits = 10L; private final long maxWorkerId = -1L ^ (-1L << workerIdBits);//最大能够分配的workerid =1023 private final long sequenceBits = 12L; @@ -34,8 +34,21 @@ public boolean init() { private int port; public SnowflakeIDGenImpl(String zkAddress, int port) { - this.port = port; - SnowflakeZookeeperHolder holder = new SnowflakeZookeeperHolder(Utils.getIp(), String.valueOf(port), zkAddress); + //Thu Nov 04 2010 09:42:54 GMT+0800 (中国标准时间) 美团默认时间戳 + this(zkAddress, port, 1288834974657L); + } + + /** + * @param zkAddress zk地址 + * @param port snowflake监听端口 + * @param twepoch 我们定义的起始的时间戳,这样可以让时间戳存储时间更久 + */ + public SnowflakeIDGenImpl(String zkAddress, int port, long twepoch) { + this.twepoch = twepoch; + Preconditions.checkArgument(timeGen() > twepoch, "Snowflake not support twepoch gt currentTime"); + final String ip = Utils.getIp(); + SnowflakeZookeeperHolder holder = new SnowflakeZookeeperHolder(ip, String.valueOf(port), zkAddress); + LOGGER.info("twepoch:{} ,ip:{} ,zkAddress:{} port:{}", twepoch, ip, zkAddress, port); initFlag = holder.init(); if (initFlag) { workerId = holder.getWorkerID();