{"id":201808,"date":"2025-05-10T09:00:15","date_gmt":"2025-05-10T01:00:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201808\/"},"modified":"2025-05-10T09:00:15","modified_gmt":"2025-05-10T01:00:15","slug":"redis%e5%88%86%e5%b8%83%e5%bc%8fid%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201808\/","title":{"rendered":"redis\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\u8be6\u89e3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6570\u636e\u5e93\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aredis\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\u8be6\u89e3\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u89e3\u51b3\u65b9\u6848\u3001redis\u5206\u5e03\u5f0fID\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<h2>\u5e38\u7528\u7684\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u751f\u6210ID\u53ef\u80fd\u4f1a\u5bfc\u81f4ID\u51b2\u7a81\u3002<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230512\/1683891549645e255dc41f0.jpg1683891549645e255dc4206.png\" class=\"aligncenter\"><\/p>\n<h3>UUID<\/h3>\n<p>UUID\uff08\u901a\u7528\u552f\u4e00\u6807\u8bc6\u7b26\uff09\u662f\u7531128\u4f4d\u6570\u5b57\u7ec4\u6210\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u5728\u5168\u7403\u8303\u56f4\u5185\u7684\u552f\u4e00\u6027\uff0c\u56e0\u4e3a\u5176\u751f\u6210\u7b97\u6cd5\u57fa\u4e8e\u65f6\u95f4\u6233\u3001\u8282\u70b9ID\u7b49\u56e0\u7d20\u3002UUID\u53ef\u4ee5\u4f7f\u7528Java\u81ea\u5e26\u7684UUID\u7c7b\u6765\u751f\u6210\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>javaCopy code\nimport java.util.UUID;\npublic class UuidGenerator {\n    public static void main(String[] args) {\n        UUID uuid = UUID.randomUUID();\n        System.out.println(uuid.toString());\n    }\n}\n<\/pre>\n<p>UUID\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u65e0\u9700\u989d\u5916\u7684\u914d\u7f6e\u548c\u7ba1\u7406\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Java\u81ea\u5e26\u7684UUID\u7c7b\u751f\u6210\u3002\u4f46\u662fUUID\u957f\u5ea6\u8f83\u957f\uff08128\u4f4d\uff09\uff0c\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6570\u636e\u5e93\u8868\u7684\u4e3b\u952e\uff0c\u4e14\u4e0d\u6613\u4e8e\u6392\u5e8f\u548c\u7d22\u5f15\u3002<\/p>\n<h3>Snowflake<\/h3>\n<p>Snowflake\u662fTwitter\u5f00\u6e90\u7684\u4e00\u79cd\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\uff0c\u5b83\u53ef\u4ee5\u751f\u621064\u4f4d\u7684\u552f\u4e00ID\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u65f6\u95f4\u6233\u3001\u6570\u636e\u4e2d\u5fc3ID\u548c\u673a\u5668ID\u7b49\u4fe1\u606f\u3002Snowflake\u7b97\u6cd5\u7684Java\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<h4>Snowflake\u7b97\u6cd5\u7684Java\u4ee3\u7801\uff1a<\/h4>\n<pre>javaCopy code\npublic class SnowflakeGenerator {\n    private final static long START_STMP = 1480166465631L;\n    private final static long SEQUENCE_BIT = 12;\n    private final static long MACHINE_BIT = 5;\n    private final static long DATACENTER_BIT = 5;\n    private final static long MAX_DATACENTER_NUM = -1L ^ (-1L  MAX_DATACENTER_NUM || datacenterId  MAX_MACHINE_NUM || machineId \n<p>Snowflake\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u751f\u6210ID\u7684\u6027\u80fd\u9ad8\uff0c\u4e14ID\u957f\u5ea6\u8f83\u77ed\uff0864\u4f4d\uff09\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u6570\u636e\u5e93\u8868\u7684\u4e3b\u952e\uff0c\u4e14\u4fbf\u4e8e\u6392\u5e8f\u548c\u7d22\u5f15\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679c\u96c6\u7fa4\u4e2d\u7684\u8282\u70b9\u6570\u8d85\u8fc7\u4e86\u673a\u5668ID\u6240\u5360\u7684\u4f4d\u6570\uff0c\u6216\u8005\u96c6\u7fa4\u89c4\u6a21\u5f88\u5927\uff0c\u65f6\u95f4\u6233\u4f4d\u6570\u4e0d\u591f\u7528\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8003\u8651\u5176\u4ed6\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\u3002<\/p>\n<h3>Leaf<\/h3>\n<p>Leaf\u662f\u7f8e\u56e2\u70b9\u8bc4\u5f00\u6e90\u7684\u4e00\u79cd\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u5168\u5c40\u552f\u4e00\u768464\u4f4dID\u3002Leaf\u7b97\u6cd5\u7684Java\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<h4>Leaf\u7b97\u6cd5\u7684Java\u4ee3\u7801\uff1a<\/h4>\n<pre>javaCopy code\npublic class LeafGenerator {\n    private static final Logger logger = LoggerFactory.getLogger(LeafGenerator.class);\n    private static final String WORKER_ID_KEY = \"leaf.worker.id\";\n    private static final String PORT_KEY = \"leaf.port\";\n    private static final int DEFAULT_PORT = 8080;\n    private static final int DEFAULT_WORKER_ID = 0;\n    private static final int WORKER_ID_BITS = 10;\n    private static final int SEQUENCE_BITS = 12;\n    private static final int MAX_WORKER_ID = (1  MAX_WORKER_ID) {\n                throw new IllegalArgumentException(String.format(\"workerId must be between %d and %d\", 0, MAX_WORKER_ID));\n            }\n            this.workerId = workerId;\n            this.port = port;\n        }\n        synchronized long nextId() {\n            long timestamp = System.currentTimeMillis();\n            if (timestamp \n<p>Leaf\u7b97\u6cd5\u7684\u7279\u70b9\u662f\u751f\u6210ID\u7684\u901f\u5ea6\u6bd4Snowflake\u7b97\u6cd5\u7565\u6162\uff0c\u4f46\u662f\u53ef\u4ee5\u652f\u6301\u66f4\u591a\u7684Worker\u8282\u70b9\u3002Leaf\u7b97\u6cd5\u751f\u6210\u7684ID\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff0c\u5206\u522b\u662f\u65f6\u95f4\u6233\u3001Worker ID\u548c\u5e8f\u5217\u53f7\uff0c\u5176\u4e2d\u65f6\u95f4\u6233\u5360\u752842\u4f4d\u3001Worker ID\u5360\u752810\u4f4d\u3001\u5e8f\u5217\u53f7\u5360\u752812\u4f4d\uff0c\u603b\u517164\u4f4d\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u5e38\u89c1\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\uff0c\u5f53\u7136\u8fd8\u6709\u5176\u4ed6\u7684\u4e00\u4e9b\u65b9\u6848\uff0c\u5982\uff1aMongoDB ID\u3001UUID\u3001Twitter Snowflake\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6848\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f\uff0c\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u548c\u6027\u80fd\u8868\u73b0\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u3002<\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u65b0\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u65b9\u6848\u4e0d\u65ad\u6d8c\u73b0\uff0c\u4f8b\u5982Flicker\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u7b97\u6cd5\uff0c\u5b83\u4f7f\u7528\u4e86\u7c7b\u4f3c\u4e8eSnowflake\u7684\u601d\u60f3\uff0c\u4f46\u662f\u91c7\u7528\u4e86\u4e0d\u540c\u7684\u4f4d\u6570\u5206\u914d\u65b9\u5f0f\uff0c\u76f8\u6bd4Snowflake\u66f4\u52a0\u7075\u6d3b\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u6bcf\u4e2a\u90e8\u5206\u5360\u7528\u7684\u4f4d\u6570\u3002\u6b64\u5916\uff0cFacebook\u8fd8\u63a8\u51fa\u4e86ID Generation Service (IGS)\u65b9\u6848\uff0c\u8be5\u65b9\u6848\u5c06ID\u7684\u751f\u6210\u548c\u5b58\u50a8\u5206\u79bb\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u65b9\u6848\uff0c\u4f46\u662f\u9700\u8981\u8fdb\u884c\u66f4\u52a0\u590d\u6742\u7684\u67b6\u6784\u8bbe\u8ba1\u548c\u5b9e\u73b0\u3002<\/p>\n<p>\u9488\u5bf9\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u53ef\u4ee5\u8bbe\u8ba1\u591a\u5957\u5206\u5e03\u5f0fID\u751f\u6210\u65b9\u6848\u3002\u4e0b\u9762\u662f\u6211\u4e2a\u4eba\u7684\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/pre>\n<ul>\n<li>\u57fa\u4e8e\u6570\u636e\u5e93\u81ea\u589eID\u751f\u6210\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u81ea\u589eID\u4f5c\u4e3a\u5168\u5c40\u552f\u4e00ID\uff0c\u53ef\u4ee5\u5f88\u597d\u7684\u4fdd\u8bc1ID\u7684\u552f\u4e00\u6027\uff0c\u5e76\u4e14\u5b9e\u73b0\u7b80\u5355\uff0c\u4f46\u662f\u5e76\u53d1\u91cf\u8f83\u9ad8\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002\u56e0\u6b64\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002<\/li>\n<li>\u57fa\u4e8eUUID\u751f\u6210\uff1a\u4f7f\u7528UUID\u4f5c\u4e3a\u5168\u5c40\u552f\u4e00ID\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u4fdd\u8bc1ID\u7684\u552f\u4e00\u6027\uff0c\u4f46\u662fID\u957f\u5ea6\u8f83\u957f\uff08128\u4f4d\uff09\uff0c\u4e0d\u4fbf\u4e8e\u5b58\u50a8\u548c\u4f20\u8f93\uff0c\u5e76\u4e14\u5b58\u5728\u91cd\u590dID\u7684\u6982\u7387\u975e\u5e38\u5c0f\u4f46\u4e0d\u4e3a0\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u4f7f\u7528\u65f6\u8981\u8003\u8651ID\u7684\u957f\u5ea6\u548c\u5b58\u50a8\u4f20\u8f93\u7684\u6210\u672c\u3002<\/li>\n<li>\u57fa\u4e8eRedis\u751f\u6210\uff1a\u4f7f\u7528Redis\u7684\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4fdd\u8bc1ID\u7684\u552f\u4e00\u6027\uff0c\u5e76\u4e14\u751f\u6210ID\u7684\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u53ef\u4ee5\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679cRedis\u5b95\u673a\u6216\u8005\u6027\u80fd\u4e0d\u8db3\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cdID\u7684\u751f\u6210\u6548\u7387\u548c\u53ef\u7528\u6027\u3002<\/li>\n<li>\u57fa\u4e8eZooKeeper\u751f\u6210\uff1a\u4f7f\u7528ZooKeeper\u7684\u5e8f\u5217\u53f7\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u4fdd\u8bc1ID\u7684\u552f\u4e00\u6027\uff0c\u5e76\u4e14\u5b9e\u73b0\u8f83\u4e3a\u7b80\u5355\uff0c\u4f46\u662f\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u4f9d\u8d56\u548c\u8d44\u6e90\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<\/ul>\n<p>\u9009\u62e9\u9002\u5408\u81ea\u5df1\u4e1a\u52a1\u573a\u666f\u7684\u5206\u5e03\u5f0fID\u751f\u6210\u65b9\u6848\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651ID\u7684\u552f\u4e00\u6027\u3001\u751f\u6210\u901f\u5ea6\u3001\u957f\u5ea6\u3001\u5b58\u50a8\u6210\u672c\u3001\u53ef\u6269\u5c55\u6027\u3001\u53ef\u7528\u6027\u7b49\u591a\u4e2a\u56e0\u7d20\u3002\u540c\u65f6\u9700\u8981\u6ce8\u610f\uff0c\u4e0d\u540c\u65b9\u6848\u7684\u5b9e\u73b0\u7ec6\u8282\u548c\u6027\u80fd\u8868\u73b0\u4e5f\u6709\u6240\u4e0d\u540c\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\u3002<\/p>\n<p>\u4e0b\u9762\u7ed9\u51fa\u6bcf\u79cd\u65b9\u6848\u7684\u8be6\u7ec6\u4ee3\u7801demo\uff1a<\/p>\n<h2>\u57fa\u4e8e\u6570\u636e\u5e93\u81ea\u589eID\u751f\u6210<\/h2>\n<pre>javaCopy code\npublic class IdGenerator {\n    private static final String JDBC_URL = \"jdbc:mysql:\/\/localhost:3306\/test\";\n    private static final String JDBC_USER = \"root\";\n    private static final String JDBC_PASSWORD = \"password\";\n    public long generateId() {\n        Connection conn = null;\n        PreparedStatement pstmt = null;\n        ResultSet rs = null;\n        try {\n            Class.forName(\"com.mysql.jdbc.Driver\");\n            conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);\n            pstmt = conn.prepareStatement(\"INSERT INTO id_generator (stub) VALUES (null)\", Statement.RETURN_GENERATED_KEYS);\n            pstmt.executeUpdate();\n            rs = pstmt.getGeneratedKeys();\n            if (rs.next()) {\n                return rs.getLong(1);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (rs != null) {\n                    rs.close();\n                }\n                if (pstmt != null) {\n                    pstmt.close();\n                }\n                if (conn != null) {\n                    conn.close();\n                }\n            } catch (Exception e) {\n                e.printStackTrace();\n            }\n        }\n        return 0L;\n    }\n}\n<\/pre>\n<h2>\u57fa\u4e8eUUID\u751f\u6210<\/h2>\n<pre>javaCopy code\nimport java.util.UUID;\npublic class IdGenerator {\n    public String generateId() {\n        return UUID.randomUUID().toString().replace(\"-\", \"\");\n    }\n}\n<\/pre>\n<h2>\u57fa\u4e8eRedis\u751f\u6210<\/h2>\n<pre>javaCopy code\nimport redis.clients.jedis.Jedis;\npublic class IdGenerator {\n    private static final String REDIS_HOST = \"localhost\";\n    private static final int REDIS_PORT = 6379;\n    private static final String REDIS_PASSWORD = \"password\";\n    private static final int ID_GENERATOR_EXPIRE_SECONDS = 3600;\n    private static final String ID_GENERATOR_KEY = \"id_generator\";\n    public long generateId() {\n        Jedis jedis = null;\n        try {\n            jedis = new Jedis(REDIS_HOST, REDIS_PORT);\n            jedis.auth(REDIS_PASSWORD);\n            long id = jedis.incr(ID_GENERATOR_KEY);\n            jedis.expire(ID_GENERATOR_KEY, ID_GENERATOR_EXPIRE_SECONDS);\n            return id;\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            if (jedis != null) {\n                jedis.close();\n            }\n        }\n        return 0L;\n    }\n}\n<\/pre>\n<h2>\u57fa\u4e8eZooKeeper\u751f\u6210<\/h2>\n<pre>javaCopy code\nimport java.util.concurrent.CountDownLatch;\nimport org.apache.zookeeper.CreateMode;\nimport org.apache.zookeeper.WatchedEvent;\nimport org.apache.zookeeper.Watcher;\nimport org.apache.zookeeper.ZooDefs.Ids;\nimport org.apache.zookeeper.ZooKeeper;\npublic class IdGenerator implements Watcher {\n    private static final String ZK_HOST = \"localhost\";\n    private static final int ZK_PORT = 2181;\n    private static final int SESSION_TIMEOUT = 5000;\n    private static final String ID_GENERATOR_NODE = \"\/id_generator\";\n    private static final int ID_GENERATOR_EXPIRE_SECONDS = 3600;\n    private long workerId = 0;\n    public IdGenerator() {\n        try {\n            ZooKeeper zk = new ZooKeeper(ZK_HOST + \":\" + ZK_PORT, SESSION_TIMEOUT, this);\n            CountDownLatch latch = new CountDownLatch(1);\n            latch.await();\n            if (zk.exists(ID_GENERATOR_NODE, false) == null) {\n                zk.create(ID_GENERATOR_NODE, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);\n            }\n            workerId = zk.getChildren(ID_GENERATOR_NODE, false).size();\n            zk.create(ID_GENERATOR_NODE + \"\/worker_\" + workerId, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n    public long generateId() {\n        ZooKeeper zk = null;\n        try {\n            zk = new ZooKeeper(ZK_HOST + \":\" + ZK_PORT, SESSION_TIMEOUT, null);\n            CountDownLatch latch = new CountDownLatch(1);\n            latch.await();\n            zk.create(ID_GENERATOR_NODE + \"\/id_\", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -&gt; {}, null);\n            byte[] data = zk.getData(ID_GENERATOR_NODE + \"\/worker_\" + workerId, false, null);\n            long id = Long.parseLong(new String(data)) * 10000 + zk.getChildren(ID_GENERATOR_NODE, false).size();\n            return id;\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            if (zk != null) {\n                try {\n                    zk.close();\n                } catch (Exception e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n        return 0L;\n    }\n    @Override\n    public void process(WatchedEvent event) {\n        if (event.getState() == Event.KeeperState.SyncConnected) {\n            System.out.println(\"Connected to ZooKeeper\");\n            CountDownLatch latch = new CountDownLatch(1);\n            latch.countDown();\n        }\n    }\n}\n<\/pre>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86ZooKeeper\u7684\u4e34\u65f6\u8282\u70b9\u6765\u534f\u8c03\u5404\u4e2a\u5de5\u4f5c\u8282\u70b9\uff0c\u5982\u679c\u4e00\u4e2a\u5de5\u4f5c\u8282\u70b9\u6302\u6389\u4e86\uff0c\u5b83\u7684\u4e34\u65f6\u8282\u70b9\u4e5f\u4f1a\u88ab\u5220\u9664\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u4e2a\u5de5\u4f5c\u8282\u70b9\u83b7\u5f97\u7684ID\u662f\u552f\u4e00\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u5206\u5e03\u5f0fID\u89e3\u51b3\u65b9\u6848\u793a\u4f8b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201808","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201808"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201808\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}