{"id":1608,"date":"2017-07-07T14:58:42","date_gmt":"2017-07-07T06:58:42","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1608"},"modified":"2017-07-07T14:58:42","modified_gmt":"2017-07-07T06:58:42","slug":"%e9%ab%98%e5%8f%af%e7%94%a8%e9%ab%98%e6%80%a7%e8%83%bd%e5%8f%af%e6%89%a9%e5%b1%95%e7%9a%84%e5%8d%95%e5%8f%b7%e7%94%9f%e6%88%90%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1608","title":{"rendered":"\u9ad8\u53ef\u7528\u9ad8\u6027\u80fd\u53ef\u6269\u5c55\u7684\u5355\u53f7\u751f\u6210\u65b9\u6848"},"content":{"rendered":"<p>\n\t\u5728\u4e1a\u52a1\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5355\u53f7\u751f\u6210\uff0c \u4f8b\u5982\u5feb\u9012\u5355\u53f7\u3001\u670d\u52a1\u5355\u53f7\u3001\u8ba2\u5355\u53f7\u7b49\u7b49\u3002 \u8fd9\u4e9b\u5355\u53f7\u751f\u6210\u5f80\u5f80\u662f\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u7684\u7b2c\u4e00\u6b65\uff0c \u5355\u53f7\u751f\u6210\u51fa\u95ee\u9898\uff0c\u5fc5\u7136\u5bfc\u81f4\u4e1a\u52a1\u8d70\u4e0d\u4e0b\u53bb\uff1b\u53e6\u5916\u6709\u591a\u5c11\u4e1a\u52a1\u91cf\u5c31\u4f1a\u81f3\u5c11\u6709\u591a\u5c11\u7684\u5355\u53f7\u751f\u6210\u9700\u6c42\u3002\u6240\u4ee5\u5355\u53f7\u751f\u6210\u5fc5\u987b\u9ad8\u53ef\u7528\uff0c\u5fc5\u987b\u9ad8\u6027\u80fd\u3002 \u53e6\u5916\u4e1a\u52a1\u4e0d\u540c\u9700\u8981\u7684\u5355\u53f7\u89c4\u5219\u53ef\u80fd\u4e5f\u4e0d\u76f8\u540c\uff0c \u6240\u4ee5\u5355\u53f7\u670d\u52a1\u8fd8\u5fc5\u987b\u5177\u5907\u8db3\u591f\u7684\u6269\u5c55\u6027\u3002\n<\/p>\n<h2>\n\t\u5355\u53f7\u5b9a\u4e49<br \/>\n<\/h2>\n<h2>\n\t\u5728\u8fdb\u5165\u6b63\u9898\u4e4b\u524d\u6211\u4eec\u5148\u7ed9\u5355\u53f7\u4e0b\u4e2a\u5b9a\u4e49\uff0c \u770b\u51e0\u4e2a\u5e38\u89c1\u7684\u5355\u53f7\u5f62\u5f0f\u3002<br \/>\n<\/h2>\n<p>\n\t\u5355\u53f7\u662f\u4e00\u4e2a\u6570\u5b57\u548c\u5b57\u7b26\u7ec4\u6210\u7684\u5e8f\u5217\uff0c \u5b83\u8981\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a \u4e00\u4e2a\u662f\u552f\u4e00\uff0c \u4fdd\u8bc1\u552f\u4e00\u624d\u53ef\u4ee5\u4f5c\u4e3a\u4e1a\u52a1\u6807\u8bc6; \u53e6\u4e00\u4e2a\u662f\u7b26\u5408\u4e1a\u52a1\u9700\u8981\u7684\u89c4\u5219\u3002 \u4f8b\u5982\u4e0b\u9762\u4e09\u4e2a\u5355\u53f7\uff1a\n<\/p>\n<ul>\n<li>\n\t\t2017030400001 \u8fd9\u4e2a\u5355\u53f7\u7531\u4e24\u4e2a\u90e8\u5206\u5e8f\u5217\u53f7\u65e5\u671f20170304+\u5b9a\u957f5\u4f4d\u88650\u6570\u5b5700001\u3002\n\t<\/li>\n<li>\n\t\t010-6541-00001 \u6b64\u5355\u53f7\u5206\u4e09\u90e8\u5206\uff0c \u4e2d\u95f4\u7528\u51cf\u53f7\u8fde\u63a5\uff0c \u7b2c\u4e00\u90e8\u5206\u4e3a\u533a\u53f7\uff0c \u7b2c\u4e8c\u90e8\u5206\u4e3a\u4f5c\u4e1a\u5355\u4f4d\u53f7\u7801\uff0c \u7b2c\u4e09\u90e8\u5206\u4e3a\u4f5c\u4e1a\u5355\u4f4d\u4ea7\u751f\u4f5c\u4e1a\u7684\u5e8f\u53f7\u3002\n\t<\/li>\n<li>\n\t\tQJ000001 \u5219\u662f\u7531\u5b57\u7b26QJ\u5f00\u5934\u540e\u9762\u8ddf\u968f\u6570\u5b57\u5e8f\u5217\u7684\u5355\u53f7\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u5355\u53f7\u6570\u5b57\u5e8f\u5217\u90e8\u5206\u7684\u751f\u6210<br \/>\n<\/h2>\n<p>\n\t\u4e0a\u8ff0\u5355\u53f7\u5b9a\u4e49\u4e2d\u7684\u6570\u5b57\u90e8\u5206\u901a\u5e38\u662f\u4e00\u4e2a\u81ea\u589e\u7684\u6570\u5b57\u5e8f\u5217\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5e93\u7684\u81ea\u589e\u5217\u3001 \u6570\u636e\u5e93\u7684\u5217+1\u65b9\u5f0f\u3001 redis\u6216\u8005memcached\u7684INCR\u6307\u4ee4\u6765\u751f\u6210\u8fd9\u79cd\u6570\u5b57\u7684\u5e8f\u5217\u3002 \u8fd9\u56db\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u751f\u6210\u5e8f\u5217\uff0c \u4f46\u5404\u81ea\u6709\u5404\u81ea\u7684\u597d\u5904\u3002\n<\/p>\n<h2>\n\t\u6570\u636e\u5e93\u81ea\u589e\u5217\u7684\u65b9\u5f0f<br \/>\n<\/h2>\n<p>\n\t\u662f\u901a\u8fc7\u6570\u636e\u5e93\u7684\u5185\u90e8\u673a\u5236\u751f\u6210\u7684\uff0c \u5728\u666e\u901aPC\u4e0a\u6bcf\u79d2\u7ea6\u53ef\u4ee5\u751f\u62104000\u4e2a\u6570\u5b57\u5e8f\u5217\uff0c \u5b83\u7684\u597d\u5904\u662f\u6bcf\u4e00\u4e2a\u6570\u5b57\u5e8f\u5217\u90fd\u4f1a\u4fdd\u7559\u4e00\u6761\u8bb0\u5f55\uff0c \u8bb0\u5f55\u751f\u6210\u4f7f\u7528\u65f6\u95f4\uff0c \u7f3a\u70b9\u662f\u541e\u5410\u91cf\u4e00\u822c\uff0c \u4f1a\u5360\u7528\u4e00\u5b9a\u7684\u6570\u636e\u5e93\u8d44\u6e90\uff0c \u5982\u4e0b\u662f\u4e00\u79cd\u63a8\u8350\u7684\u8868\u7ed3\u6784\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\nCREATE TABLE `xx_code_sequence` (\r\n   `id` bigint(20) NOT NULL AUTO_INCREMENT,\r\n   `generate_time` timestamp NOT NULL\r\n      default CURRENT_TIMESTAMP,\r\n   PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULTCHARSET=utf8;<\/pre>\n<p>\n\t\u6b64\u8868\u6709\u4e24\u5217\uff0c id\u5217\u4e3abigint\u7c7b\u578b\u7684\u81ea\u589e\u957f\u5b57\u6bb5\uff0c\u4f5c\u4e3a\u6570\u5b57\u5e8f\u5217\u7684\u503c\uff0c generate_time\u65f6\u95f4\u6233\u5b57\u6bb5\u53ef\u4ee5\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u5355\u53f7\u7684\u751f\u6210\u65f6\u95f4\u3002\u751f\u6210\u6570\u5b57\u5e8f\u5217\u7684\u65b9\u5f0f\u7528sql\u8bf4\u660e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\nbegin trans;\r\ninsert into `xx_code_sequence`(generate_time)values(current_timestamp);\r\nselect last_insert_id();\r\ncommit;<\/pre>\n<p>\n\t\u8bf4\u660e\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u8868\u540d\u683c\u5f0fxx_code_sequence\uff0csto_code_0\u5206\u4e3a\u4e09\u90e8\u5206sto\u4e3aownerKey\uff0c code\u56fa\u5b9a\u4e0d\u53d8\uff0c0\u8868\u793a\u8868\u7684\u5e8f\u53f7\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u4e0b\u6807\u4e0d\u540c\u8868\u6765\u652f\u6301\u66f4\u9ad8\u7684\u5e76\u53d1\uff0c\u5171\u6709\u51e0\u4e2a\u8868\u9700\u8981\u5728\u5f00\u59cb\u786e\u8ba4\u4e86\uff0c\u786e\u8ba4\u7684\u4f9d\u636e\u662f\u9700\u8981\u6ee1\u8db3\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u8868\u7684\u4e2a\u6570\u5fc5\u987b\u662f2\u7684n\u6b21\u65b9\uff0c\u4f8b\u59821\uff0c 2\uff0c 4\uff0c 8\uff0c16\uff1b\n\t<\/li>\n<li>\n\t\t`id` \u5373\u5e8f\u5217\u7684\u90e8\u5206\u503c\uff0c\u662f\u901a\u8fc7mysql\u7684\u81ea\u589e\u7279\u6027\u751f\u6210\u7684\uff0c\u6700\u7ec8\u7684\u5e8f\u5217\u503c\u662fid\u548c\u8868\u5e8f\u53f7\u5171\u540c\u7ec4\u6210\u7684\uff0c\u5047\u5b9a\u67094\u4e2a\u8868\uff0c\u5e8f\u53f7\u5206\u522b\u4e3a0\uff0c1\uff0c2\uff0c3\uff1b\n\t<\/li>\n<li>\n\t\t\u90a3\u4e48\u5e8f\u5217\u503c\u4e3a id&lt;&lt; 2 | table_index; \u5373id\u5411\u5de6\u79fb\u4f4d2\u4f4d\uff08\u79fb\u4f4d\u51e0\u4f4d\u53d6\u51b3\u4e8e\u8868\u7684\u4e2a\u6570\uff09\uff0c\u7136\u540e\u548c\u8868\u5e8f\u53f7\u6c42\u6216\uff1b`generate_time` \u4e3aid\u751f\u6210\u65f6\u95f4\uff0c\u65e0\u5176\u4ed6\u542b\u4e49\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u4e0d\u540c\u5e8f\u53f7\u7684\u8868\u53ef\u4ee5\u5efa\u5728\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e0a\uff0c\u5f53\u67d0\u4e2a\u5e8f\u53f7\u7684\u8868\u4e0d\u53ef\u7528\u65f6\u8981\u62a5\u8b66\uff0c\u5e76\u5207\u6362\u5230\u5176\u4ed6\u8868\u4e0a\u751f\u6210\u6570\u5b57\u5e8f\u5217\u3002\n<\/p>\n<h2>\n\t\u6570\u636e\u5e93\u7684\u5217+1\u65b9\u5f0f<br \/>\n<\/h2>\n<p>\n\t\u901a\u8fc7\u5bf9\u6570\u636e\u5e93\u7684\u67d0\u5217\u505a+1\u64cd\u4f5c\uff0c \u6765\u5f97\u5230\u552f\u4e00\u7684\u6570\u5b57\u5e8f\u5217\uff0c \u662f\u901a\u8fc7\u6570\u636e\u5e93\u7684\u884c\u9501\u6765\u4fdd\u969c\u552f\u4e00\u7684\uff0c \u56e0\u4e3a\u6d89\u53ca\u5230\u884c\u9501\uff0c \u6240\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u751f\u6210\u5e8f\u5217\u7684\u5355\u884c\u541e\u5410\u91cf\u4e0d\u4f1a\u592a\u5927\uff0c \u9002\u5408\u9700\u8981\u751f\u6210\u591a\u79cd\uff08\u6bcf\u4e00\u79cd\u653e\u5230\u4e00\u884c\uff09\u4e0d\u540c\u6570\u5b57\u751f\u6210\u9700\u6c42\u3002 \u5982\u4e0b\u662f\u4e00\u79cd\u63a8\u8350\u7684\u8868\u7ed3\u6784\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\ncreate table `xx_rowbased_sequence` (\r\n   `owner_key` varchar(32) NOT NULL,\r\n   `current_value` bigint NOT NULL,\r\n   PRIMARY KEY (`owner_key`)\r\n);<\/pre>\n<p>\n\t\u8868\u4e2d\u7684ownerKey\u5217\u4e3a\u5355\u53f7\u79cd\u7c7b\u6807\u8bc6\uff0c current_value\u4e3a+1\u64cd\u4f5c\u5217\u3002\u751f\u6210\u5e8f\u5217\u7684\u65b9\u5f0f\u7528sql\u8bf4\u660e\u5982\u4e0b\u3002\n<\/p>\n<pre class=\"brush:sql;\">\r\nbegin trans;\r\nUPDATE  `xx_rowbased_sequence`SET current_value=current_value+1 WHERE owner_key=&rsquo;order-no&rsquo;;\r\nSELECT current_value FROM `xx_rowbased_sequence` WHERE owner_key=&rsquo;order-no&rsquo;;\r\ncommit;<\/pre>\n<p>\n\t\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u6b64\u65b9\u5f0f\u751f\u6210\u6570\u5b57\u5e8f\u5217\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u9700\u8981\u662fRR\u3002\n<\/p>\n<h2>\n\t\u4f7f\u7528redis\/memcached\u7684INCR\u6307\u4ee4\u65b9\u5f0f<br \/>\n<\/h2>\n<p>\n\tredis\/memcached\u672c\u8eab\u53ef\u4ee5\u4fdd\u8bc1\u751f\u6210\u6570\u5b57\u7684\u552f\u4e00\u6027\uff0c\u548c\u9ad8\u6027\u80fd\u3002\u5355\u4e00redis\u670d\u52a1\u5668\u6bcf\u79d2\u53ef\u4ee5\u751f\u6210\u7ea66w\u5de6\u53f3\u7684\u6570\u5b57\u5e8f\u5217\u3002 \u4f46\u9700\u8981\u6ce8\u610fredis\u5fc5\u987b\u914d\u7f6e\u4e3b\u4ece\u548c\u5b58\u50a8\uff0c \u4ee5\u907f\u514d\u5728\u6781\u7aef\u60c5\u51b5\u4e0bredis\u8282\u70b9down\u673a\uff0c \u5bfc\u81f4\u4e22\u5931\u5e8f\u5217\u6216\u5e8f\u5217\u91cd\u590d\u3002\n<\/p>\n<p>\n\t\u9ad8\u53ef\u7528\u5b9e\u73b0<br \/>\n\t\u4e0a\u9762\u4ecb\u7ecd\u4e864\u79cd\u751f\u6210\u6570\u5b57\u5e8f\u5217\u7684\u65b9\u5f0f\uff0c \u4f46\u8981\u4fdd\u8bc1\u9ad8\u53ef\u7528\uff0c \u5355\u9760\u4e00\u79cd\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\u8fd8\u662f\u4e0d\u591f\u7684\uff0c \u6211\u4eec\u8fd8\u9700\u8981\u4e00\u79cd\u9ad8\u53ef\u7528\u7684\u5b9e\u73b0\u3002\n<\/p>\n<p>\n\t\u9ad8\u53ef\u7528\u6570\u5b57\u5e8f\u5217\u751f\u6210\u5668\u5185\u90e8\u662f2\u7684n\u6b21\u65b9\u4e2a\u5e95\u5c42\u6570\u5b57\u5e8f\u5217\u751f\u6210\u5668\uff0c \u6bcf\u4e2a\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\u5bf9\u5e94\u4e00\u4e2a\u4e0b\u6807\u503c\uff0c \u4e0b\u6807\u503c\u7684\u8303\u56f4\u4e3a[0, 2n-1]\u3002 \u5728\u751f\u6210\u5e8f\u5217\u65f6\uff0c \u8f6e\u8be2\u5e95\u5c42\u751f\u6210\u5668\uff0c \u5982\u679c\u6b63\u5e38\uff0c \u5219\u5c06\u751f\u6210\u7ed3\u679c\u5411\u5de6\u79fbn\u4f4d\uff0c \u5e76\u4e0e\u5f53\u524d\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\u4e0b\u6807\u53d6\u6216\u5f97\u5230\u6700\u7ec8\u5e8f\u5217\u503c\u3002 \u5982\u679c\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\u53d1\u751f\u5f02\u5e38\uff0c \u5219\u5c06\u5176\u6807\u8bb0\u4e3a\u4e0d\u53ef\u7528\uff0c \u5e76\u8f6e\u8be2\u4e0b\u4e00\u4e2a\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\uff0c \u76f4\u5230\u6210\u529f\u3002\n<\/p>\n<p>\n\t\u9ad8\u53ef\u7528\u5b9e\u73b0\u7c7bcom.xttblog.BitwiseLoadBalanceSequenceGen\uff0c\u5176\u5185\u90e8\u6709x\u4e2a\u5e95\u5c42SequenceGen\u5b9e\u73b0\uff0c\u6b64\u7c7b\u4f1a\u8f6e\u8be2\u7684\u8c03\u7528\u5e95\u5c42SequenceGen\u6765\u751f\u6210\u5e8f\u5217\uff0c\u5982\u679c\u67d0\u4e2a\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u51fa\u9519\uff0c\u4f1a\u4ece\u53ef\u7528\u5217\u8868\u4e2d\u79fb\u9664\u6389\uff0c\u88ab\u79fb\u9664\u6389\u7684\u5e95\u5c42SequenceGen\u5728\u8fc7xx\u65f6\u95f4\uff08\u9ed8\u8ba4\u4e3a5\u5206\u949f\uff09\u540e\uff0c\u53ef\u4ee5\u91cd\u65b0\u52a0\u5165\u5230\u53ef\u7528\u5217\u8868\u4e2d\u3002\u5982\u679c\u5185\u90e8\u5e8f\u5217\u751f\u6210\u5355\u4e2a\u5e8f\u5217\u65f6\u95f4\u8d85\u65f6\uff0c\u5e76\u5728\u6700\u8fd1n\u65f6\u95f4\u5185\u8fde\u7eed\u8d85\u65f6x\u6b21\uff0c\u4f1a\u88ab\u79fb\u52a8\u5230\u5f02\u5e38\u5217\u8868\uff0c\u5728\u5f02\u5e38\u5217\u8868\u4e2d\u65f6\u95f4\u8d85\u8fc7xx\u65f6\u95f4\u540e\uff0c\u4e5f\u4f1a\u88ab\u91cd\u65b0\u653e\u5165\u53ef\u7528\u5217\u8868\u4e2d\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u5982\u679c\u4e00\u4e2a\u5e95\u5c42\u5e8f\u5217\u88ab\u6807\u8bb0\u4e3a\u4e0d\u53ef\u7528\uff0c \u8fc7\u914d\u7f6e\u65f6\u95f4\u540e\u4f1a\u5c06\u5176\u6062\u590d\u5230\u53ef\u7528\u5217\u8868\u4e2d\uff0c \u81ea\u52a8\u6062\u590d\u673a\u5236\u53ef\u4ee5\u907f\u514d\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\u5df2\u6062\u590d\u53ef\u7528\uff0c \u800c\u7a0b\u5e8f\u5374\u4e00\u76f4\u4e0d\u4f7f\u7528\u6b64\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5668\u7684\u60c5\u51b5\u3002\n<\/p>\n<p>\n\t\u9ad8\u53ef\u7528\u5b9e\u73b0\u7684\u5185\u90e8\u7ed3\u6784\u56fe\uff0c \u5982\u4e0b\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u9ad8\u53ef\u7528\u5e8f\u5217\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/640.png\" style=\"width: 337px;height: 294px\" \/>\n<\/p>\n<p>\n\t\u5176\u6838\u5fc3\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic long gen(String ownerKey){\r\n    long sequence=0;\r\n    int currentPartitionIndex=-1;\r\n    SequenceGen innerGen=null;\r\n    do{\r\n        long startTime=System.currentTimeMillis();\r\n        boolean hasError=false;\r\n        try{\r\n            currentPartitionIndex=getCurrentPartitionIndex(ownerKey);\r\n            LOGGER.trace(&quot;current partition index {}&quot;,currentPartitionIndex);\r\n            innerGen=innerSequences.get(currentPartitionIndex);\r\n            if(innerGen==SkipSequence.INSTANCE){\r\n                LOGGER.warn(&quot;current partition index {} is skipped&quot;,currentPartitionIndex);\r\n                if(availablePartitionIndices.contains(currentPartitionIndex)){\r\n                    LOGGER.warn(&quot;current partition index {} is skipped, remove it&quot;,currentPartitionIndex);\r\n                    availablePartitionIndices.remove(Integer.valueOf(currentPartitionIndex));\r\n                }\r\n\r\n                continue;\r\n            }\r\n\r\n            HighAvailablePartitionHolder.setPartition(currentPartitionIndex);\r\n            sequence=innerGen.gen(ownerKey);\r\n            onGenNewId(ownerKey,currentPartitionIndex,sequence);\r\n            LOGGER.trace(&quot;genNewId {} with inner {}&quot;,sequence,currentPartitionIndex);\r\n            break;\r\n        }catch(SequenceOutOfRangeException ex){\r\n            LOGGER.error(&quot;gen error SequenceOutOfRangeException index {} total available {}&quot;,\r\n                    currentPartitionIndex,\r\n                    availablePartitionIndices.size());\r\n            hasError=true;\r\n\r\n            LOGGER.error(&quot;set {} to SKIP&quot;,currentPartitionIndex);\r\n            this.innerSequences.set(currentPartitionIndex,SkipSequence.INSTANCE);\r\n            onError(ownerKey,currentPartitionIndex,innerGen,ex);\r\n            LOGGER.error(&quot;after onError total available {}\/{}&quot;,currentPartitionIndex,\r\n                    availablePartitionIndices.size());\r\n\r\n        }catch(Exception ex){\r\n            LOGGER.error(&quot;gen error index {} total available {}&quot;,currentPartitionIndex,\r\n                    availablePartitionIndices.size());\r\n            LOGGER.error(&quot;gen error &quot;,ex);\r\n            hasError=true;\r\n            onError(ownerKey,currentPartitionIndex,innerGen,ex);\r\n            LOGGER.error(&quot;after onError total available {}\/{}&quot;,currentPartitionIndex,\r\n                    availablePartitionIndices.size());\r\n        }finally{\r\n            long usedTime=System.currentTimeMillis()-startTime;\r\n            boolean isTimeout=usedTime&gt;timeoutThresholdInMilliseconds;\r\n            if(!hasError&amp;&amp;isTimeout){\r\n                onTimeout(currentPartitionIndex,innerGen,usedTime);\r\n            }\r\n            LOGGER.trace(&quot;gen usedTime {}&quot;,usedTime);\r\n        }\r\n    }while(true);\r\n    return sequence;\r\n}<\/pre>\n<p>\n\t\u4f7f\u7528\u65f6\u914d\u7f6ebean\u4f7f\u7528\u5373\u53ef\uff0c \u5982\u4e0bspring bean xml\u914d\u7f6e\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;highAvailableSequenceGen&quot; class=&quot;com.xttblog.BitwiseLoadBalanceSequenceGen&quot;&gt;\r\n      &lt;!-- \u6307\u5b9a\u9ad8\u53ef\u7528\u5e8f\u5217\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u5e8f\u5217\u540e\u5411\u5de6\u79fb\u4f4d\u4f4d\u6570--&gt;\r\n    &lt;constructor-arg index=&quot;0&quot; value=&quot;2&quot;\/&gt;\r\n     &lt;!-- \u6307\u5b9a\u5e95\u5c42\u5e8f\u5217 --&gt;\r\n    &lt;constructor-arg index=&quot;1&quot;&gt;\r\n        &lt;map&gt;\r\n            &lt;!-- key \u4e3a\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u503c\u5de6\u79fb\u4f4d\u540e\u6216\u7684\u4e0b\u6807--&gt;\r\n        &lt;entry key=&quot;0&quot;&gt;\r\n                &lt;bean class=&quot;com.xttblog.AutoIncrementTablesSequenceGen&quot;&gt;\r\n                    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSourceA&quot;\/&gt;\r\n                    &lt;property name=&quot;sequenceTableFormat&quot; value=&quot;%s_code_%d&quot;\/&gt;\r\n                &lt;\/bean&gt;\r\n            &lt;\/entry&gt;\r\n            &lt;entry key=&quot;1&quot;&gt;\r\n                &lt;bean class=&quot;com.xttblog.AutoIncrementTablesSequenceGen&quot;&gt;\r\n                    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSourceB&quot;\/&gt;\r\n                    &lt;property name=&quot;sequenceTableFormat&quot; value=&quot;%s_code_%d&quot;\/&gt;\r\n                &lt;\/bean&gt;\r\n            &lt;\/entry&gt;\r\n            &lt;entry key=&quot;2&quot;&gt;\r\n                &lt;bean class=&quot;com.xttblog.AutoIncrementTablesSequenceGen&quot;&gt;\r\n                    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSourceA&quot;\/&gt;\r\n                    &lt;property name=&quot;sequenceTableFormat&quot; value=&quot;%s_code_%d&quot;\/&gt;\r\n                &lt;\/bean&gt;\r\n            &lt;\/entry&gt;\r\n            &lt;entry key=&quot;3&quot;&gt;\r\n                &lt;bean class=&quot;com.xttblog.AutoIncrementTablesSequenceGen&quot;&gt;\r\n                    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSourceB&quot;\/&gt;\r\n                    &lt;property name=&quot;sequenceTableFormat&quot; value=&quot;%s_code_%d&quot;\/&gt;\r\n                &lt;\/bean&gt;\r\n            &lt;\/entry&gt;\r\n        &lt;\/map&gt;\r\n    &lt;\/constructor-arg&gt;\r\n    &lt;!-- \u5c06timeout\u5224\u65ad\u7684\u9608\u503c\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u5f88\u5927\u7684\u503c\uff0c \u907f\u514dtimeout\u5e94\u7528error\u7684\u60c5\u51b5\u53d1\u751f--&gt;\r\n   &lt;property name=&quot;timeoutThresholdInMilliseconds&quot; value=&quot;200&quot;\/&gt;\r\n    &lt;!-- \u8d85\u65f6\u591a\u5c11\u6b21\u540e\u4f1a\u79fb\u51fa\u53ef\u7528\u5217\u8868 --&gt;\r\n   &lt;property name=&quot;timeoutEventCountThreshold&quot; value=&quot;3&quot;\/&gt;\r\n    &lt;!-- \u8ba1\u7b97\u8d85\u65f6\u5f02\u5e38\u7684\u65f6\u95f4\u5468\u671f\uff0c \u4ee5\u79d2\u4e3a\u5355\u4f4d --&gt;\r\n   &lt;property name=&quot;timeoutTimeThresholdInSeconds&quot; value=&quot;60&quot; \/&gt;\r\n    &lt;!-- \u79fb\u5230\u4e0d\u53ef\u7528\u961f\u5217\u591a\u957f\u65f6\u95f4\u540e\u4f1a\u88ab\u91cd\u65b0\u653e\u5165\u53ef\u7528\u961f\u5217 --&gt;\r\n   &lt;property name=&quot;onErrorRescueThresholdInSeconds&quot; value=&quot;2000&quot;\/&gt;\r\n&lt;\/bean&gt;<\/pre>\n<h2>\n\t\u9ad8\u6027\u80fd\u5b9e\u73b0<br \/>\n<\/h2>\n<p>\n\t\u5355\u53f7\u751f\u6210\u53ea\u662f\u4e1a\u52a1\u64cd\u4f5c\u7684\u7b2c\u4e00\u4e2a\u6b65\u9aa4\uff0c \u4e1a\u52a1\u64cd\u4f5c\u5f80\u5f80\u662f\u590d\u6742\u8017\u65f6\u7684\uff0c \u6211\u4eec\u5fc5\u987b\u4fdd\u8bc1\u5355\u53f7\u751f\u6210\u7684\u6027\u80fd\uff0c \u4f7f\u5176\u51e0\u4e4e\u4e0d\u4f1a\u5f71\u54cd\u4e1a\u52a1\u65f6\u95f4\u3002\n<\/p>\n<p>\n\t\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u56db\u79cd\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\u90fd\u662f\u8de8\u7f51\u7edc\u901a\u8fc7\u4e2d\u95f4\u4ef6\u83b7\u5f97\u7684\u5e8f\u5217\u53f7\uff0c\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u5176\u6027\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5e8f\u5217\u653e\u5728\u79bbCPU\u66f4\u8fd1\u7684\u5730\u65b9\u2015\u2015\u5185\u5b58\u4e2d\u3002\u6211\u4eec\u4f7f\u7528\u5982\u4e0b\u4e24\u79cd\u65b9\u5f0f\u5c06\u6570\u5b57\u5e8f\u5217\u653e\u5230CPU\u66f4\u8fd1\u7684\u5730\u65b9\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u5c06\u5185\u90e8\u5e8f\u5217\u503c\u5411\u5de6\u79fb\u4f4dn\u4f4d\uff0c \u7136\u540e\u5e8f\u5217\u7684\u6700\u53f3n\u4f4d\u5728\u5185\u5b58\u751f\u6210\uff0c\u4e00\u6b21\u751f\u62102\u7684n\u6b21\u65b9\u4e2a\u6570\u5b57\u5e8f\u5217\uff0c \u7136\u540e\u653e\u5728\u5185\u5b58\u961f\u5217\u4e2d\uff1b\n\t<\/li>\n<li>\n\t\t\u5f02\u6b65\u63d0\u524d\u751f\u6210\uff1a\u5b9e\u65f6\u8ba1\u7b97\u5e8f\u5217\u53f7\u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u901f\u5ea6\uff0c \u7136\u540e\u5728\u5f02\u6b65\u7ebf\u7a0b\uff08\u6c60\uff09\u4e2d\u751f\u6210\u6700\u8fd1x ms\u9700\u8981\u7684\u5e8f\u5217\uff0c\u653e\u5165\u5185\u5b58\u961f\u5217\u4e2d\u5907\u7528\n\t<\/li>\n<\/ul>\n<p>\n\t\u8fd9\u4e24\u79cd\u65b9\u5f0f\u5e76\u4e0d\u4e00\u5b9a\u90fd\u9700\u8981\uff0c &nbsp;\u7f6e\u653e\u5165\u5185\u5b58\u961f\u5217\u4e2d\u7684\u6570\u5b57\u5e8f\u5217\u8d8a\u591a\uff0c\u91cd\u542f\u65f6\u4e22\u5931\u7684\u4e5f\u4f1a\u8d8a\u591a\u3002\n<\/p>\n<p>\n\t\u5176\u5185\u90e8\u7ed3\u6784\u56fe\u793a\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u9ad8\u6027\u80fd\u5e8f\u5217\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/20170707144445.png\" style=\"width: 357px;height: 323px\" \/>\n<\/p>\n<p>\n\t\u9ad8\u6027\u80fd\u5e8f\u5217\u4f7f\u7528\u7684bean\u914d\u7f6e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean class=&quot;com.xttblog.QueuedSequenceGen&quot; id=&quot;queuedSequenceGen&quot; init-method=&quot;start&quot; destroy-method=&quot;stop&quot;&gt;\r\n    &lt;!-- \u6307\u5b9a\u5185\u90e8\u5e8f\u5217\uff0c \u901a\u5e38\u662f\u4e00\u4e2a\u9ad8\u53ef\u7528\u7684\u5185\u90e8\u5e8f\u5217--&gt;\r\n    &lt;constructor-arg index=&quot;0&quot; ref=&quot;haSequenceGen&quot; \/&gt;\r\n    &lt;!-- \u6307\u5b9a\u5185\u5b58\u4e2d\u751f\u6210\u7684bit\u4f4d\u6570--&gt;\r\n    &lt;property name=&quot;memoryBitLength&quot; value=&quot;3&quot;\/&gt;\r\n    &lt;!--\u5f02\u6b65\u751f\u6210\u914d\u7f6e--&gt;\r\n    &lt;property name=&quot;enableAsync&quot; value=&quot;true&quot;\/&gt;\r\n    &lt;property name=&quot;asyncTask&quot;&gt;\r\n        &lt;bean class=&quot;com.xttblog.QueuedSequenceGen$AsyncTask&quot;&gt;\r\n            &lt;constructor-arg index=&quot;0&quot; ref=&quot;queuedSequenceGen&quot;\/&gt;\r\n            &lt;property name=&quot;loopSleepInterval&quot; value=&quot;20&quot;\/&gt;\r\n            &lt;property name=&quot;reserveTimeInMilliseconds&quot; value=&quot;10&quot;\/&gt;\r\n        &lt;\/bean&gt;\r\n    &lt;\/property&gt;\r\n    &lt;!--\u7ed3\u675f\u5f02\u6b65\u914d\u7f6e--&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p>\n\t\u901a\u8fc7\u8bbe\u5b9amemoryBitLength\uff0c\u6307\u5b9a\u5e8f\u5217\u7684\u6700\u53f3\u7684memoryBitLength\u4f4d\u5728\u5185\u5b58\u4e2d\u751f\u6210\u4ee5\u63d0\u9ad8\u751f\u6210\u7684\u6548\u7387\u3002 \u9700\u8981\u6ce8\u610fmemoryBitLength\u503c\u8d8a\u5927\u5219\u5728\u5185\u5b58\u4e2d\u7684\u5e8f\u5217\u6761\u6570\u8d8a\u591a\uff0c \u6027\u80fd\u8d8a\u9ad8\uff0c \u5982\u679c\u53d1\u751f\u91cd\u542f\u65f6\u4e22\u5931\u7684\u5e8f\u5217\u4e5f\u4f1a\u8d8a\u591a\uff0c \u8981\u6839\u636e\u60c5\u51b5\u6765\u8bbe\u7f6e\u3002 \u652f\u6301\u5f02\u6b65\u751f\u6210\u5e8f\u5217\u503c\uff0c \u5f02\u6b65\u751f\u6210\u7684\u901f\u5ea6\u4f1a\u6839\u636e\u5e8f\u5217\u503c\u6d88\u8d39\u901f\u5ea6\u81ea\u9002\u5e94\u3002\n<\/p>\n<h2>\n\t\u5173\u4e8e\u53ef\u6269\u5c55\u6027<br \/>\n<\/h2>\n<p>\n\t\u5355\u53f7\u89c4\u5219\u591a\u79cd\u591a\u6837\uff0c \u4e0d\u80fd\u6bcf\u589e\u52a0\u4e00\u79cd\u89c4\u5219\u5c31\u589e\u52a0\u4e00\u4e2a\u9700\u6c42\uff0c \u6211\u4eec\u9700\u8981\u76f8\u5bf9\u7075\u6d3b\u7684\u6269\u5c55\u6027\u3002 \u4e0a\u8ff0\u4ecb\u7ecd\u4e86\u591a\u79cd\u5355\u53f7\u6570\u5b57\u5e8f\u5217\u7684\u751f\u6210\u65b9\u5f0f\uff0c \u548c\u6570\u5b57\u5e8f\u5217\u751f\u6210\u7684\u9ad8\u53ef\u7528\u548c\u9ad8\u6027\u80fd\u5b9e\u73b0\uff0c \u4ed6\u4eec\u90fd\u5b9e\u73b0\u4e86\u540c\u4e00\u4e2a\u63a5\u53e3\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n\/**\r\n * \u6839\u636e\u5e8f\u5217\u4e1a\u52a1\u7c7b\u578b\u751f\u6210\u65b0\u5e8f\u5217\u7684\u63a5\u53e3\r\n * \u751f\u6210\u5e8f\u5217\u662f\u5927\u81f4\u9012\u589e\u7684\r\n *\/\r\npublic interface SequenceGen {\r\n    \/**\r\n     * \u751f\u6210\u5e8f\u5217\r\n     * @param ownerKey \u5e8f\u5217\u4e1a\u52a1key\r\n     * @return \u65b0\u5e8f\u5217\u503c\r\n     *\/\r\n    long gen(String ownerKey);\r\n}<\/pre>\n<p>\n\t\u6709\u4e86\u8fd9\u4e2a\u7edf\u4e00\u7684\u6570\u5b57\u5e8f\u5217\u751f\u6210\u63a5\u53e3\uff0c \u6211\u4eec\u53ef\u4ee5\u6269\u5c55\u591a\u79cd\u4e0d\u540c\u7684\u6570\u5b57\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\u3002 \u6216\u8005\u5b9e\u73b0\u4e0d\u540c\u7684\u9ad8\u53ef\u7528\u3001\u9ad8\u6027\u80fd\u673a\u5236\u3002\n<\/p>\n<p>\n\t\u53e6\u5916\u5728\u672c\u6587\u7684\u5f00\u5934\u6211\u4eec\u4ecb\u7ecd\u4e86\u591a\u79cd\u4e0d\u540c\u7684\u5355\u53f7\u751f\u6210\u89c4\u5219\uff0c \u8981\u7075\u6d3b\u6ee1\u8db3\u8fd9\u4e9b\u4e0d\u540c\u7684\u89c4\u5219\uff0c \u6211\u4eec\u4f7f\u7528\u8868\u8fbe\u5f0f\u6765\u8868\u8fbe\u5355\u53f7\u7684\u7ec4\u5408\u89c4\u5219\uff0c \u901a\u8fc7\u5c06\u8868\u8fbe\u5f0f\u89e3\u6790\u6210\u4e0d\u540c\u7684Expression\u6765\u5b9e\u73b0\u4e0d\u540c\u5355\u53f7\u90e8\u5206\u7684\u751f\u6210\u3002 \u4e0b\u9762\u6211\u4eec\u770b\u4e00\u4e2a\u5355\u53f7\u8868\u8fbe\u5f0f\u7684\u793a\u4f8b\uff0c \u5982\u4e0b\u662f\u4e00\u4e2aspring bean\u914d\u7f6e\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;!-- \u5355\u53f7\u751f\u6210bean, \u5728\u5e94\u7528\u4e2d\u6ce8\u5165\u6b64bean\u751f\u6210\u5355\u53f7 --&gt;\r\n&lt;bean class=&quot;com.xttblog.SmartSNGen&quot; name=&quot;snGen&quot;&gt;\r\n    &lt;!-- \u5e8f\u5217\u53f7\u7684\u8868\u8fbe\u5f0f\uff0c \u89c1\u4e0b\u9762\u8bf4\u660e --&gt;\r\n    &lt;constructor-arg value=&quot;@{ownerKey, value=SN}-@{bean, ref=sequence}-@{com.xttblog.CheckSumExpression}&quot;\/&gt;\r\n    &lt;!-- \u8868\u8fbe\u5f0f\u89e3\u6790\u5668 --&gt;\r\n    &lt;property name=&quot;interpreter&quot;&gt;\r\n        &lt;!-- \u5355\u53f7\u751f\u6210\u5668\u7684\u8868\u8fbe\u5f0f\u89e3\u91ca\u5668\uff0c \u56fa\u5b9a\u4e3aSmartInterpreter--&gt;\r\n        &lt;bean class=&quot;com.xttblog.SmartInterpreter&quot; name=&quot;smartInterpreter&quot;\/&gt;\r\n    &lt;\/property&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p>\n\tSmartSNGen\u7c7b\u8d1f\u8d23\u6839\u636e\u8868\u8fbe\u5f0f\u751f\u6210\u4e0d\u540c\u89c4\u5219\u7684\u5355\u53f7\uff0c\u5176\u6784\u9020\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u503c\uff1a<br \/>\n\t@{ownerKey, value=SN}-@{bean, ref=sequence}-@{com.xttblog.CheckSumExpression} &nbsp;\u5373\u4e3a\u8868\u8fbe\u5f0f\uff0c \u8be5\u8868\u8fbe\u5f0f\u5206\u4e3a\u4e94\u4e2a\u90e8\u5206\uff1a\n<\/p>\n<ul>\n<li>\n\t\t@{ownerKey, value=SN} \u5728\u8868\u8fbe\u5f0f\u751f\u6210\u7684\u4e0a\u4e0b\u6587\u4e2d\u5199\u5165key\u4e3aownerKey\u503c\u4e3aSN\u7684\u53c2\u6570&ldquo;-&ldquo; \u8868\u793a\u9759\u6001\u8868\u8fbe\u5f0f&ldquo;-&rdquo;\n\t<\/li>\n<li>\n\t\t@{bean, ref=sequence} \u6307\u5b9a\u5f15\u7528id\u4e3asequence\u7684spring bean\u6765\u751f\u6210\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206&ldquo;-&ldquo;\u8868\u793a\u9759\u6001\u8868\u8fbe\u5f0f&rdquo;-&ldquo;\n\t<\/li>\n<li>\n\t\t@{com.xttblog.CheckSumExpression} \u8868\u793a\u8981\u521b\u5efa\u6307\u5b9a\u7c7bcom.xttblog.CheckSumExpression\u7684\u5b9e\u4f8b\u6765\u751f\u6210\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\n\t<\/li>\n<\/ul>\n<p>\n\t\u8be5bean\u7684interpreter\u5c5e\u6027\u6307\u5b9a\u4e86\u8868\u8fbe\u5f0f\u7684\u89e3\u91ca\u5668\uff0c\u8be5\u89e3\u91ca\u5668\u4f1a\u5c06\u8868\u8fbe\u5f0f\u503c\u8f6c\u6362\u4e3a\u5b9e\u73b0\u4e86Expression\u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u901a\u8fc7\u8be5\u5bf9\u8c61\u53ef\u4ee5\u8ba1\u7b97\u51fa\u5355\u53f7\u7684\u503c\u3002\n<\/p>\n<p>\n\t\u8868\u8fbe\u5f0f\u89e3\u91ca\u5668\u67e5\u627e\u8868\u8fbe\u5f0f\u4e2d\u7684&ldquo;@{&rdquo;\u548c&ldquo;}&rdquo;\u5bf9\uff0c\u5c06\u5176\u5185\u90e8\u7684\u8868\u8fbe\u5f0f\u89e3\u6790\u4e3a\u52a8\u6001\u8868\u8fbe\u5f0f\uff0c\u5c06\u5176\u4ed6\u90e8\u5206\u7684\u8868\u8fbe\u5f0f\u89e3\u6790\u4e3a\u9759\u6001\u8868\u8fbe\u5f0f\u3002\u52a8\u6001\u8868\u8fbe\u5f0f\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\n<\/p>\n<ol>\n<li>\n\t\tspring\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684bean\u5f15\u7528\u8868\u8fbe\u5f0f\n\t<\/li>\n<li>\n\t\t\u6307\u5b9a\u4e0a\u4e0b\u6587\u53c2\u6570\u7684\u8868\u8fbe\u5f0f\n\t<\/li>\n<li>\n\t\t\u6307\u5b9a\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\n\t<\/li>\n<\/ol>\n<p>\n\t\u7b2c3\u79cd\u8868\u8fbe\u5f0f\u7559\u51fa\u4efb\u610f\u6269\u5c55\u81ea\u5b9a\u4e49\u8868\u8fbe\u5f0f\u7684\u6269\u5c55\u70b9\u3002\n<\/p>\n<p>\n\tExpression\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nimport com.xttblog.GenContext;\r\n\/\/SmartSNGen\u8868\u8fbe\u5f0f\u63a5\u53e3\r\npublic interface Expression {\r\n    \/**\r\n     * \u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u503c\r\n     * @param context \u8868\u8fbe\u5f0f\u8ba1\u7b97\u4e0a\u4e0b\u6587\uff0c \u8868\u8fbe\u5f0f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u8ba1\u7b97\u4e2d\u95f4\u503c\u5b58\u50a8\u5230\u4e0a\u4e0b\u6587\u4e2d\uff0c \u4ee5\u4fbf\u5728\u8868\u8fbe\u5f0f\u4e4b\u95f4\u5171\u4eab\u6570\u636e\r\n     * @return \u8868\u8fbe\u5f0f\u8ba1\u7b97\u503c\r\n     *\/\r\n    Object eval(GenContext context);\r\n    \/**\r\n     * \u8ba1\u7b97\u4f18\u5148\u7ea7\uff0c \u4f18\u5148\u7ea7\u8d8a\u9ad8\u8d8a\u5148\u6267\u884c\uff0c \u5982\u679c\u8868\u8fbe\u5f0f\u9700\u8981\u4f9d\u8d56\u5176\u4ed6\u8868\u8fbe\u5f0f\u7684\u503c\uff0c \u5219\u8981\u5728\u4f9d\u8d56\u8868\u8fbe\u5f0f\u8ba1\u7b97\u4e4b\u540e\u6267\u884c\r\n     * @return \u6267\u884c\u987a\u5e8f\r\n     *\/\r\n    ExecuteOrder executeOrder();\r\n    \/**\r\n     * \u8be5\u8868\u8fbe\u5f0f\u7684\u6700\u5927\u5b57\u7b26\u4e32\u957f\u5ea6\u503c\r\n     *\r\n     * @return \u6700\u5927\u957f\u5ea6\u503c\r\n     *\/\r\n    int maxLength();\r\n}<\/pre>\n<p>\n\t\u901a\u8fc7\u5b9e\u73b0\u6b64\u63a5\u53e3\u5373\u53ef\u5b9e\u73b0\u4efb\u4f55\u81ea\u5b9a\u4e49\u7684\u5355\u53f7\u751f\u6210\u903b\u8f91\u3002\u5982\u4e0b\u662f\u81ea\u5b9a\u4e49\u7684\u5355\u53f7\u6821\u9a8c\u4f4d\u751f\u6210\u8868\u8fbe\u5f0f\u793a\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class CheckSumExpressionimplements Expression {\r\n    public Object eval(GenContext context) {\r\n        Long newId = (Long) context.get(&quot;sequence&quot;);\r\n        if (newId == null) {\r\n            throw newRuntimeException(&quot;sequence can not be null when calculate checksum&quot;);\r\n        }\r\n        return newId * 9 % 31 % 10;\r\n    }\r\n    public ExecuteOrder executeOrder() {\r\n        return ExecuteOrder.AfterNormal;\r\n    }\r\n    public int maxLength() {\r\n        return 1;\r\n    }\r\n}<\/pre>\n<h2>\n\t\u603b\u7ed3<br \/>\n<\/h2>\n<p>\n\t\u672c\u6587\u63d0\u5230\u4e86\u591a\u79cd\u5355\u53f7\u6570\u5b57\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u9ad8\u53ef\u7528\u3001\u9ad8\u6027\u80fd\u4ee5\u53ca\u6269\u5c55\u6027\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\n<\/p>\n<ol>\n<li>\n\t\t\u8981\u6839\u636e\u573a\u666f\uff0c \u5e76\u53d1\u91cf\uff0c \u5355\u53f7\u7c7b\u578b\u6570\u91cf\u9009\u62e9\u6570\u5b57\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\uff1b\n\t<\/li>\n<li>\n\t\t\u4e0d\u8981\u88f8\u5954\uff0c \u8981\u4f7f\u7528\u9ad8\u53ef\u7528+\u9ad8\u6027\u80fd\u5e8f\u5217\u751f\u6210\u5668\uff0c \u4fdd\u8bc1\u5355\u53f7\u751f\u6210\u65b9\u5f0f\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\uff1b\n\t<\/li>\n<li>\n\t\t\u5e95\u5c42\u5e8f\u5217\u8981\u4ece\u7269\u7406\u4e0a\u505a\u9694\u79bb\uff0c \u5426\u5219\u51fa\u73b0\u786c\u4ef6\u6545\u969c\u9ad8\u53ef\u7528\u673a\u5236\u4e5f\u4f1a\u65f6\u6548\uff1b\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528\u4e86\u591a\u4e2a\u5e95\u5c42\u5e8f\u5217\u751f\u6210\u65b9\u5f0f\u65f6\u751f\u6210\u7684\u5e8f\u5217\u662f\u5927\u81f4\u81ea\u589e\uff0c \u4e0d\u80fd\u4fdd\u8bc1\u5b8c\u5168\u81ea\u589e\uff0c \u8fd9\u662f\u8bbe\u8ba1\u4f7f\u7136\uff0c \u5982\u679c\u8981\u4fdd\u8bc1\u5b8c\u5168\u81ea\u589e\uff0c \u5219\u4f1a\u51fa\u73b0\u5355\u70b9\uff0c \u5728\u5b8c\u5168\u81ea\u589e\u548c\u5355\u70b9\u7684\u9009\u62e9\u4e0a\uff0c \u6211\u4eec\u9009\u62e9\u4e86\u5927\u81f4\u81ea\u589e+\u975e\u5355\u70b9\uff1b\n\t<\/li>\n<li>\n\t\t\u9ad8\u6027\u80fd\u5e8f\u5217\u751f\u6210\u7684\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u8c03\u8282\u5176memoryBitLength\u5c5e\u6027\u6765\u63d0\u9ad8\uff0c \u4f46\u8981\u6839\u636e\u4e1a\u52a1\u5b9e\u9645\u60c5\u51b5\u6765\u505a\u9009\u62e9\uff0cmemoryBitLength\u5c5e\u6027\u503c\u8d8a\u9ad8\u5728\u5185\u5b58\u751f\u6210\u7684\u5e8f\u5217\u6570\u8d8a\u591a\uff0c\u6027\u80fd\u8d8a\u9ad8\uff0c \u4f46\u5728\u8fdb\u7a0b\u505c\u6b62\u65f6\u4e22\u5931\u7684\u5e8f\u5217\u4e5f\u4f1a\u8d8a\u591a\u3002\n\t<\/li>\n<\/ol>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1608\">\u9ad8\u53ef\u7528\u9ad8\u6027\u80fd\u53ef\u6269\u5c55\u7684\u5355\u53f7\u751f\u6210\u65b9\u6848<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e1a\u52a1\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5355\u53f7\u751f\u6210\uff0c \u4f8b\u5982\u5feb\u9012\u5355\u53f7\u3001\u670d\u52a1\u5355\u53f7\u3001\u8ba2\u5355\u53f7\u7b49\u7b49\u3002 \u8fd9\u4e9b\u5355\u53f7\u751f\u6210\u5f80\u5f80\u662f\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u7684\u7b2c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1345,1344,1110,1346,671,723],"class_list":["post-1608","post","type-post","status-publish","format-standard","hentry","category-java","tag-sequence","tag-sequencegen","tag-1110","tag-1346","tag-671","tag-723"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1608"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1608\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1608\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}