{"id":2048,"date":"2023-04-01T21:09:32","date_gmt":"2023-04-01T13:09:32","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2048"},"modified":"2023-04-07T06:06:49","modified_gmt":"2023-04-06T22:06:49","slug":"distributed-snowflake-algorithm","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/distributed-snowflake-algorithm\/","title":{"rendered":"\u5206\u5e03\u5f0fSnowflake\u96ea\u82b1\u7b97\u6cd5"},"content":{"rendered":"<h2>\u4e3b\u952eID\u751f\u6210\u65b9\u5f0f\u6bd4\u8f83<\/h2>\n<ul>\n<li><code>UUID<\/code>\uff08\u7f3a\u70b9\uff1a\u592a\u957f\u3001\u6ca1\u6cd5\u6392\u5e8f\u3001\u4f7f\u6570\u636e\u5e93\u6027\u80fd\u964d\u4f4e\uff09<\/li>\n<li><code>Redis<\/code>\uff08\u7f3a\u70b9\uff1a\u5fc5\u987b\u4f9d\u8d56Redis\uff09<\/li>\n<li><code>Oracle\u5e8f\u5217\u53f7<\/code>\uff08\u7f3a\u70b9\uff1a\u7528Oracle\u624d\u80fd\u4f7f\u7528\uff09<\/li>\n<li><code>Snowflake\u96ea\u82b1\u7b97\u6cd5<\/code>\uff08\u4f18\u70b9\uff1a\u751f\u6210\u6709\u987a\u5e8f\u7684id\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u7684\u6027\u80fd\uff09<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>Snowflake\u96ea\u82b1\u7b97\u6cd5\u89e3\u6790<\/h2>\n<p>\u96ea\u82b1\u7b97\u6cd5snowflake\u7684\u7ed3\u6784\u5982\u4e0b(\u6bcf\u90e8\u5206\u7528-\u5206\u5f00):<\/p>\n<p><code>0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000<\/code><\/p>\n<ul>\n<li>\u7b2c\u4e00\u4f4d\u4e3a\u672a\u4f7f\u7528\uff0c\u63a5\u4e0b\u6765\u768441\u4f4d\u4e3a\u6beb\u79d2\u7ea7\u65f6\u95f4\uff0841\u4f4d\u7684\u957f\u5ea6\u53ef\u4ee5\u4f7f\u752869\u5e74\uff09<\/li>\n<li>\u7136\u540e\u662f5\u4f4ddatacenterId\u548c5\u4f4dworkerId\uff0810\u4f4d\u7684\u957f\u5ea6\u6700\u591a\u652f\u6301\u90e8\u7f721024\u4e2a\u8282\u70b9\uff09<\/li>\n<li>\u6700\u540e12\u4f4d\u662f\u6beb\u79d2\u5185\u7684\u8ba1\u6570\uff0812\u4f4d\u7684\u8ba1\u6570\u987a\u5e8f\u53f7\u652f\u6301\u6bcf\u4e2a\u8282\u70b9\u6bcf\u6beb\u79d2\u4ea7\u751f4096\u4e2aID\u5e8f\u53f7\uff09<\/li>\n<\/ul>\n<p>\u4e00\u5171\u52a0\u8d77\u6765\u521a\u597d64\u4f4d\uff0c\u4e3a\u4e00\u4e2aLong\u578b\uff08\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a18\uff09<\/p>\n<p>Snowflake\u7b97\u6cd5\u6838\u5fc3\u628a\u65f6\u95f4\u6233\uff0c\u5de5\u4f5c\u673a\u5668id\uff0c\u5e8f\u5217\u53f7\u7ec4\u5408\u5728\u4e00\u8d77<\/p>\n<p>\u6574\u4f53\u4e0a\u6309\u7167\u65f6\u95f4\u81ea\u589e\u6392\u5e8f\uff0c\u5e76\u4e14\u6574\u4e2a\u5206\u5e03\u5f0f\u7cfb\u7edf\u5185\u4e0d\u4f1a\u4ea7\u751fID\u78b0\u649e\uff08\u7531datacenter\u548c\u673a\u5668ID\u4f5c\u533a\u5206\uff09\uff0c\u5e76\u4e14\u6548\u7387\u8f83\u9ad8\uff0c\u7ecf\u6d4b\u8bd5\uff0csnowflake\u6bcf\u79d2\u80fd\u591f\u4ea7\u751f26\u4e07ID\u5de6\u53f3\uff0c\u5b8c\u5168\u6ee1\u8db3\u9700\u8981\u3002<\/p>\n<h2>\u5206\u5e03\u5f0fSnowflake\u96ea\u82b1\u7b97\u6cd5\u4ee3\u7801<\/h2>\n<pre><code class=\"language-java\">public class SnowFlakeGenerator {\n    public static class Factory {\n        \/**\n         * \u6bcf\u4e00\u90e8\u5206\u5360\u7528\u4f4d\u6570\u7684\u9ed8\u8ba4\u503c\n         *\/\n        private final static int DEFAULT_MACHINE_BIT_NUM = 5;   \/\/\u673a\u5668\u6807\u8bc6\u5360\u7528\u7684\u4f4d\u6570\n        private final static int DEFAULT_IDC_BIT_NUM = 5;\/\/\u6570\u636e\u4e2d\u5fc3\u5360\u7528\u7684\u4f4d\u6570\n\n        private int machineBitNum;\n        private int idcBitNum;\n\n        public Factory() {\n            this.idcBitNum = DEFAULT_IDC_BIT_NUM;\n            this.machineBitNum = DEFAULT_MACHINE_BIT_NUM;\n        }\n\n        public Factory(int machineBitNum, int idcBitNum) {\n            this.idcBitNum = idcBitNum;\n            this.machineBitNum = machineBitNum;\n        }\n\n        public SnowFlakeGenerator create(long idcId, long machineId) {\n            return new SnowFlakeGenerator(this.idcBitNum, this.machineBitNum, idcId, machineId);\n        }\n    }\n\n    \/**\n     * \u8d77\u59cb\u7684\u65f6\u95f4\u6233\n     * \u4f5c\u8005\u5199\u4ee3\u7801\u65f6\u7684\u65f6\u95f4\u6233\n     *\/\n    private final static long START_STAMP = 1508143349995L;\n\n    \/**\n     * \u53ef\u5206\u914d\u7684\u4f4d\u6570\n     *\/\n    private final static int REMAIN_BIT_NUM = 22;\n\n    \/**\n     * idc\u7f16\u53f7\n     *\/\n    private long idcId;\n\n    \/**\n     * \u673a\u5668\u7f16\u53f7\n     *\/\n    private long machineId;\n\n    \/**\n     * \u5f53\u524d\u5e8f\u5217\u53f7\n     *\/\n    private long sequence = 0L;\n\n    \/**\n     * \u4e0a\u6b21\u6700\u65b0\u65f6\u95f4\u6233\n     *\/\n    private long lastStamp = -1L;\n\n    \/**\n     * idc\u504f\u79fb\u91cf\uff1a\u4e00\u6b21\u8ba1\u7b97\u51fa\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\n     *\/\n    private int idcBitLeftOffset;\n\n    \/**\n     * \u673a\u5668id\u504f\u79fb\u91cf\uff1a\u4e00\u6b21\u8ba1\u7b97\u51fa\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\n     *\/\n    private int machineBitLeftOffset;\n\n    \/**\n     * \u65f6\u95f4\u6233\u504f\u79fb\u91cf\uff1a\u4e00\u6b21\u8ba1\u7b97\u51fa\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\n     *\/\n    private int timestampBitLeftOffset;\n\n    \/**\n     * \u6700\u5927\u5e8f\u5217\u503c\uff1a\u4e00\u6b21\u8ba1\u7b97\u51fa\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\n     *\/\n    private int maxSequenceValue;\n\n    private SnowFlakeGenerator(int idcBitNum, int machineBitNum, long idcId, long machineId) {\n        int sequenceBitNum = REMAIN_BIT_NUM - idcBitNum - machineBitNum;\n\n        if (idcBitNum &lt;= 0 || machineBitNum &lt;= 0 || sequenceBitNum &lt;= 0) {\n            throw new IllegalArgumentException(&quot;error bit number&quot;);\n        }\n\n        this.maxSequenceValue = ~(-1 &lt;&lt; sequenceBitNum);\n\n        machineBitLeftOffset = sequenceBitNum;\n        idcBitLeftOffset = idcBitNum + sequenceBitNum;\n        timestampBitLeftOffset = idcBitNum + machineBitNum + sequenceBitNum;\n\n        this.idcId = idcId;\n        this.machineId = machineId;\n    }\n\n    \/**\n     * \u4ea7\u751f\u4e0b\u4e00\u4e2aID\n     *\/\n    public synchronized long nextId() {\n        long currentStamp = getTimeMill();\n        if (currentStamp &lt; lastStamp) {\n            throw new RuntimeException(String.format(&quot;Clock moved backwards. Refusing to generate id for %d milliseconds&quot;, lastStamp - currentStamp));\n        }\n\n        \/\/\u65b0\u7684\u6beb\u79d2\uff0c\u5e8f\u5217\u4ece0\u5f00\u59cb\uff0c\u5426\u5219\u5e8f\u5217\u81ea\u589e\n        if (currentStamp == lastStamp) {\n            sequence = (sequence + 1) &amp; this.maxSequenceValue;\n            if (sequence == 0L) {\n                \/\/Twitter\u6e90\u4ee3\u7801\u4e2d\u7684\u903b\u8f91\u662f\u5faa\u73af\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2a\u6beb\u79d2\n                lastStamp = tilNextMillis();\n                \/\/throw new IllegalStateException(&quot;sequence over flow&quot;);\n            }\n        } else {\n            sequence = 0L;\n        }\n\n        lastStamp = currentStamp;\n\n        return (currentStamp - START_STAMP) &lt;&lt; timestampBitLeftOffset | idcId &lt;&lt; idcBitLeftOffset | machineId &lt;&lt; machineBitLeftOffset | sequence;\n    }\n\n    private long getTimeMill() {\n        return System.currentTimeMillis();\n    }\n\n    private long tilNextMillis() {\n        long timestamp = getTimeMill();\n        while (timestamp &lt;= lastStamp) {\n            timestamp = getTimeMill();\n        }\n        return timestamp;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u952eID\u751f\u6210\u65b9\u5f0f\u6bd4\u8f83 UUID\uff08\u7f3a\u70b9\uff1a\u592a\u957f\u3001\u6ca1\u6cd5\u6392\u5e8f\u3001\u4f7f\u6570\u636e\u5e93\u6027\u80fd\u964d\u4f4e\uff09 Redis\uff08\u7f3a\u70b9\uff1a\u5fc5\u987b\u4f9d\u8d56Redis [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[516],"class_list":["post-2048","post","type-post","status-publish","format-standard","hentry","category-data-structure-algorithm","tag-snowflake"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2048"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2048\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}