{"id":4308,"date":"2023-07-19T15:04:32","date_gmt":"2023-07-19T07:04:32","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4308"},"modified":"2023-07-19T15:04:32","modified_gmt":"2023-07-19T07:04:32","slug":"%e4%b8%80%e4%b8%aa%e4%ba%8b%e5%8a%a1%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bf%9d%e8%af%81mysql%e5%92%8credis%e7%9a%84%e6%93%8d%e4%bd%9c%e4%b8%80%e8%87%b4%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4308","title":{"rendered":"\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u5982\u4f55\u4fdd\u8bc1mysql\u548credis\u7684\u64cd\u4f5c\u4e00\u81f4\u6027"},"content":{"rendered":"\n<p>\u5982\u679cmysql\u5199\u5165\u6210\u529f\u4e4b\u540e\uff0credis\u5b95\u673a\u4e86\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684tcc\u73a9\u6cd5\uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u7ed9\u4e09\u4e2a\u670d\u52a1\u65b9\u6cd5\u6dfb\u52a0\u4e00\u4e9b\u7b80\u5355\u4e1a\u52a1\u903b\u8f91\u7684\u4f8b\u5b50\u3002\u6211\u5047\u8bbe\u4e86\u4e00\u4e9b\u6570\u636e\u548c\u5bf9\u8c61\uff0c\u5e76\u6a21\u62df\u4e86\u4e00\u4e9b\u53ef\u80fd\u7684\u4e1a\u52a1\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u8ba2\u5355\u670d\u52a1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>@Service\npublic class OrderAction {\n\n    @Autowired\n    private OrderMapper orderMapper;\n\n    \/\/ Try\u9636\u6bb5\uff0c\u521b\u5efa\u8ba2\u5355\n    public boolean prepareCreateOrder(String txId, String userId, int productId, int amount) {\n        \/\/ \u521b\u5efa\u8ba2\u5355\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u6210\u529f\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\n        Order order = new Order(txId, userId, productId, amount);\n        int result = orderMapper.insert(order);\n        return result > 0;\n    }\n\n    \/\/ Confirm\u9636\u6bb5\uff0c\u786e\u8ba4\u521b\u5efa\u8ba2\u5355\n    public boolean commitCreateOrder(String txId) {\n        \/\/ \u786e\u8ba4\u8ba2\u5355\u7684\u64cd\u4f5c\uff0c\u6b64\u5904\u53ef\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5bf9\u4e8e\u521b\u5efa\u8ba2\u5355\u64cd\u4f5c\uff0c\u786e\u8ba4\u9636\u6bb5\u53ef\u80fd\u65e0\u5177\u4f53\u64cd\u4f5c\n        return true;\n    }\n\n    \/\/ Cancel\u9636\u6bb5\uff0c\u53d6\u6d88\u521b\u5efa\u8ba2\u5355\n    public boolean cancelCreateOrder(String txId) {\n        \/\/ \u53d6\u6d88\u8ba2\u5355\u7684\u64cd\u4f5c\uff0c\u5373\u5220\u9664\u5bf9\u5e94\u7684\u8ba2\u5355\n        int result = orderMapper.deleteByTxId(txId);\n        return result > 0;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>\u5e93\u5b58\u670d\u52a1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>@Service\npublic class StorageAction {\n\n    @Autowired\n    private StorageMapper storageMapper;\n\n    \/\/ Try\u9636\u6bb5\uff0c\u51cf\u5c11\u5e93\u5b58\n    public boolean prepareDecreaseStorage(String txId, int productId, int count) {\n        \/\/ \u51cf\u5c11\u5e93\u5b58\u7684\u64cd\u4f5c\n        int result = storageMapper.decreaseStorage(productId, count);\n        return result > 0;\n    }\n\n    \/\/ Confirm\u9636\u6bb5\uff0c\u786e\u8ba4\u51cf\u5c11\u5e93\u5b58\n    public boolean commitDecreaseStorage(String txId) {\n        \/\/ \u786e\u8ba4\u51cf\u5c11\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u6b64\u5904\u53ef\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5bf9\u4e8e\u51cf\u5c11\u5e93\u5b58\u64cd\u4f5c\uff0c\u786e\u8ba4\u9636\u6bb5\u53ef\u80fd\u65e0\u5177\u4f53\u64cd\u4f5c\n        return true;\n    }\n\n    \/\/ Cancel\u9636\u6bb5\uff0c\u53d6\u6d88\u51cf\u5c11\u5e93\u5b58\n    public boolean cancelDecreaseStorage(String txId, int productId, int count) {\n        \/\/ \u53d6\u6d88\u51cf\u5c11\u5e93\u5b58\u7684\u64cd\u4f5c\uff0c\u5373\u589e\u52a0\u5bf9\u5e94\u6570\u91cf\u7684\u5e93\u5b58\n        int result = storageMapper.increaseStorage(productId, count);\n        return result > 0;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Redis\u64cd\u4f5c\u670d\u52a1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>@Service\npublic class RedisAction {\n\n    @Autowired\n    private RedisTemplate&lt;String, String> redisTemplate;\n\n    \/\/ Try\u9636\u6bb5\uff0c\u5199\u5165Redis\n    public boolean prepareWriteRedis(String txId, String key, String value) {\n        \/\/ \u5199\u5165Redis\u7684\u64cd\u4f5c\n        redisTemplate.opsForValue().set(key, value);\n        return true;\n    }\n\n    \/\/ Confirm\u9636\u6bb5\uff0c\u786e\u8ba4\u5199\u5165Redis\n    public boolean commitWriteRedis(String txId) {\n        \/\/ \u786e\u8ba4\u5199\u5165Redis\u7684\u64cd\u4f5c\uff0c\u6b64\u5904\u53ef\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5bf9\u4e8eRedis\u5199\u5165\u64cd\u4f5c\uff0c\u786e\u8ba4\u9636\u6bb5\u53ef\u80fd\u65e0\u5177\u4f53\u64cd\u4f5c\n        return true;\n    }\n\n    \/\/ Cancel\u9636\u6bb5\uff0c\u53d6\u6d88\u5199\u5165Redis\n    public boolean cancelWriteRedis(String txId, String key) {\n        \/\/ \u53d6\u6d88\u5199\u5165Redis\u7684\u64cd\u4f5c\uff0c\u5373\u5220\u9664\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\n        redisTemplate.delete(key);\n        return true;\n    }\n}\n<\/code>\n\/\/ Spring Boot\u4e2d\u7684Service\u7c7b\n@Service\npublic class BusinessService {\n\n    @Autowired\n    private OrderAction orderAction;\n\n    @Autowired\n    private StorageAction storageAction;\n\n    @Autowired\n    private RedisAction redisAction;\n\n    \/\/ \u5f00\u59cb\u5168\u5c40\u4e8b\u52a1\n    @GlobalTransactional\n    public void doBusiness() {\n        String txId = UUID.randomUUID().toString();\n        String userId = \"1\";\n        int productId = 1;\n        int count = 1;\n        String key = \"key\";\n        String value = \"value\";\n\n        \/\/ Try\u9636\u6bb5\n        boolean orderResult = orderAction.prepareCreateOrder(txId, userId, productId, count);\n        boolean storageResult = storageAction.prepareDecreaseStorage(txId, productId, count);\n        boolean redisResult = redisAction.prepareWriteRedis(txId, key, value);\n\n        if (orderResult &amp;&amp; storageResult &amp;&amp; redisResult) {\n            \/\/ Confirm\u9636\u6bb5\n            orderAction.commitCreateOrder(txId);\n            storageAction.commitDecreaseStorage(txId);\n            redisAction.commitWriteRedis(txId);\n        } else {\n            \/\/ Cancel\u9636\u6bb5\n            orderAction.cancelCreateOrder(txId);\n            storageAction.cancelDecreaseStorage(txId);\n            redisAction.cancelWriteRedis(txId);\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u60c5\u51b5\u53ef\u80fd\u4f1a\u66f4\u590d\u6742\u3002\u5728\u5b9e\u9645\u7684\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u66f4\u591a\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u53ca\u53ef\u80fd\u7684\u6027\u80fd\u548c\u5e76\u53d1\u95ee\u9898\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0cSeata\u548cTCC\u6a21\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\u548c\u4f18\u5316\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6700\u7ec8\u4e00\u81f4\u6027\u7684\u65b9\u6848\uff1a<\/p>\n\n\n\n<p><strong>\u6d88\u606f\u961f\u5217\u6700\u7ec8\u4e00\u81f4\u6027<\/strong>: \u901a\u8fc7\u6d88\u606f\u961f\u5217\u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\u53ef\u4ee5\u786e\u4fdd\u64cd\u4f5c\u7684\u6700\u7ec8\u4e00\u81f4\u6027\u3002\u4e00\u822c\u6d41\u7a0b\u4e3a\uff1a<ol><li>\u4e1a\u52a1\u7cfb\u7edf\u5c06\u4e1a\u52a1\u64cd\u4f5c\u548c\u76f8\u5e94\u7684\u8865\u507f\u64cd\u4f5c\u4f5c\u4e3a\u4e00\u4e2a\u6d88\u606f\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\u3002<\/li><li>\u6d88\u606f\u6d88\u8d39\u8005\u63a5\u6536\u5230\u6d88\u606f\u5e76\u6267\u884c\u4e1a\u52a1\u64cd\u4f5c\u3002<\/li><li>\u5982\u679c\u4e1a\u52a1\u64cd\u4f5c\u6210\u529f\uff0c\u6d88\u606f\u88ab\u786e\u8ba4\u5e76\u4ece\u961f\u5217\u4e2d\u5220\u9664\u3002<\/li><li>\u5982\u679c\u4e1a\u52a1\u64cd\u4f5c\u5931\u8d25\uff0c\u6d88\u606f\u91cd\u65b0\u653e\u56de\u961f\u5217\u4e2d\uff0c\u7b49\u5f85\u518d\u6b21\u88ab\u6d88\u8d39\u3002<\/li><\/ol>\u4ee5\u4e0b\u4e3a\u4e00\u4e2a\u7b80\u5355\u7684RabbitMQ\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ \u751f\u4ea7\u8005\npublic void sendMessage(TransactionMessage message) {\n    rabbitTemplate.convertAndSend(\"exchange\", \"routingKey\", message);\n}\n\n\/\/ \u6d88\u8d39\u8005\n@RabbitListener(queues = \"queue\")\npublic void handleMessage(TransactionMessage message) {\n    try {\n        \/\/ \u5199\u5165Redis\n        redisTemplate.opsForValue().set(\"key\", message.getData());\n    } catch (Exception e) {\n        \/\/ \u5982\u679c\u64cd\u4f5c\u5931\u8d25\uff0c\u91cd\u65b0\u53d1\u5e03\u6d88\u606f\u5230\u961f\u5217\uff0c\u7b49\u5f85\u91cd\u8bd5\n        rabbitTemplate.convertAndSend(\"exchange\", \"routingKey\", message);\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679cmysql\u5199\u5165\u6210\u529f\u4e4b\u540e\uff0credis\u5b95\u673a\u4e86\uff0c\u5982\u4f55\u5904\u7406\uff1f \u53ef\u4ee5\u4f7f\u7528\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684tcc\u73a9\u6cd5\uff1a \u4ee5\u4e0b\u662f\u7ed9\u4e09\u4e2a\u670d\u52a1\u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[212,154,281],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4308"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4308"}],"version-history":[{"count":9,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4308\/revisions"}],"predecessor-version":[{"id":4317,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4308\/revisions\/4317"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}