Skip to content

Commit

Permalink
支持自定义起始时间
Browse files Browse the repository at this point in the history
  • Loading branch information
qixiaobo committed Sep 30, 2019
1 parent 2005779 commit 1f75828
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 1f75828

Please sign in to comment.