diff --git a/README.md b/README.md index d26a2202..918fe106 100644 --- a/README.md +++ b/README.md @@ -108,3 +108,7 @@ insert into leaf_alloc(biz_tag, max_id, step, description) values('leaf-segment- ### Leaf Core 当然,为了追求更高的性能,需要通过RPC Server来部署Leaf 服务,那仅需要引入leaf-core的包,把生成ID的API封装到指定的RPC框架中即可。 + +#### 注意事项 +注意现在leaf使用snowflake模式的情况下 其获取ip的逻辑直接取首个网卡ip【特别对于会更换ip的服务要注意】避免浪费workId + diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java index c0bd8b43..e217e0bf 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java @@ -156,6 +156,7 @@ public void updateSegmentFromDb(String key, Segment segment) { } else if (buffer.getUpdateTimestamp() == 0) { leafAlloc = dao.updateMaxIdAndGetLeafAlloc(key); buffer.setUpdateTimestamp(System.currentTimeMillis()); + buffer.setStep(leafAlloc.getStep()); buffer.setMinStep(leafAlloc.getStep());//leafAlloc中的step为DB中的step } else { long duration = System.currentTimeMillis() - buffer.getUpdateTimestamp(); 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 ef8cea91..fa706ab0 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 @@ -17,31 +17,29 @@ public boolean init() { return true; } - static private final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class); private final long twepoch; private final long workerIdBits = 10L; - private final long maxWorkerId = -1L ^ (-1L << workerIdBits);//最大能够分配的workerid =1023 + private final long maxWorkerId = ~(-1L << workerIdBits);//最大能够分配的workerid =1023 private final long sequenceBits = 12L; private final long workerIdShift = sequenceBits; private final long timestampLeftShift = sequenceBits + workerIdBits; - private final long sequenceMask = -1L ^ (-1L << sequenceBits); + private final long sequenceMask = ~(-1L << sequenceBits); private long workerId; private long sequence = 0L; private long lastTimestamp = -1L; - public boolean initFlag = false; private static final Random RANDOM = new Random(); - private int port; public SnowflakeIDGenImpl(String zkAddress, int port) { - //Thu Nov 04 2010 09:42:54 GMT+0800 (中国标准时间) 美团默认时间戳 + //Thu Nov 04 2010 09:42:54 GMT+0800 (中国标准时间) this(zkAddress, port, 1288834974657L); } /** * @param zkAddress zk地址 * @param port snowflake监听端口 - * @param twepoch 我们定义的起始的时间戳,这样可以让时间戳存储时间更久 + * @param twepoch 起始的时间戳 */ public SnowflakeIDGenImpl(String zkAddress, int port, long twepoch) { this.twepoch = twepoch; diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeZookeeperHolder.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeZookeeperHolder.java index 2c92b258..76e85e85 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeZookeeperHolder.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/snowflake/SnowflakeZookeeperHolder.java @@ -74,8 +74,9 @@ public boolean init() { //有自己的节点,zk_AddressNode=ip:port zk_AddressNode = PATH_FOREVER + "/" + realNode.get(listenAddress); workerID = workerid;//启动worder时使用会使用 - if (!checkInitTimeStamp(curator, zk_AddressNode)) + if (!checkInitTimeStamp(curator, zk_AddressNode)) { throw new CheckLastTimeException("init timestamp check error,forever node timestamp gt this node time"); + } //准备创建临时节点 doService(curator); updateLocalWorkerID(workerID); @@ -186,12 +187,12 @@ private Endpoint deBuildData(String json) throws IOException { * @param workerID */ private void updateLocalWorkerID(int workerID) { - File LeafconfFile = new File(PROP_PATH.replace("{port}", port)); - boolean exists = LeafconfFile.exists(); + File leafConfFile = new File(PROP_PATH.replace("{port}", port)); + boolean exists = leafConfFile.exists(); LOGGER.info("file exists status is {}", exists); if (exists) { try { - FileUtils.writeStringToFile(LeafconfFile, "workerID=" + workerID, false); + FileUtils.writeStringToFile(leafConfFile, "workerID=" + workerID, false); LOGGER.info("update file cache workerID is {}", workerID); } catch (IOException e) { LOGGER.error("update file cache error ", e); @@ -199,11 +200,11 @@ private void updateLocalWorkerID(int workerID) { } else { //不存在文件,父目录页肯定不存在 try { - boolean mkdirs = LeafconfFile.getParentFile().mkdirs(); + boolean mkdirs = leafConfFile.getParentFile().mkdirs(); LOGGER.info("init local file cache create parent dis status is {}, worker id is {}", mkdirs, workerID); if (mkdirs) { - if (LeafconfFile.createNewFile()) { - FileUtils.writeStringToFile(LeafconfFile, "workerID=" + workerID, false); + if (leafConfFile.createNewFile()) { + FileUtils.writeStringToFile(leafConfFile, "workerID=" + workerID, false); LOGGER.info("local file cache workerID is {}", workerID); } } else { diff --git a/leaf-server/deploy/run.sh b/leaf-server/deploy/run.sh index 77b06c59..d94ce9d4 100644 --- a/leaf-server/deploy/run.sh +++ b/leaf-server/deploy/run.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash