{"id":275,"date":"2018-12-14T22:00:55","date_gmt":"2018-12-14T14:00:55","guid":{"rendered":"http:\/\/kaispace.cn\/?p=275"},"modified":"2018-12-14T22:00:55","modified_gmt":"2018-12-14T14:00:55","slug":"%e9%ab%98%e5%b9%b6%e5%8f%91%e4%b8%8b%e8%ae%a2%e5%8d%95%e5%8f%b7%e7%9a%84%e7%94%9f%e6%88%90%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=275","title":{"rendered":"\u9ad8\u5e76\u53d1\u4e0b\u8ba2\u5355\u53f7\u7684\u751f\u6210\u95ee\u9898"},"content":{"rendered":"<h3>\u9ad8\u5e76\u53d1\u4e0b\u8ba2\u5355\u53f7\u751f\u6210\u5e94\u6ee1\u8db3\u7684\u8981\u6c42\uff1a<\/h3>\n<ol>\n<li>\u552f\u4e00\u6027\u3002<\/li>\n<li>\u53ef\u6392\u5e8f\u6027\u3002<\/li>\n<li>\u5206\u5e03\u5f0f\u652f\u6301<\/li>\n<\/ol>\n<h3>\u63d0\u4f9b\u7684\u65b9\u6848\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b<\/h3>\n<ol>\n<li>UUID\u7c7b\u578b<\/li>\n<li>\u5b57\u7b26\u4e32\u62fc\u63a5\u7c7b\u578b<\/li>\n<\/ol>\n<h4>1.UUID<\/h4>\n<p>\u8fd9\u91cc\u7528\u7684\u662fJava\u7684UUID\u7c7b(java.util.UUID)<\/p>\n<p>\u751f\u6210\u7684\u683c\u5f0f\u4e3a:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/p>\n<p>UUID\u751f\u6210\u7b97\u6cd5\u7528\u5230\u4e86\u65e5\u671f\u65f6\u95f4\u3001\u65f6\u949f\u5e8f\u5217\u4ee5\u53ca\u786c\u4ef6\u5730\u5740<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">public class JavaUUID {\n    private JavaUUID(){};\n    private final static Character emptyChar = null;\n    public static String getUUID(){\n        UUID u = UUID.randomUUID();\n        return u.toString().replace('-',emptyChar);\n    }\n}\n<\/code><\/pre>\n<h4>2.\u5b57\u7b26\u4e32\u62fc\u63a5\u7c7b\u578b<\/h4>\n<p>\u5176\u4e2d\u6700\u5178\u578b\u7684\u5c31\u662f<strong>twitter<\/strong>\u7684<strong>snowflake<\/strong>\u7b97\u6cd5\u4e86\uff0c\u5176\u957f\u5ea6\u4e3a\u4e00\u4e2along\uff088\u5b57\u828264\u4f4d\uff09\u59821073564473528561651\uff0c\u4ee5\u4e0b\u4e3a\u5404\u4e2a\u4f4d\u7684\u4f7f\u7528<\/p>\n<ul>\n<li>1\u4f4d\uff0c\u7b26\u53f7\u4f4d\uff0c\u9ed8\u8ba4\u4e3a0\u3002\u4f46\u662f\u6211\u4eec\u751f\u6210\u7684id\u4e00\u822c\u90fd\u4f7f\u7528\u6b63\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>41\u4f4d\uff0c\u7528\u6765\u8bb0\u5f55\u65f6\u95f4\u6233\uff08\u6beb\u79d2\uff09\u3002<br \/>\n41\u4f4d\u53ef\u4ee5\u8868\u793a241\u22121\u4e2a\u6570\u5b57\uff0c\u5982\u679c\u53ea\u7528\u6765\u8868\u793a\u6b63\u6574\u6570\uff08\u8ba1\u7b97\u673a\u4e2d\u6b63\u6570\u5305\u542b0\uff09\uff0c\u53ef\u4ee5\u8868\u793a\u7684\u6570\u503c\u8303\u56f4\u662f\uff1a0 \u81f3\u00a0241\u22121\uff0c\u51cf1\u662f\u56e0\u4e3a\u53ef\u8868\u793a\u7684\u6570\u503c\u8303\u56f4\u662f\u4ece0\u5f00\u59cb\u7b97\u7684\uff0c\u800c\u4e0d\u662f1\u3002\u4e5f\u5c31\u662f\u8bf441\u4f4d\u53ef\u4ee5\u8868\u793a241\u22121\u4e2a\u6beb\u79d2\u7684\u503c\uff0c \u8f6c\u5316\u6210\u5355\u4f4d\u5e74\u5219\u662f(241\u22121)\/(1000\u221760\u221760\u221724\u2217365)=69\u5e74<\/p>\n<\/li>\n<li>\n<p>10\u4f4d\uff0c\u7528\u6765\u8bb0\u5f55\u5de5\u4f5c\u673a\u5668id\u548c\u6570\u636e\u4e2d\u5fc3id\u3002\u53ef\u4ee5\u90e8\u7f72\u5728210=1024\u4e2a\u8282\u70b9\uff0c\u5305\u62ec5\u4f4ddatacenterId\u548c5\u4f4dworkerId<\/p>\n<\/li>\n<li>\n<p>5\u4f4d\uff08bit) \u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u6b63\u6574\u6570\u662f25\u22121=31\uff0c\u5373\u53ef\u4ee5\u75280\u30011\u30012\u30013\u3001&#8230;.31\u8fd932\u4e2a\u6570\u5b57\uff0c\u6765\u8868\u793a\u4e0d\u540c\u7684datecenterId\u6216workerId<\/p>\n<\/li>\n<li>\n<p>12\u4f4d\uff0cSequence\u5e8f\u5217\u53f7\uff0c\u7528\u6765\u8bb0\u5f55\u540c\u6beb\u79d2\u5185\u4ea7\u751f\u7684\u4e0d\u540cid\u300212\u4f4d\uff08bit\uff09\u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u6b63\u6574\u6570\u662f212\u22121=4095\uff0c\u5373\u53ef\u4ee5\u75280\u30011\u30012\u30013\u3001&#8230;.4094\u8fd94095\u4e2a\u6570\u5b57\uff0c\u6765\u8868\u793a\u540c\u4e00\u673a\u5668\u540c\u4e00\u65f6\u95f4\u622a\uff08\u6beb\u79d2)\u5185\u4ea7\u751f\u76844095\u4e2aID\u5e8f\u53f7<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">public class IdWorker {\n    private final long twepoch = 1288834974657L;\n    private final long workerIdBits = 5L;\n    private final long datacenterIdBits = 5L;\n    private final long maxWorkerId = ~(-1L &lt;&lt; workerIdBits);\n    private final long maxDatacenterId = ~(-1L &lt;&lt; datacenterIdBits);\n    private final long sequenceBits = 12L;\n    private final long workerIdShift = sequenceBits;\n    private final long datacenterIdShift = sequenceBits + workerIdBits;\n    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;\n    private final long sequenceMask = ~(-1L &lt;&lt; sequenceBits);\n\n    private long workerId;\n    private long datacenterId;\n    private long sequence = 0L;\n    private long lastTimestamp = -1L;\n\n    public IdWorker(long workerId,long datacenterId,long sequence){\n        \/\/worker\u8fc7\u591a 2^5=32 \u5373[0,31]\n        if(workerId&gt;maxWorkerId){\n            throw new IllegalArgumentException(String.format(\"worker Id can't be greater than %d or less than 0\", maxWorkerId));\n        }\n        \/\/datacenter\u8fc7\u591a 2^5=32 \u5373[0,31]\n        if (datacenterId &gt; maxDatacenterId || datacenterId &lt; 0) {\n            throw new IllegalArgumentException(String.format(\"datacenter Id can't be greater than %d or less than 0\", maxDatacenterId));\n        }\n        this.workerId = workerId;\n        this.datacenterId = datacenterId;\n        \/\/\u539fscala\u4ee3\u7801\u652f\u6301\u9ed8\u8ba4\u53c2\u6570,java\u4e0d\u652f\u6301\u6240\u4ee5\u7528null\u5427\n        this.sequence = sequence;\n    }\n\n    public synchronized long nextId() {\n        long timestamp = timeGen();\n        if (timestamp &lt; lastTimestamp) {\n            \/\/\u65f6\u95f4\u6233\u5c0f\u4e8e\u5f53\u524d\u65f6\u95f4\u6233\uff0c\u670d\u52a1\u5668\u4e8b\u95f4\u53d1\u751f\u56de\u6eda\n            throw new RuntimeException(String.format(\"Clock moved backwards.Refusing to generate id for %d milliseconds\", lastTimestamp - timestamp));\n        }\n        \/\/\u7528\u4e8e\u6700\u540e\u7684sequence\u589e\u957f\u4ee5\u53ca\u6beb\u7c73\u66f4\u66ff\u7684sequence\u91cd\u7f6e\n        if (lastTimestamp == timestamp) {\n            sequence = (sequence + 1) &amp; sequenceMask;\n            if (sequence == 0) {\n                \/\/\u4f7f\u540c\u65f6\u8fdb\u5165nextId\u8fd4\u56de\u7684\u8ba2\u5355\u65f6\u95f4\u6233\u4e0e\u5b8c\u6210\u4e8b\u4ef6\u66f4\u52a0\u63a5\u8fd1\n                timestamp = tilNextMillis(lastTimestamp);\n            }\n        } else {\n            sequence = 0L;\n        }\n\n        lastTimestamp = timestamp;\n        return ((timestamp - twepoch) &lt;&lt; timestampLeftShift) | (datacenterId &lt;&lt; datacenterIdShift) | (workerId &lt;&lt; workerIdShift) | sequence;\n    }\n\n    private long tilNextMillis(long lastTimestamp) {\n        long timestamp = timeGen();\n        while (timestamp &lt;= lastTimestamp) {\n            timestamp = timeGen();\n        }\n        return timestamp; \n    }\n\n    private long timeGen() {\n        return System.currentTimeMillis();\n    }\n}\n<\/code><\/pre>\n<p><!--more--><br \/>\n\u5176\u4f59\u7684\u7c7b\u4f3c\u652f\u4ed8\u5b9d\u6d41\u6c34\u5355\u53f7\u5e94\u8be5\u4e5f\u662f\u7c7b\u4f3c\u7684\u57fa\u4e8e\u65f6\u95f4+\u673a\u5668\u53f7+\u7d2f\u52a0\u53f7\u3002\u9664\u4e86\u5185\u5b58\u5b9e\u73b0\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\uff0c\u4f8b\u5982\u666e\u901a\u6570\u636e\u5e93\u7684\u9012\u589e\u5c5e\u6027\uff0c\u6216\u8005Redis\u7684indr\u65b9\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u5e76\u53d1\u4e0b\u8ba2\u5355\u53f7\u751f\u6210\u5e94\u6ee1\u8db3\u7684\u8981\u6c42\uff1a \u552f\u4e00\u6027\u3002 \u53ef\u6392\u5e8f\u6027\u3002 \u5206\u5e03\u5f0f\u652f\u6301 \u63d0\u4f9b\u7684\u65b9\u6848\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b UUID\u7c7b\u578b &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/275"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275"}],"version-history":[{"count":0,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}