From 0da2d582e046695456a148959bd9400662b3a70e Mon Sep 17 00:00:00 2001 From: qidaijie Date: Fri, 9 Aug 2019 16:50:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SnowFlake/SnowflakeId.java | 5 +++-- Zookeeper/ZookeeperUtils.java | 9 ++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SnowFlake/SnowflakeId.java b/SnowFlake/SnowflakeId.java index e697202..5f77996 100644 --- a/SnowFlake/SnowflakeId.java +++ b/SnowFlake/SnowflakeId.java @@ -105,11 +105,12 @@ public class SnowflakeId { if (tmpWorkerId > maxWorkerId || tmpWorkerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } - if (FlowWriteConfig.DATA_CENTER_ID_NUM > maxDataCenterId || FlowWriteConfig.DATA_CENTER_ID_NUM < 0) { + int dataCenterId = FlowWriteConfig.DATA_CENTER_ID_NUM; + if (dataCenterId > maxDataCenterId || dataCenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDataCenterId)); } this.workerId = tmpWorkerId; - this.dataCenterId = FlowWriteConfig.DATA_CENTER_ID_NUM; + this.dataCenterId = dataCenterId; } // ==============================Methods========================================== diff --git a/Zookeeper/ZookeeperUtils.java b/Zookeeper/ZookeeperUtils.java index 612c4e7..639b50c 100644 --- a/Zookeeper/ZookeeperUtils.java +++ b/Zookeeper/ZookeeperUtils.java @@ -41,7 +41,7 @@ public class ZookeeperUtils implements Watcher { createNode("/Snowflake/" + FlowWriteConfig.KAFKA_TOPIC, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE); int workerId; try { - connectZookeeper(FlowWriteConfig.ZOOKEEPER_SERVERS); + connectZookeeper(); Stat stat = zookeeper.exists(path, true); workerId = Integer.parseInt(getNodeDate(path)); if (workerId > 55) { @@ -68,11 +68,10 @@ public class ZookeeperUtils implements Watcher { /** * 连接zookeeper * - * @param host 地址 */ - private void connectZookeeper(String host) { + private void connectZookeeper() { try { - zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this); + zookeeper = new ZooKeeper(FlowWriteConfig.ZOOKEEPER_SERVERS, SESSION_TIME_OUT, this); countDownLatch.await(); } catch (IOException | InterruptedException e) { e.printStackTrace(); @@ -118,7 +117,7 @@ public class ZookeeperUtils implements Watcher { */ private void createNode(String path, byte[] date, List acls) { try { - connectZookeeper(FlowWriteConfig.ZOOKEEPER_SERVERS); + connectZookeeper(); Stat exists = zookeeper.exists(path, true); if (exists == null) { zookeeper.create(path, date, acls, CreateMode.PERSISTENT);