{"id":1784,"date":"2023-03-27T22:16:04","date_gmt":"2023-03-27T14:16:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1784"},"modified":"2023-04-23T21:16:10","modified_gmt":"2023-04-23T13:16:10","slug":"rabbitmq-application-scenario-automatic-expiration-and-cancellation-of-orders","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/27\/rabbitmq-application-scenario-automatic-expiration-and-cancellation-of-orders\/","title":{"rendered":"RabbitMQ\u5e94\u7528\u573a\u666f\uff1a\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u53d6\u6d88"},"content":{"rendered":"<h2>\u573a\u666f\u63cf\u8ff0<\/h2>\n<p>\u573a\u666f\u63cf\u8ff0\uff1a\u5f53\u7528\u6237\u4e0b\u5355\u540e\uff0c\u72b6\u6001\u4e3a\u5f85\u652f\u4ed8\uff0c\u5047\u5982\u5728\u89c4\u5b9a\u7684\u8fc7\u671f\u65f6\u95f4\u5185\u5c1a\u672a\u652f\u4ed8\u91d1\u989d\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u8bbe\u7f6e\u8ba2\u5355\u72b6\u6001\u4e3a\u53d6\u6d88\u3002\u5728\u4e0d\u7528MQ\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6bcf\u79d2\u8f6e\u8be2\u6570\u636e\u5e93\u67e5\u627e\u8d85\u51fa\u8fc7\u671f\u65f6\u95f4\u4e14\u672a\u652f\u4ed8\u7684\u8ba2\u5355\uff0c\u7136\u540e\u4fee\u6539\u72b6\u6001\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5360\u7528\u5f88\u591a\u8d44\u6e90\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u5229\u7528RabbitMQ\u7684\u6b7b\u4fe1\u961f\u5217\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u6b7b\u4fe1\u961f\u5217\u4e0e\u666e\u901a\u961f\u5217\u4e00\u6837\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u4f1a\u53d8\u6210\u6b7b\u4fe1\u961f\u5217\uff1a<\/p>\n<ul>\n<li>\u6d88\u606f\u88ab\u62d2\u7edd\uff08<code>basic.reject<\/code> \/ <code>basic.nack<\/code>\uff09\u5e76\u4e14\u4e0d\u518d\u91cd\u65b0\u6295\u9012<code>requeue=false<\/code><\/li>\n<li>\u6d88\u606f\u8fc7\u671f (<code>rabbitmq Time-To-Live -&gt; messageProperties.setExpiration()<\/code>)<\/li>\n<li>\u961f\u5217\u8d85\u51fa\u6700\u5927\u957f\u5ea6<\/li>\n<\/ul>\n<h2>\u6a21\u62df\u6848\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u57fa\u4e8eSpring Boot\u7684\u7b80\u5355\u6a21\u62df\u6848\u4f8b\uff1a<\/p>\n<p>\uff081\uff09<code>pom.xml<\/code>\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;!-- rabbitmq--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-amqp&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff082\uff09<code>application.properties<\/code>\u6587\u4ef6<\/p>\n<pre><code>##rabbitmq\nspring.rabbitmq.host=127.0.0.1\nspring.rabbitmq.port=5672\nspring.rabbitmq.username=guest\nspring.rabbitmq.password=guest\nspring.rabbitmq.virtual-host=\/\n# \u6d88\u606f\u53d1\u9001\u5230\u4ea4\u6362\u673a\u786e\u8ba4\u673a\u5236,\u662f\u5426\u786e\u8ba4\u56de\u8c03\nspring.rabbitmq.publisher-confirms=true<\/code><\/pre>\n<p>\uff083\uff09queue\u4ee5\u53caExchange\u7684\u521b\u5efa\u4e0e\u7ed1\u5b9a<\/p>\n<pre><code class=\"language-java\">import org.springframework.amqp.core.*;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport java.util.HashMap;\nimport java.util.Map;\n\n@Configuration\npublic class RabbitMQConfig {\n\n    \/**\n     * \u8ba2\u5355\u6b7b\u4fe1\u961f\u5217\u4ea4\u6362\u673a\u6807\u8bc6\u7b26  \u5c5e\u6027\u503c\u4e0d\u80fd\u6539\uff0c\u5199\u6b7b\n     *\/\n    private static final String ORDER_DEAD_LETTER_QUEUE_KEY = &quot;x-dead-letter-exchange&quot;;\n    \/**\n     * \u8ba2\u5355\u6b7b\u4fe1\u961f\u5217\u4ea4\u6362\u673a\u7ed1\u5b9a\u952e \u6807\u8bc6\u7b26  \u5c5e\u6027\u503c\u4e0d\u80fd\u6539\uff0c\u5199\u6b7b\n     *\/\n    private static final String ORDER_DEAD_LETTER_ROUTING_KEY = &quot;x-dead-letter-routing-key&quot;;\n\n    \/\/----------------------------\u8ba2\u5355\u6b7b\u4fe1\u5b9a\u4e49------------------------------\n    \/\/ \u8ba2\u5355\u8fc7\u671f\u6d41\u7a0b\uff1a \u6d88\u606f\uff08\u521b\u5efa\u7684\u8ba2\u5355\u53f7\uff09---&gt; \u53d1\u9001\u5230\u8ba2\u5355\u6b7b\u4fe1\u961f\u5217\uff0c\u4e0d\u6d88\u8d39\uff08\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff09---&gt; \uff08\u8d85\u8fc7\u8bbe\u5b9a\u7684\u8fc7\u671f\u65f6\u95f4\uff09\u6839\u636eORDER_DEAD_LETTER_QUEUE_KEY\u8def\u7531\u6b7b\u4fe1\u4ea4\u6362\u673a ---&gt; \u91cd\u65b0\u6d88\u8d39\uff0c\u6839\u636eORDER_DEAD_LETTER_ROUTING_KEY\u8f6c\u53d1\u5230\u8f6c\u53d1\u961f\u5217\uff08\u53d6\u51fa\u6d88\u606f\u8ba2\u5355\u53f7\u67e5\u627e\u8ba2\u5355\uff0c\u5047\u5982\u4ecd\u7136\u672a\u652f\u4ed8\u5c31\u53d6\u6d88\u8ba2\u5355\uff09---&gt; end\n\n    \/**\n     * orderDeadLetterExchange\uff08direct\u7c7b\u578b\u4ea4\u6362\u673a\uff09\n     *\/\n    @Bean(&quot;orderDeadLetterExchange&quot;)\n    public Exchange orderDeadLetterExchange() {\n        return ExchangeBuilder.directExchange(&quot;ORDER_DL_EXCHANGE&quot;).durable(true).build();\n    }\n\n    \/**\n     * \u58f0\u660e\u4e00\u4e2a\u8ba2\u5355\u6b7b\u4fe1\u961f\u5217.\n     * x-dead-letter-exchange   \u5bf9\u5e94 \u6b7b\u4fe1\u4ea4\u6362\u673a\n     * x-dead-letter-routing-key  \u5bf9\u5e94 \u6b7b\u4fe1\u961f\u5217\n     *\/\n    @Bean(&quot;orderDeadLetterQueue&quot;)\n    public Queue orderDeadLetterQueue() {\n        \/\/ \u53c2\u6570\n        Map&lt;String, Object&gt; args = new HashMap&lt;&gt;(2);\n        \/\/ \u51fa\u73b0dead letter\u4e4b\u540e\u5c06dead letter\u91cd\u65b0\u53d1\u9001\u5230\u6307\u5b9aexchange\n        args.put(ORDER_DEAD_LETTER_QUEUE_KEY, &quot;ORDER_DL_EXCHANGE&quot;);\n        \/\/ \u51fa\u73b0dead letter\u4e4b\u540e\u5c06dead letter\u91cd\u65b0\u6309\u7167\u6307\u5b9a\u7684routing-key\u53d1\u9001\n        args.put(ORDER_DEAD_LETTER_ROUTING_KEY, &quot;RED_KEY&quot;);\n        \/\/ name\u961f\u5217\u540d\u5b57  durable\u662f\u5426\u6301\u4e45\u5316\uff0ctrue\u4fdd\u8bc1\u6d88\u606f\u7684\u4e0d\u4e22\u5931, exclusive\u662f\u5426\u6392\u4ed6\u961f\u5217\uff0c\u5982\u679c\u4e00\u4e2a\u961f\u5217\u88ab\u58f0\u660e\u4e3a\u6392\u4ed6\u961f\u5217\uff0c\u8be5\u961f\u5217\u4ec5\u5bf9\u9996\u6b21\u7533\u660e\u5b83\u7684\u8fde\u63a5\u53ef\u89c1\uff0c\u5e76\u5728\u8fde\u63a5\u65ad\u5f00\u65f6\u81ea\u52a8\u5220\u9664, autoDelete\u5982\u679c\u8be5\u961f\u5217\u6ca1\u6709\u4efb\u4f55\u8ba2\u9605\u7684\u6d88\u8d39\u8005\u7684\u8bdd\uff0c\u8be5\u961f\u5217\u662f\u5426\u4f1a\u88ab\u81ea\u52a8\u5220\u9664, arguments\u53c2\u6570map\n        return new Queue(&quot;ORDER_DL_QUEUE&quot;, true, false, false, args);\n    }\n\n    \/**\n     * \u5b9a\u4e49\u8ba2\u5355\u6b7b\u4fe1\u961f\u5217\u8f6c\u53d1\u961f\u5217.\n     *\/\n    @Bean(&quot;orderRedirectQueue&quot;)\n    public Queue orderRedirectQueue() {\n        return new Queue(&quot;ORDER_REDIRECT_QUEUE&quot;, true, false, false);\n    }\n\n    \/**\n     * \u6b7b\u4fe1\u8def\u7531\u901a\u8fc7 DL_KEY \u7ed1\u5b9a\u952e\u7ed1\u5b9a\u5230\u8ba2\u5355\u6b7b\u4fe1\u961f\u5217\u4e0a.\n     *\/\n    @Bean\n    public Binding orderDeadLetterBinding() {\n        return new Binding(&quot;ORDER_DL_QUEUE&quot;, Binding.DestinationType.QUEUE, &quot;ORDER_DL_EXCHANGE&quot;, &quot;DL_KEY&quot;, null);\n\n    }\n\n    \/**\n     * \u6b7b\u4fe1\u8def\u7531\u901a\u8fc7 RED_KEY \u7ed1\u5b9a\u952e\u7ed1\u5b9a\u5230\u8ba2\u5355\u8f6c\u53d1\u961f\u5217\u4e0a.\n     *\/\n    @Bean\n    public Binding orderRedirectBinding() {\n        return new Binding(&quot;ORDER_REDIRECT_QUEUE&quot;, Binding.DestinationType.QUEUE, &quot;ORDER_DL_EXCHANGE&quot;, &quot;RED_KEY&quot;, null);\n    }\n}<\/code><\/pre>\n<p>\uff084\uff09\u6d88\u606f\u7684\u53d1\u9001<\/p>\n<pre><code class=\"language-java\">import org.springframework.amqp.core.MessagePostProcessor;\nimport org.springframework.amqp.core.MessageProperties;\nimport org.springframework.amqp.rabbit.connection.CorrelationData;\nimport org.springframework.amqp.rabbit.core.RabbitTemplate;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestParam;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport java.util.Date;\nimport java.util.UUID;\n\n@RestController\npublic class OrderController {\n\n    @Autowired\n    private RabbitTemplate rabbitTemplate;\n\n    @RequestMapping(&quot;\/test&quot;)\n    public void sendMessage(@RequestParam String orderNo){\n        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); \/\/ \u751f\u6210\u4e00\u4e2a\u6d88\u606f\u7684\u552f\u4e00id\uff0c\u53ef\u4e0d\u9009\n        \/\/ \u58f0\u660e\u6d88\u606f\u5904\u7406\u5668  \u8bbe\u7f6e\u6d88\u606f\u7684\u7f16\u7801\u4ee5\u53ca\u6d88\u606f\u7684\u8fc7\u671f\u65f6\u95f4  \u65f6\u95f4\u6beb\u79d2\u503c \u4e3a\u5b57\u7b26\u4e32\n        MessagePostProcessor messagePostProcessor = message -&gt; {\n            MessageProperties messageProperties = message.getMessageProperties();\n            \/\/ \u8bbe\u7f6e\u7f16\u7801\n            messageProperties.setContentEncoding(&quot;utf-8&quot;);\n            \/\/ \u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4 \u4e00\u5206\u949f\n            int expiration = 1000 * 20;\n            messageProperties.setExpiration(String.valueOf(expiration));\n            return message;\n        };\n        \/\/ \u5411ORDER_DL_EXCHANGE \u53d1\u9001\u6d88\u606f  \u5f62\u6210\u6b7b\u4fe1   \u5728OrderQueueReceiver\u7c7b\u5904\u7406\u6b7b\u4fe1\u4ea4\u6362\u673a\u8f6c\u53d1\u7ed9\u8f6c\u53d1\u961f\u5217\u7684\u4fe1\u606f\n        rabbitTemplate.convertAndSend(&quot;ORDER_DL_EXCHANGE&quot;, &quot;DL_KEY&quot;, orderNo, messagePostProcessor, correlationData);\n        System.out.println(new Date() +  &quot;\u53d1\u9001\u6d88\u606f\uff0c\u8ba2\u5355\u53f7\u4e3a&quot; + orderNo);\n    }\n}<\/code><\/pre>\n<p>\uff085\uff09\u6d88\u606f\u7684\u6d88\u8d39<\/p>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport org.springframework.amqp.core.Message;\nimport org.springframework.amqp.rabbit.annotation.RabbitListener;\nimport org.springframework.stereotype.Component;\n\nimport java.io.IOException;\nimport java.util.Date;\n\n@Component\npublic class MesageReceiver {\n\n    \/**\n     * \u76d1\u542c\u8f6c\u53d1\u961f\u5217  \u8d70\u903b\u8f91\u5224\u65ad\uff0c\u5c1a\u672a\u652f\u4ed8\u4e14\u8d85\u8fc7\u8fc7\u671f\u65f6\u95f4\u7684\u8ba2\u5355\u53f7\u8bbe\u7f6e\u4e3a\u5931\u6548\u8ba2\u5355\n     * @param message \u4fe1\u606f\u5305\u88c5\u7c7b\n     * @param channel \u901a\u9053\n     *\/\n    @RabbitListener(queues = {&quot;ORDER_REDIRECT_QUEUE&quot;})\n    public void redirect(Message message, Channel channel) throws IOException {\n        \/\/ \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u8ba2\u5355\u53f7\n        byte[] body = message.getBody();\n        String orderNo = new String(body,&quot;UTF-8&quot;);\n        System.out.println(new Date() +  &quot;\u6d88\u8d39\u6d88\u606f\uff0c\u8ba2\u5355\u53f7\u4e3a&quot; + orderNo);\n        \/\/ \u786e\u8ba4\u6d88\u606f\u6709\u6ca1\u6709\u88ab\u6536\u5230\uff0cfalse\u8868\u793a\u624b\u52a8\u786e\u8ba4  \u5728\u5904\u7406\u5b8c\u6d88\u606f\u65f6\uff0c\u8fd4\u56de\u5e94\u7b54\u72b6\u6001\n        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);\n    }\n}<\/code><\/pre>\n<p>\uff086\uff09\u6d4b\u8bd5\u7ed3\u679c<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u6b7b\u4fe1\u961f\u5217\u6d4b\u8bd5\u7ed3\u679c.png\" alt=\"RabbitMQ\u6b7b\u4fe1\u961f\u5217\u6d4b\u8bd5\u7ed3\u679c\" \/><\/p>\n<p>\u53d1\u9001\u6d88\u606f\u65f6\u53ef\u4ee5\u770b\u5230RabbitMQ\u7ba1\u7406\u754c\u9762\u7684<code>ORDER_DL_QUEUE<\/code>\u961f\u5217\u6709\u4e00\u6761\u5f85\u6d88\u8d39\u7684\u6d88\u606f\uff0c\u7136\u540e\u572820\u79d2\u8fc7\u671f\u540e\u53d8\u6210\u6b7b\u4fe1\u961f\u5217\u53d1\u9001\u81f3<code>ORDER_DL_EXCHANGE<\/code>\u4ea4\u6362\u5668\uff0c\u7136\u540e\u4ea4\u6362\u5668\u6839\u636e\u8def\u7531\u8f6c\u53d1\u5230<code>ORDER_REDIRECT_QUEUE<\/code>\u961f\u5217\uff0c\u5e76\u88ab\u76d1\u542c\u6d88\u8d39\u6389\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u63a7\u5236\u53f0\u6b7b\u4fe1\u961f\u5217\u76d1\u6d4b.png\" alt=\"RabbitMQ\u63a7\u5236\u53f0\u6b7b\u4fe1\u961f\u5217\u76d1\u6d4b\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u573a\u666f\u63cf\u8ff0 \u573a\u666f\u63cf\u8ff0\uff1a\u5f53\u7528\u6237\u4e0b\u5355\u540e\uff0c\u72b6\u6001\u4e3a\u5f85\u652f\u4ed8\uff0c\u5047\u5982\u5728\u89c4\u5b9a\u7684\u8fc7\u671f\u65f6\u95f4\u5185\u5c1a\u672a\u652f\u4ed8\u91d1\u989d\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u8bbe\u7f6e\u8ba2\u5355\u72b6\u6001\u4e3a\u53d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-1784","post","type-post","status-publish","format-standard","hentry","category-rabbitmq"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1784"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1784\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}