{"id":234399,"date":"2022-02-06T10:51:51","date_gmt":"2022-02-06T02:51:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=234399"},"modified":"2022-01-29T15:53:17","modified_gmt":"2022-01-29T07:53:17","slug":"ensure-idempotency-interface","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ensure-idempotency-interface.html","title":{"rendered":"\u5982\u4f55\u4fdd\u8bc1\u63a5\u53e3\u7684\u5e42\u7b49\u6027\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4ec0\u4e48\u662f\u5e42\u7b49\u6027?\u6240\u8c13\u5e42\u7b49\uff0c\u5c31\u662f\u4efb\u610f\u591a\u6b21\u6267\u884c\u6240\u4ea7\u751f\u7684\u5f71\u54cd\u5747\u4e0e\u4e00\u6b21\u6267\u884c\u7684\u5f71\u54cd\u76f8\u540c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e3a\u4ec0\u4e48\u4f1a\u4ea7\u751f\u63a5\u53e3\u5e42\u7b49\u6027\u95ee\u9898<\/strong><\/div>\n

\u5728\u8ba1\u7b97\u673a\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9047\u5230\u7f51\u7edc\u6296\u52a8\uff0c\u4e34\u65f6\u6545\u969c\uff0c\u6216\u8005\u670d\u52a1\u8c03\u7528\u5931\u8d25\uff0c\u5c24\u5176\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u66f4\u4e3a\u5e38\u89c1\u3002\u4e3a\u4e86\u4fdd\u8bc1\u670d\u52a1\u7684\u5b8c\u6574\u6027\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u53d1\u8d77\u63a5\u53e3\u7684\u91cd\u8bd5\u8c03\u7528\uff0c\u5982\u679c\u63a5\u53e3\u4e0d\u5904\u7406\u5e42\u7b49\uff0c\u53ef\u80fd\u5bf9\u7cfb\u7edf\u9020\u6210\u5f88\u5927\u7684\u5f71\u54cd\uff0c\u56e0\u6b64\u63a5\u53e3\u7684\u5e42\u7b49\u8bbe\u8ba1\u5c24\u5176\u66f4\u4e3a\u91cd\u8981\u3002<\/p>\n

\u5bf9\u4e8e\u4e1a\u52a1\u4e2d\u9700\u8981\u8003\u8651\u5e42\u7b49\u6027\u7684\u5730\u65b9\u4e00\u822c\u90fd\u662f\u63a5\u53e3\u7684\u91cd\u590d\u8bf7\u6c42\uff0c\u91cd\u590d\u8bf7\u6c42\u662f\u6307\u540c\u4e00\u4e2a\u8bf7\u6c42\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\u88ab\u591a\u6b21\u63d0\u4ea4\u3002\u5bfc\u81f4\u8fd9\u79cd\u60c5\u51b5\u7684\u53d1\u751f\u6709\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u7684\u573a\u666f\uff1a<\/p>\n

\u524d\u7aef\u91cd\u590d\u63d0\u4ea4\uff1a\u7528\u6237\u5728\u63d0\u4ea4\u8868\u5355\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u56e0\u7f51\u7edc\u6ce2\u52a8\u6ca1\u6709\u53ca\u65f6\u505a\u51fa\u63d0\u4ea4\u6210\u529f\u54cd\u5e94\uff0c\u81f4\u4f7f\u7528\u6237\u8ba4\u4e3a\u6ca1\u6709\u6210\u529f\u63d0\u4ea4\uff0c\u7136\u540e\u4e00\u76f4\u70b9\u63d0\u4ea4\u6309\u94ae\uff0c\u8fd9\u65f6\u5c31\u4f1a\u53d1\u751f\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u8bf7\u6c42\u3002<\/p>\n

\u63a5\u53e3\u8d85\u65f6\u91cd\u8bd5\uff1a\u7b2c\u4e09\u65b9\u8c03\u7528\u63a5\u53e3\u65f6\u5019\uff0c\u4e3a\u4e86\u8d85\u65f6\u7b49\u5f02\u5e38\u60c5\u51b5\u9020\u6210\u7684\u8bf7\u6c42\u5931\u8d25\uff0c\u90fd\u4f1a\u6dfb\u52a0\u91cd\u8bd5\u673a\u5236\uff0c\u5bfc\u81f4\u4e00\u4e2a\u8bf7\u6c42\u63d0\u4ea4\u591a\u6b21\u3002<\/p>\n

\u6d88\u606f\u91cd\u590d\u6d88\u8d39\uff1a\u5f53\u4f7f\u7528 MQ \u6d88\u606f\u4e2d\u95f4\u4ef6\u65f6\u5019\uff0c\u5982\u679c\u53d1\u751f\u6d88\u606f\u4e2d\u95f4\u4ef6\u51fa\u73b0\u9519\u8bef\u672a\u53ca\u65f6\u63d0\u4ea4\u6d88\u8d39\u4fe1\u606f\uff0c\u5bfc\u81f4\u53d1\u751f\u91cd\u590d\u6d88\u8d39\u3002<\/p>\n

\u5e42\u7b49\u6027\u89e3\u51b3\u65b9\u6848<\/strong><\/div>\n

\u90a3\u6211\u4eec\u5e94\u8be5\u80fd\u600e\u6837\u4fdd\u8bc1\u63a5\u53e3\u7684\u5e42\u7b49\u6027\u5462\uff1f<\/p>\n

\u53ef\u4ee5\u601d\u8003\u4e00\u4e0b\uff0c\u7b2c\u4e00\u79cd\u573a\u666f\u4e0b\uff0c\u65e2\u7136\u662f\u7528\u6237\u91cd\u590d\u63d0\u4ea4\u5bfc\u81f4\u7684\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u60f3\u529e\u6cd5\u8ba9\u7528\u6237\u6ca1\u529e\u6cd5\u91cd\u590d\u63d0\u4ea4\u3002<\/p>\n

\u65b9\u6848\u4e00\uff1a\u524d\u7aef\u63a7\u5236<\/p>\n

\u5728\u524d\u7aef\u505a\u62e6\u622a\uff0c\u6bd4\u5982\u6309\u94ae\u70b9\u51fb\u4e00\u6b21\u4e4b\u540e\u5c31\u7f6e\u7070\u6216\u8005\u9690\u85cf\u3002\u4f46\u662f\u5f80\u5f80\u524d\u7aef\u5e76\u4e0d\u53ef\u9760\uff0c\u8fd8\u662f\u5f97\u540e\u7aef\u5904\u7406\u624d\u66f4\u653e\u5fc3\u3002<\/p>\n

\u65b9\u6848\u4e8c\uff1aToken\u673a\u5236<\/p>\n

\u7528\u6237\u8fdb\u5165\u8868\u5355\u9875\u9762\u9996\u5148\u8c03\u7528\u540e\u53f0\u63a5\u53e3\u83b7\u53d6 token \u5e76\u5b58\u5165 redis\uff0c\u5f53\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6\u5c06 token \u4e5f\u4f5c\u4e3a\u5165\u53c2\uff0c\u540e\u7aef\u5148\u5220\u9664 redis \u4e2d\u7684 token\uff0c\u5220\u9664\u6210\u529f\u5219\u4fdd\u5b58\u8868\u5355\u6570\u636e\uff0c\u5931\u8d25\u5219\u63d0\u793a\u7528\u6237\u91cd\u590d\u63d0\u4ea4\u3002<\/p>\n

\"\"<\/a><\/p>\n

\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u4e0d\u5148\u5224\u65ad redis \u662f\u5426\u5b58\u5728\u8fd9\u4e2a token \u518d\u5220\u9664\uff0c\u662f\u56e0\u4e3a\u8981\u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff0c\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u67e5\u8be2\u5230 redis \u4e2d\u5b58\u5728\u8fd9\u4e2a token\uff0c\u8fd8\u6ca1\u6765\u5f97\u53ca\u5220\u9664\uff0c\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u8fdb\u6765\uff0c\u4e5f\u67e5\u8be2\u5230 redis \u4e2d\u5b58\u5728\u8fd9\u4e2a token\uff0c\u90a3\u4e48\u8fd8\u662f\u4f1a\u9020\u6210\u91cd\u590d\u63d0\u4ea4\u7684\u95ee\u9898\u3002<\/p>\n

token \u673a\u5236\u9700\u8981\u5148\u8bf7\u6c42\u83b7\u53d6 token \u7684\u63a5\u53e3\uff0c\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\u5f88\u660e\u663e\u5e76\u4e0d\u5408\u9002\u3002\u6211\u4eec\u5927\u90e8\u5206\u8bf7\u6c42\u90fd\u662f\u8981\u843d\u5230\u6570\u636e\u5e93\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u7740\u624b\u3002<\/p>\n

\u65b9\u6848\u4e09\u3001\u552f\u4e00\u7d22\u5f15<\/p>\n

\u8fd9\u79cd\u65b9\u6848\u5c31\u6bd4\u8f83\u597d\u7406\u89e3\u4e86\uff0c\u4f7f\u7528\u552f\u4e00\u7d22\u5f15\u53ef\u4ee5\u907f\u514d\u810f\u6570\u636e\u7684\u6dfb\u52a0\uff0c\u5f53\u63d2\u5165\u91cd\u590d\u6570\u636e\u65f6\u6570\u636e\u5e93\u4f1a\u629b\u5f02\u5e38\uff0c\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u552f\u4e00\u6027\u3002\u552f\u4e00\u7d22\u5f15\u53ef\u4ee5\u652f\u6301\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u4e1a\u52a1\u64cd\u4f5c\u3002<\/p>\n

\u65b9\u6848\u56db\u3001\u60b2\u89c2\u9501<\/p>\n

\u8fd9\u91cc\u6240\u8bf4\u7684\u60b2\u89c2\u9501\u662f\u57fa\u4e8e\u6570\u636e\u5e93\u5c42\u9762\u7684\uff0c\u5728\u83b7\u53d6\u6570\u636e\u65f6\u8fdb\u884c\u52a0\u9501\uff0c\u5f53\u540c\u65f6\u6709\u591a\u4e2a\u91cd\u590d\u8bf7\u6c42\u65f6\uff0c\u5176\u4ed6\u8bf7\u6c42\u90fd\u65e0\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002\u60b2\u89c2\u9501\u53ea\u9002\u7528\u4e8e\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n

\r\n\/\/ \u4f8b\u5982\r\nselect name from t_goods where id=1 for update;\r\n<\/pre>\n

\u6ce8\u610f\uff1aid \u5b57\u6bb5\u4e00\u5b9a\u8981\u662f\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\uff0c\u4e0d\u7136\u4f1a\u9501\u4f4f\u6574\u5f20\u8868\uff0c\u8fd9\u662f\u4f1a\u6b7b\u4eba\u7684\u3002\u60b2\u89c2\u9501\u4f7f\u7528\u65f6\u4e00\u822c\u4f34\u968f\u4e8b\u52a1\u4e00\u8d77\u4f7f\u7528\uff0c\u6570\u636e\u9501\u5b9a\u65f6\u95f4\u53ef\u80fd\u4f1a\u5f88\u957f\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u7528\u3002<\/p>\n

\u5728\u8bf7\u6c42\u91cf\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u60b2\u89c2\u9501\u660e\u663e\u4e0d\u5408\u9002\uff0c\u8fd9\u65f6\u5019\u5c31\u5230\u4e50\u89c2\u9501\u4e0a\u573a\u4e86\u3002<\/p>\n

\u65b9\u6848\u4e94\u3001\u4e50\u89c2\u9501<\/p>\n

\u53ef\u4ee5\u901a\u8fc7\u7248\u672c\u53f7\u5b9e\u73b0\uff0c\u4e3a\u8868\u589e\u52a0\u4e00\u4e2a version \u5b57\u6bb5\uff0c\u5f53\u6570\u636e\u9700\u8981\u66f4\u65b0\u65f6\uff0c\u5148\u53bb\u6570\u636e\u5e93\u91cc\u83b7\u53d6\u6b64\u65f6\u7684version\u7248\u672c\u53f7\u3002<\/p>\n

\r\nselect version from t_goods where id=1\r\n<\/pre>\n

\u66f4\u65b0\u6570\u636e\u65f6\u9996\u5148\u8981\u5bf9\u6bd4\u7248\u672c\u53f7\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u8bf4\u660e\u5df2\u7ecf\u6709\u5176\u4ed6\u7684\u8bf7\u6c42\u53bb\u66f4\u65b0\u6570\u636e\u4e86\uff0c\u63d0\u793a\u66f4\u65b0\u5931\u8d25\u3002<\/p>\n

update t_goods set count=count+1,version=version+1 where version=#{version}
\n\u8fd8\u6709\u4e00\u79cd\u662f\u901a\u8fc7\u72b6\u6001\u673a\u5b9e\u73b0\u7684\uff0c\u5176\u5b9e\u4e5f\u662f\u4e50\u89c2\u9501\u7684\u539f\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5728\u6709\u72b6\u6001\u6d41\u8f6c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bd4\u5982\u8ba2\u5355\u7684\u521b\u5efa\u548c\u4ed8\u6b3e\uff0c\u8ba2\u5355\u7684\u521b\u5efa\u80af\u5b9a\u662f\u5728\u4ed8\u6b3e\u4e4b\u524d\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u8bbe\u8ba1\u72b6\u6001\u5b57\u6bb5\u65f6\uff0c\u4f7f\u7528 int \u7c7b\u578b\uff0c\u5e76\u4e14\u901a\u8fc7\u503c\u7c7b\u578b\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u5e42\u7b49\u6027\u3002<\/p>\n

\r\nupdate t_goods set status=#{status} where id=1 and status<#{status}\r\n<\/pre>\n

\u540c\u6837\uff0c\u4e50\u89c2\u9501\u4e5f\u53ea\u9002\u7528\u4e8e\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n

\u65b9\u6848\u516d\u3001\u5206\u5e03\u5f0f\u9501<\/p>\n

\u6709\u65f6\u5019\u6211\u4eec\u7684\u4e1a\u52a1\u4e0d\u4ec5\u4ec5\u662f\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u4e5f\u53ef\u80fd\u662f\u53d1\u9001\u77ed\u4fe1\u3001\u6d88\u606f\u7b49\u7b49\uff0c\u90a3\u6570\u636e\u5e93\u5c42\u9762\u7684\u9501\u5c31\u4e0d\u9002\u5408\u4e86\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u8981\u8003\u8651\u4ee3\u7801\u5c42\u9762\u7684\u9501\u4e86\uff0c\u800c java \u7684\u81ea\u5e26\u7684\u9501\u5728\u5206\u5e03\u5f0f\u96c6\u7fa4\u90e8\u7f72\u7684\u573a\u666f\u4e0b\u5e76\u4e0d\u9002\u7528\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u91c7\u7528\u5206\u5e03\u5f0f\u9501\u6765\u5b9e\u73b0\uff08Redis \u6216 Zookeeper\uff09\u3002<\/p>\n

\u62ff Redis \u5206\u5e03\u5f0f\u9501\u4e3e\u4f8b\uff0c\u6bd4\u5982\u4e00\u4e2a\u8ba2\u5355\u53d1\u8d77\u652f\u4ed8\u8bf7\u6c42\uff0c\u652f\u4ed8\u7cfb\u7edf\u4f1a\u53bb Redis \u7f13\u5b58\u4e2d\u67e5\u8be2\u662f\u5426\u5b58\u5728\u8be5\u8ba2\u5355\u53f7\u7684 Key\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4ee5 Key \u4e3a\u8ba2\u5355\u53f7\u5411 Redis \u63d2\u5165\u3002\u67e5\u8be2\u8ba2\u5355\u662f\u5426\u5df2\u7ecf\u652f\u4ed8\uff0c\u5982\u679c\u6ca1\u6709\u5219\u8fdb\u884c\u652f\u4ed8\uff0c\u652f\u4ed8\u5b8c\u6210\u540e\u5220\u9664\u8be5\u8ba2\u5355\u53f7\u7684Key\u3002\u901a\u8fc7 Redis \u505a\u5230\u4e86\u5206\u5e03\u5f0f\u9501\uff0c\u53ea\u6709\u8fd9\u6b21\u8ba2\u5355\u652f\u4ed8\u8bf7\u6c42\u5b8c\u6210\uff0c\u4e0b\u6b21\u8bf7\u6c42\u624d\u80fd\u8fdb\u6765\u3002\u5f53\u7136\u8fd9\u91cc\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2aKey \u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5728\u53d1\u751f\u5f02\u5e38\u7684\u65f6\u5019\u8fd8\u8981\u6ce8\u610f\u5220\u9664 Redis \u7684 Key\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u63a5\u53e3\u7684\u5e42\u7b49\u6027\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u573a\u666f\u7684\u4e0d\u540c\uff0c\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u8ba1\u7b97\u673a\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9047\u5230\u7f51\u7edc\u6296\u52a8\uff0c\u4e34\u65f6\u6545\u969c\uff0c\u6216\u8005\u670d\u52a1\u8c03\u7528\u5931\u8d25\uff0c\u5c24\u5176\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u66f4\u4e3a\u5e38\u89c1\u3002\u4e3a\u4e86\u4fdd […]<\/p>\n","protected":false},"author":1903,"featured_media":235036,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-234399","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=234399"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234399\/revisions"}],"predecessor-version":[{"id":234401,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234399\/revisions\/234401"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235036"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=234399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=234399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=234399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}