{"id":52986,"date":"2024-12-03T11:27:23","date_gmt":"2024-12-03T03:27:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52986\/"},"modified":"2024-12-03T11:27:23","modified_gmt":"2024-12-03T03:27:23","slug":"snowuuid-%e7%ae%80%e4%bb%8b%ef%bc%9a%e5%8f%97-snowflake-%e5%90%af%e5%8f%91%e7%9a%84%e5%88%86%e5%b8%83%e5%bc%8f-uuid-%e7%94%9f%e6%88%90%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52986\/","title":{"rendered":"SnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>SnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aSnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u786e\u4fdd\u8de8\u591a\u4e2a\u670d\u52a1\u5668\u6216\u8fdb\u7a0b\u7684\u552f\u4e00 id \u53ef\u80fd\u5177\u6709\u6311\u6218\u6027\u3002 id \u5fc5\u987b\u662f\u552f\u4e00\u7684\u3001\u5feb\u901f\u751f\u6210\u7684\uff0c\u5e76\u4e14\u6709\u65f6\u8fd8\u4f20\u8fbe\u6709\u5173\u5176\u6765\u6e90\u7684\u4fe1\u606f\u3002\u53d7 twitter \u7684 snowflake id \u751f\u6210\u7b97\u6cd5\u7684\u542f\u53d1\uff0c\u6211\u5f00\u53d1\u4e86 <strong>snowuuid<\/strong>\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e13\u4e3a node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5206\u5e03\u5f0f uuid \u751f\u6210\u5668\u3002 snowuuid \u7ed3\u5408\u4e86\u7cbe\u5ea6\u548c\u53ef\u6269\u5c55\u6027\uff0c\u751f\u6210\u72ec\u7279\u7684\u3001\u6309\u65f6\u95f4\u6392\u5e8f\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u9488\u5bf9\u4efb\u4f55\u5206\u5e03\u5f0f\u8bbe\u7f6e\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48\u9009\u62e9 snowuuid\uff1f <\/h3>\n<p>snowuuid \u5c06 twitter snowflake \u7b97\u6cd5\u7684\u5f3a\u5927\u529f\u80fd\u878d\u5165\u5230\u4e00\u4e2a\u7d27\u51d1\u3001\u6613\u4e8e\u4f7f\u7528\u7684 npm \u5305\u4e2d\uff0c\u4f9b javascript \u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u3002 snowuuid \u751f\u6210\u7684\u6bcf\u4e2a id \u90fd\u662f 64 \u4f4d\u6574\u6570\uff0c\u5305\u542b\u6709\u5173\u65f6\u95f4\u6233\u3001\u6570\u636e\u4e2d\u5fc3\u3001worker \u548c\u5e8f\u5217\u7684\u4fe1\u606f\uff0c\u975e\u5e38\u9002\u5408\u6bcf\u4e2a\u670d\u52a1\u5668\u6216\u8fdb\u7a0b\u72ec\u7acb\u751f\u6210 id \u7684\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/173251282867440c3c4a388.jpg\" class=\"aligncenter\" title=\"SnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668\u63d2\u56fe\" alt=\"SnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668\u63d2\u56fe\" \/><\/p>\n<h3> snowuuid\u7684\u7279\u70b9 <\/h3>\n<ul>\n<li> <strong>\u65f6\u95f4\u987a\u5e8f id<\/strong>\uff1aid \u6309\u65f6\u95f4\u987a\u5e8f\u751f\u6210\uff0c\u786e\u4fdd\u5b83\u4eec\u53ef\u4ee5\u6309\u65f6\u95f4\u987a\u5e8f\u6392\u5e8f\u3002<\/li>\n<li> <strong>\u81ea\u5b9a\u4e49\u9009\u9879<\/strong>\uff1asnowuuid \u5141\u8bb8\u914d\u7f6e\u6570\u636e\u4e2d\u5fc3\u548c\u5de5\u4f5c\u4eba\u5458 id\uff0c\u652f\u6301\u8de8\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u4e0d\u540c\u8bbe\u7f6e\u3002<\/li>\n<li> <strong>\u9ad8\u541e\u5410\u91cf<\/strong>\uff1asnowuuid \u5177\u6709\u6bcf\u6beb\u79d2\u7684\u5e8f\u5217\u9650\u5236\uff0c\u53ef\u4ee5\u5904\u7406\u9ad8\u541e\u5410\u91cf\u8981\u6c42\uff0c\u9632\u6b62 id \u51b2\u7a81\u3002<\/li>\n<\/ul>\n<h3> snowuuid \u7684\u5de5\u4f5c\u539f\u7406 <\/h3>\n<p>snowuuid id \u7531\u591a\u4e2a\u6bb5\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li> <strong>\u65f6\u95f4\u6233<\/strong>\uff1a\u786e\u4fdd id \u6309\u65f6\u95f4\u987a\u5e8f\u6392\u5217\u3002<\/li>\n<li> <strong>\u6570\u636e\u4e2d\u5fc3id<\/strong>\uff1a\u533a\u5206\u4e0d\u540c\u7684\u6570\u636e\u4e2d\u5fc3\u3002<\/li>\n<li> <strong>\u5de5\u4f5c\u4eba\u5458 id<\/strong>\uff1a\u533a\u5206\u6570\u636e\u4e2d\u5fc3\u5185\u7684\u4e0d\u540c\u5de5\u4f5c\u4eba\u5458\u3002<\/li>\n<li> <strong>\u5e8f\u5217<\/strong>\uff1a\u786e\u4fdd\u53ef\u4ee5\u5728\u540c\u4e00\u6beb\u79d2\u5185\u521b\u5efa\u591a\u4e2a\u552f\u4e00id\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u6bcf\u4e2a\u6bb5\u7684\u4f4d\u5206\u5e03\u7ec6\u5206\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>segment<\/th>\n<th>bits allocated<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>timestamp<\/td>\n<td>41 bits<\/td>\n<\/tr>\n<tr>\n<td>datacenter id<\/td>\n<td>5 bits<\/td>\n<\/tr>\n<tr>\n<td>worker id<\/td>\n<td>5 bits<\/td>\n<\/tr>\n<tr>\n<td>sequence number<\/td>\n<td>12 bits<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3> \u7528\u6cd5 <\/h3>\n<p>\u4ece npm \u5b89\u88c5 snowuuid\uff1a<\/p>\n<pre>npm install snowuuid\n<\/pre>\n<p>\u8981\u4f7f\u7528 snowuuid \u751f\u6210\u552f\u4e00 id\uff0c\u8bf7\u5bfc\u5165\u5305\u5e76\u521d\u59cb\u5316\u65b0\u5b9e\u4f8b\uff1a<\/p>\n<pre>const { snowuuid } = require('snowuuid');\n\n\/\/ initialize snowuuid with options\nconst generator = new snowuuid({\n    epoch: 1609459200000n,  \/\/ starting from january 1, 2021\n    workerid: 1n,           \/\/ unique id for each worker\n    datacenterid: 1n        \/\/ unique id for each datacenter\n});\n\n\/\/ generate a unique id\nconst uniqueid = generator.nextid();\nconsole.log(uniqueid.tostring());\n<\/pre>\n<h3> \u81ea\u5b9a\u4e49snowuuid <\/h3>\n<p>snowuuid \u7684 workeroptions \u754c\u9762\u63d0\u4f9b\u53ef\u81ea\u5b9a\u4e49\u7684\u8bbe\u7f6e\u4ee5\u9002\u5e94\u60a8\u7684\u7cfb\u7edf\uff1a<\/p>\n<ul>\n<li> <strong>\u7eaa\u5143<\/strong>\uff1a\u81ea\u5b9a\u4e49\u5f00\u59cb\u65f6\u95f4\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\u3002<\/li>\n<li> <strong>workerid<\/strong>\uff1a\u6570\u636e\u4e2d\u5fc3\u5185\u6bcf\u4e2a\u5de5\u4f5c\u4eba\u5458\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/li>\n<li> <strong>datacenterid<\/strong>\uff1a\u6bcf\u4e2a\u6570\u636e\u4e2d\u5fc3\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002 <\/li>\n<\/ul>\n<pre>const generator = new snowuuid({\n    epoch: 1610000000000n,  \/\/ custom epoch\n    workerid: 3n,           \/\/ worker id\n    datacenterid: 2n        \/\/ datacenter id\n});\n<\/pre>\n<h3> \u5e95\u5c42\uff1a\u4ee3\u7801 <\/h3>\n<p>snowuuid \u7684\u6838\u5fc3\u662f nextid() \u51fd\u6570\uff0c\u5b83\u901a\u8fc7\u7ec4\u5408\u65f6\u95f4\u6233\u3001\u6570\u636e\u4e2d\u5fc3 id\u3001\u5de5\u4f5c id \u548c\u5e8f\u5217\u4f4d\u6765\u751f\u6210\u552f\u4e00 id\u3002\u5176\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li> <strong>\u65f6\u949f\u5904\u7406<\/strong>\uff1a\u8be5\u7b97\u6cd5\u786e\u4fdd\u7cfb\u7edf\u65f6\u949f\u59cb\u7ec8\u5411\u524d\u79fb\u52a8\u3002\u5982\u679c\u65f6\u949f\u5012\u9000\uff0c\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u4ee5\u9632\u6b62\u91cd\u590d id\u3002<\/li>\n<li> <strong>\u5e8f\u5217\u6ea2\u51fa\u5904\u7406<\/strong>\uff1a\u5f53\u4e00\u6beb\u79d2\u5185\u8fbe\u5230\u5e8f\u5217\u9650\u5236\u65f6\uff0csnowuuid \u4f1a\u7b49\u5f85\u4e0b\u4e00\u6beb\u79d2\u7ee7\u7eed\u751f\u6210 id\u3002<\/li>\n<\/ul>\n<h3> \u793a\u4f8b\u4ee3\u7801\uff1anextid \u51fd\u6570 <\/h3>\n<pre>nextId() {\n    let timestamp = SnowUUID.now();\n\n    if (timestamp &lt; this.#lastTimestamp) {\n        throw new Error(\n            `Clock moved backwards. Unable to generate ID for ${this.#lastTimestamp - timestamp} milliseconds.`\n        );\n    }\n\n    if (timestamp === this.#lastTimestamp) {\n        this.#sequence = (this.#sequence + 1n) &amp; SEQUENCE_MASK;\n        if (this.#sequence === 0n) {\n            timestamp = this.tilNextMillis(this.#lastTimestamp);\n        }\n    } else {\n        this.#sequence = 0n;\n    }\n\n    this.#lastTimestamp = timestamp;\n\n    return (\n        ((timestamp - this.#epoch) &lt;&lt; DEFAULT_TIMESTAMP_LEFT_SHIFT) |\n        (this.#datacenterId &lt;&lt; DEFAULT_DATACENTER_ID_SHIFT) |\n        (this.#workerId &lt;&lt; DEFAULT_WORKER_ID_SHIFT) |\n        this.#sequence\n    );\n}\n<\/pre>\n<h3> \u7ed3\u8bba <\/h3>\n<p>snowuuid \u4e3a\u5206\u5e03\u5f0f uuid \u751f\u6210\u63d0\u4f9b\u4e86\u5f3a\u5927\u3001\u53ef\u5b9a\u5236\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8de8\u591a\u4e2a\u7cfb\u7edf\u7684\u9ad8\u53ef\u6269\u5c55\u6027\u548c\u552f\u4e00 id \u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u5206\u6790\u3001\u6d88\u606f\u4f20\u9012\u8fd8\u662f\u5fae\u670d\u52a1\uff0csnowuuid \u90fd\u80fd\u786e\u4fdd\u552f\u4e00\u7684\u3001\u6309\u65f6\u95f4\u6392\u5e8f\u7684\u6807\u8bc6\u7b26\uff0c\u5e2e\u52a9\u60a8\u5145\u6ee1\u4fe1\u5fc3\u5730\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<p>\u5728 github \u4e0a\u63a2\u7d22 snowuuid\uff1asnowuuid \u5b58\u50a8\u5e93<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u7535\u8111\u7248\u9b42\u6597\u7f57\u600e\u4e48\u9009\u53cc\u4eba\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>SnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aSnowUUID \u7b80\u4ecb\uff1a\u53d7 Snowflake \u542f\u53d1\u7684\u5206\u5e03\u5f0f UUID \u751f\u6210\u5668\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u786e\u4fdd\u8de8\u591a\u4e2a\u670d\u52a1\u5668\u6216\u8fdb\u7a0b\u7684\u552f\u4e00 id \u53ef\u80fd\u5177\u6709\u6311\u6218\u6027\u3002 id \u5fc5\u987b\u662f\u552f\u4e00\u7684\u3001\u5feb\u901f\u751f\u6210\u7684\uff0c\u5e76\u4e14\u6709\u65f6\u8fd8\u4f20\u8fbe\u6709\u5173\u5176\u6765\u6e90\u7684\u4fe1\u606f\u3002\u53d7 twitter \u7684 snowflake id \u751f\u6210\u7b97\u6cd5\u7684\u542f\u53d1\uff0c\u6211\u5f00\u53d1\u4e86 snowuuid\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e13\u4e3a node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5206\u5e03\u5f0f uuid \u751f\u6210\u5668\u3002 snowuuid \u7ed3\u5408\u4e86\u7cbe\u5ea6\u548c\u53ef\u6269\u5c55\u6027\uff0c\u751f\u6210\u72ec\u7279\u7684\u3001\u6309\u65f6\u95f4\u6392\u5e8f\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u9488\u5bf9\u4efb\u4f55\u5206\u5e03\u5f0f\u8bbe\u7f6e\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002 \u4e3a\u4ec0\u4e48\u9009\u62e9 snowuuid\uff1f snowuuid \u5c06 twitter snowflake \u7b97\u6cd5\u7684\u5f3a\u5927\u529f\u80fd\u878d\u5165\u5230\u4e00\u4e2a\u7d27\u51d1\u3001\u6613\u4e8e\u4f7f\u7528\u7684 npm \u5305\u4e2d\uff0c\u4f9b javascript \u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u3002 snowuuid \u751f\u6210\u7684\u6bcf\u4e2a id \u90fd\u662f 64 \u4f4d\u6574\u6570\uff0c\u5305\u542b\u6709\u5173\u65f6\u95f4\u6233\u3001\u6570\u636e\u4e2d\u5fc3\u3001worker \u548c\u5e8f\u5217\u7684\u4fe1\u606f\uff0c\u975e\u5e38\u9002\u5408\u6bcf\u4e2a\u670d\u52a1\u5668\u6216\u8fdb\u7a0b\u72ec\u7acb\u751f\u6210 id \u7684\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002 snowuuid\u7684\u7279\u70b9 \u65f6\u95f4\u987a\u5e8f id\uff1aid \u6309\u65f6\u95f4\u987a\u5e8f\u751f\u6210\uff0c\u786e\u4fdd\u5b83\u4eec\u53ef\u4ee5\u6309\u65f6\u95f4\u987a\u5e8f\u6392\u5e8f\u3002 \u81ea\u5b9a\u4e49\u9009\u9879\uff1asnowuuid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52986","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52986"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52986\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}