{"id":2161,"date":"2023-04-02T13:35:25","date_gmt":"2023-04-02T05:35:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2161"},"modified":"2023-04-05T13:17:40","modified_gmt":"2023-04-05T05:17:40","slug":"introduction-and-application-scenarios-of-7-queue-patterns-in-rabbitmq","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/02\/introduction-and-application-scenarios-of-7-queue-patterns-in-rabbitmq\/","title":{"rendered":"RabbitMQ\u4e03\u79cd\u961f\u5217\u6a21\u5f0f\u4ecb\u7ecd\u4e0e\u5e94\u7528\u573a\u666f\uff08\u901a\u4fd7\u6613\u61c2\uff09"},"content":{"rendered":"<h2>\u4e03\u79cd\u6a21\u5f0f\u4ecb\u7ecd\u4e0e\u5e94\u7528\u573a\u666f<\/h2>\n<h3>\u7b80\u5355\u6a21\u5f0f\uff08Hello World\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u7b80\u5355\u6a21\u5f0f.png\" alt=\"RabbitMQ\u7b80\u5355\u6a21\u5f0f\" \/><\/p>\n<p><!-- more --><\/p>\n<p><strong>\u505a\u6700\u7b80\u5355\u7684\u4e8b\u60c5<\/strong>\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\u5bf9\u5e94\u4e00\u4e2a\u6d88\u8d39\u8005\uff0cRabbitMQ\u76f8\u5f53\u4e8e\u4e00\u4e2a\u6d88\u606f\u4ee3\u7406\uff0c\u8d1f\u8d23\u5c06A\u7684\u6d88\u606f\u8f6c\u53d1\u7ed9B<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u5c06\u53d1\u9001\u7684\u7535\u5b50\u90ae\u4ef6\u653e\u5230\u6d88\u606f\u961f\u5217\uff0c\u7136\u540e\u90ae\u4ef6\u670d\u52a1\u5728\u961f\u5217\u4e2d\u83b7\u53d6\u90ae\u4ef6\u5e76\u53d1\u9001\u7ed9\u6536\u4ef6\u4eba<\/p>\n<h3>\u5de5\u4f5c\u961f\u5217\u6a21\u5f0f\uff08Work queues\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u5de5\u4f5c\u961f\u5217\u6a21\u5f0f.png\" alt=\"RabbitMQ\u5de5\u4f5c\u961f\u5217\u6a21\u5f0f\" \/><\/p>\n<p><strong>\u5728\u591a\u4e2a\u6d88\u8d39\u8005\u4e4b\u95f4\u5206\u914d\u4efb\u52a1\uff08\u7ade\u4e89\u7684\u6d88\u8d39\u8005\u6a21\u5f0f\uff09<\/strong>\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\u5bf9\u5e94\u591a\u4e2a\u6d88\u8d39\u8005\uff0c\u4e00\u822c\u9002\u7528\u4e8e\u6267\u884c\u8d44\u6e90\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5355\u4e2a\u6d88\u8d39\u8005\u5904\u7406\u4e0d\u8fc7\u6765\uff0c\u9700\u8981\u591a\u4e2a\u6d88\u8d39\u8005\u8fdb\u884c\u5904\u7406<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u4e00\u4e2a\u8ba2\u5355\u7684\u5904\u7406\u9700\u898110s\uff0c\u6709\u591a\u4e2a\u8ba2\u5355\u53ef\u4ee5\u540c\u65f6\u653e\u5230\u6d88\u606f\u961f\u5217\uff0c\u7136\u540e\u8ba9\u591a\u4e2a\u6d88\u8d39\u8005\u540c\u65f6\u5904\u7406\uff0c\u8fd9\u6837\u5c31\u662f\u5e76\u884c\u4e86\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u6d88\u8d39\u8005\u7684\u4e32\u884c\u60c5\u51b5<\/p>\n<h3>\u8ba2\u9605\u6a21\u5f0f\uff08Publish\/Subscribe\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u8ba2\u9605\u6a21\u5f0f.png\" alt=\"RabbitMQ\u8ba2\u9605\u6a21\u5f0f\" \/><\/p>\n<p><strong>\u4e00\u6b21\u5411\u8bb8\u591a\u6d88\u8d39\u8005\u53d1\u9001\u6d88\u606f<\/strong>\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\u53d1\u9001\u7684\u6d88\u606f\u4f1a\u88ab\u591a\u4e2a\u6d88\u8d39\u8005\u83b7\u53d6\uff0c\u4e5f\u5c31\u662f\u5c06\u6d88\u606f\u5c06\u5e7f\u64ad\u5230\u6240\u6709\u7684\u6d88\u8d39\u8005\u4e2d\u3002<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u66f4\u65b0\u5546\u54c1\u5e93\u5b58\u540e\u9700\u8981\u901a\u77e5\u591a\u4e2a\u7f13\u5b58\u548c\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u8fd9\u91cc\u7684\u7ed3\u6784\u5e94\u8be5\u662f\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2afanout\u7c7b\u578b\u4ea4\u6362\u673a\u6247\u51fa\u4e24\u4e2a\u6d88\u606f\u961f\u5217\uff0c\u5206\u522b\u4e3a\u7f13\u5b58\u6d88\u606f\u961f\u5217\u3001\u6570\u636e\u5e93\u6d88\u606f\u961f\u5217<\/li>\n<li>\u4e00\u4e2a\u7f13\u5b58\u6d88\u606f\u961f\u5217\u5bf9\u5e94\u7740\u591a\u4e2a\u7f13\u5b58\u6d88\u8d39\u8005<\/li>\n<li>\u4e00\u4e2a\u6570\u636e\u5e93\u6d88\u606f\u961f\u5217\u5bf9\u5e94\u7740\u591a\u4e2a\u6570\u636e\u5e93\u6d88\u8d39\u8005<\/li>\n<\/ul>\n<h3>\u8def\u7531\u6a21\u5f0f\uff08Routing\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u8def\u7531\u6a21\u5f0f.png\" alt=\"RabbitMQ\u8def\u7531\u6a21\u5f0f\" \/><\/p>\n<p><strong>\u6709\u9009\u62e9\u5730\uff08Routing key\uff09\u63a5\u6536\u6d88\u606f<\/strong>\uff0c\u53d1\u9001\u6d88\u606f\u5230\u4ea4\u6362\u673a\u5e76\u4e14\u8981\u6307\u5b9a\u8def\u7531key \uff0c\u6d88\u8d39\u8005\u5c06\u961f\u5217\u7ed1\u5b9a\u5230\u4ea4\u6362\u673a\u65f6\u9700\u8981\u6307\u5b9a\u8def\u7531key\uff0c\u4ec5\u6d88\u8d39\u6307\u5b9a\u8def\u7531key\u7684\u6d88\u606f<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u5982\u5728\u5546\u54c1\u5e93\u5b58\u4e2d\u589e\u52a0\u4e861\u53f0iphone12\uff0ciphone12\u4fc3\u9500\u6d3b\u52a8\u6d88\u8d39\u8005\u6307\u5b9arouting key\u4e3aiphone12\uff0c\u53ea\u6709\u6b64\u4fc3\u9500\u6d3b\u52a8\u4f1a\u63a5\u6536\u5230\u6d88\u606f\uff0c\u5176\u5b83\u4fc3\u9500\u6d3b\u52a8\u4e0d\u5173\u5fc3\u4e5f\u4e0d\u4f1a\u6d88\u8d39\u6b64routing key\u7684\u6d88\u606f<\/p>\n<h3>\u4e3b\u9898\u6a21\u5f0f\uff08Topics\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u4e3b\u9898\u6a21\u5f0f.png\" alt=\"RabbitMQ\u4e3b\u9898\u6a21\u5f0f\" \/><\/p>\n<p><strong>\u6839\u636e\u4e3b\u9898\uff08Topics\uff09\u6765\u63a5\u6536\u6d88\u606f<\/strong>\uff0c\u5c06\u8def\u7531key\u548c\u67d0\u6a21\u5f0f\u8fdb\u884c\u5339\u914d\uff0c\u6b64\u65f6\u961f\u5217\u9700\u8981\u7ed1\u5b9a\u5728\u4e00\u4e2a\u6a21\u5f0f\u4e0a\uff0c<code>#<\/code>\u5339\u914d\u4e00\u4e2a\u8bcd\u6216\u591a\u4e2a\u8bcd\uff0c<code>*<\/code>\u53ea\u5339\u914d\u4e00\u4e2a\u8bcd<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u540c\u4e0a\uff0ciphone\u4fc3\u9500\u6d3b\u52a8\u53ef\u4ee5\u63a5\u6536\u4e3b\u9898\u4e3aiphone\u7684\u6d88\u606f\uff0c\u5982iphone12\u3001iphone13\u7b49<\/p>\n<h3>\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08RPC\uff09<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/RabbitMQ\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528.png\" alt=\"RabbitMQ\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\" \/><\/p>\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u529f\u80fd\u5e76\u7b49\u5f85\u7ed3\u679c\u5c31\u53ef\u4ee5\u4f7f\u7528RPC\uff0c\u5177\u4f53\u6d41\u7a0b\u53ef\u4ee5\u770b\u56fe\u3002<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u9700\u8981\u7b49\u5f85\u63a5\u53e3\u8fd4\u56de\u6570\u636e\uff0c\u5982\u8ba2\u5355\u652f\u4ed8<\/p>\n<h3>\u53d1\u5e03\u8005\u786e\u8ba4\uff08Publisher Confirms\uff09<\/h3>\n<p><strong>\u4e0e\u53d1\u5e03\u8005\u8fdb\u884c\u53ef\u9760\u7684\u53d1\u5e03\u786e\u8ba4<\/strong>\uff0c\u53d1\u5e03\u8005\u786e\u8ba4\u662fRabbitMQ\u6269\u5c55\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53ef\u9760\u7684\u53d1\u5e03\u3002\u5728\u901a\u9053\u4e0a\u542f\u7528\u53d1\u5e03\u8005\u786e\u8ba4\u540e\uff0cRabbitMQ\u5c06\u5f02\u6b65\u786e\u8ba4\u53d1\u9001\u8005\u53d1\u5e03\u7684\u6d88\u606f\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u5df2\u5728\u670d\u52a1\u5668\u7aef\u5904\u7406<br \/>\n<strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a\u5bf9\u4e8e\u6d88\u606f\u53ef\u9760\u6027\u8981\u6c42\u8f83\u9ad8\uff0c\u6bd4\u5982\u94b1\u5305\u6263\u6b3e<\/p>\n<h2>\u4ee3\u7801\u6f14\u793a<\/h2>\n<p>\u4ee3\u7801\u4e2d\u6ca1\u6709\u5bf9\u540e\u9762\u4e24\u79cd\u6a21\u5f0f\u6f14\u793a\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u81ea\u5df1\u7814\u7a76<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.rabbitmq&lt;\/groupId&gt;\n    &lt;artifactId&gt;amqp-client&lt;\/artifactId&gt;\n    &lt;version&gt;5.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u7b80\u5355\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Sender {\n\n    private final static String QUEUE_NAME = &quot;simple_queue&quot;;\n\n    public static void main(String[] args) throws IOException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        \/\/ \u58f0\u660e\u961f\u5217\n        \/\/ queue\uff1a\u961f\u5217\u540d\n        \/\/ durable\uff1a\u662f\u5426\u6301\u4e45\u5316\n        \/\/ exclusive\uff1a\u662f\u5426\u6392\u5916  \u5373\u53ea\u5141\u8bb8\u8be5channel\u8bbf\u95ee\u8be5\u961f\u5217   \u4e00\u822c\u7b49\u4e8etrue\u7684\u8bdd\u7528\u4e8e\u4e00\u4e2a\u961f\u5217\u53ea\u80fd\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u6765\u6d88\u8d39\u7684\u573a\u666f\n        \/\/ autoDelete\uff1a\u662f\u5426\u81ea\u52a8\u5220\u9664  \u6d88\u8d39\u5b8c\u5220\u9664\n        \/\/ arguments\uff1a\u5176\u4ed6\u5c5e\u6027\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n\n        \/\/\u6d88\u606f\u5185\u5bb9\n        String message = &quot;simplest mode message&quot;;\n        channel.basicPublish(&quot;&quot;, QUEUE_NAME, null, message.getBytes());\n        System.out.println(&quot;[x]Sent &#039;&quot; + message + &quot;&#039;&quot;);\n\n        \/\/\u6700\u540e\u5173\u95ed\u901a\u5173\u548c\u8fde\u63a5\n        channel.close();\n        connection.close();\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver {\n\n    private final static String QUEUE_NAME = &quot;simple_queue&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        \/\/ \u83b7\u53d6\u8fde\u63a5\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<h3>\u5de5\u4f5c\u961f\u5217\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver1 {\n\n    private final static String QUEUE_NAME = &quot;queue_work&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ \u540c\u4e00\u65f6\u523b\u670d\u52a1\u5668\u53ea\u4f1a\u53d1\u9001\u4e00\u6761\u6d88\u606f\u7ed9\u6d88\u8d39\u8005\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver2 {\n\n    private final static String QUEUE_NAME = &quot;queue_work&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ \u540c\u4e00\u65f6\u523b\u670d\u52a1\u5668\u53ea\u4f1a\u53d1\u9001\u4e00\u6761\u6d88\u606f\u7ed9\u6d88\u8d39\u8005\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Sender {\n\n    private final static String QUEUE_NAME = &quot;queue_work&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        \/\/ \u58f0\u660e\u961f\u5217\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        for (int i = 0; i &lt; 100; i++) {\n            String message = &quot;work mode message&quot; + i;\n            channel.basicPublish(&quot;&quot;, QUEUE_NAME, null, message.getBytes());\n            System.out.println(&quot;[x] Sent &#039;&quot; + message + &quot;&#039;&quot;);\n            Thread.sleep(i * 10);\n        }\n\n        channel.close();\n        connection.close();\n    }\n}<\/code><\/pre>\n<h3>\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\npublic class Receive1 {\n\n    private static final String EXCHANGE_NAME = &quot;logs&quot;;\n\n    public static void main(String[] argv) throws Exception {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.exchangeDeclare(EXCHANGE_NAME, &quot;fanout&quot;);\n        String queueName = channel.queueDeclare().getQueue();\n        channel.queueBind(queueName, EXCHANGE_NAME, &quot;&quot;);\n\n        System.out.println(&quot; [*] Waiting for messages. To exit press CTRL+C&quot;);\n\n        \/\/ \u8ba2\u9605\u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; + message + &quot;&#039;&quot;);\n        };\n\n        \/\/ \u6d88\u8d39\u8005\uff0c\u6709\u6d88\u606f\u65f6\u89e6\u53d1\u8ba2\u9605\u56de\u8c03\u51fd\u6570\n        channel.basicConsume(queueName, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\npublic class Receive2 {\n\n    private static final String EXCHANGE_NAME = &quot;logs&quot;;\n\n    public static void main(String[] argv) throws Exception {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.exchangeDeclare(EXCHANGE_NAME, &quot;fanout&quot;);\n        String queueName = channel.queueDeclare().getQueue();\n        channel.queueBind(queueName, EXCHANGE_NAME, &quot;&quot;);\n\n        System.out.println(&quot; [*] Waiting for messages. To exit press CTRL+C&quot;);\n\n        \/\/ \u8ba2\u9605\u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received2 &#039;&quot; + message + &quot;&#039;&quot;);\n        };\n\n        \/\/ \u6d88\u8d39\u8005\uff0c\u6709\u6d88\u606f\u65f6\u89e6\u53d1\u8ba2\u9605\u56de\u8c03\u51fd\u6570\n        channel.basicConsume(queueName, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\n\npublic class Sender {\n\n    private static final String EXCHANGE_NAME = &quot;logs&quot;;\n\n    public static void main(String[] argv) throws Exception {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.exchangeDeclare(EXCHANGE_NAME, &quot;fanout&quot;);\n\n        String message = &quot;publish subscribe message&quot;;\n        channel.basicPublish(EXCHANGE_NAME, &quot;&quot;, null, message.getBytes(&quot;UTF-8&quot;));\n        System.out.println(&quot; [x] Sent &#039;&quot; + message + &quot;&#039;&quot;);\n\n        channel.close();\n        connection.close();\n    }\n}<\/code><\/pre>\n<h3>\u8def\u7531\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver1 {\n\n    private final static String QUEUE_NAME = &quot;queue_routing&quot;;\n    private final static String EXCHANGE_NAME = &quot;exchange_direct&quot;;\n\n    public static void main(String[] args) throws IOException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ \u6307\u5b9a\u8def\u7531\u7684key\uff0c\u63a5\u6536key\u548ckey2\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;key&quot;);\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;key2&quot;);\n\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver2 {\n\n    private final static String QUEUE_NAME = &quot;queue_routing2&quot;;\n    private final static String EXCHANGE_NAME = &quot;exchange_direct&quot;;\n\n    public static void main(String[] args) throws IOException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ \u4ec5\u63a5\u6536key2\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;key2&quot;);\n\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Sender {\n\n    private final static String EXCHANGE_NAME = &quot;exchange_direct&quot;;\n    private final static String EXCHANGE_TYPE = &quot;direct&quot;;\n\n    public static void main(String[] args) throws IOException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        \/\/ \u4ea4\u6362\u673a\u58f0\u660e\n        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);\n\n        \/\/ \u53ea\u6709routingKey\u76f8\u540c\u7684\u624d\u4f1a\u6d88\u8d39\n        String message = &quot;routing mode message&quot;;\n        channel.basicPublish(EXCHANGE_NAME, &quot;key2&quot;, null, message.getBytes());\n        System.out.println(&quot;[x] Sent &#039;&quot; + message + &quot;&#039;&quot;);\n\/\/        channel.basicPublish(EXCHANGE_NAME, &quot;key&quot;, null, message.getBytes());\n\/\/        System.out.println(&quot;[x] Sent &#039;&quot; + message + &quot;&#039;&quot;);\n\n        channel.close();\n        connection.close();\n    }\n}<\/code><\/pre>\n<h3>\u4e3b\u9898\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver1 {\n\n    private final static String QUEUE_NAME = &quot;queue_topic&quot;;\n    private final static String EXCHANGE_NAME = &quot;exchange_topic&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ \u53ef\u4ee5\u63a5\u6536key.1\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;key.*&quot;);\n\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\nimport com.rabbitmq.client.DeliverCallback;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Receiver2 {\n\n    private final static String QUEUE_NAME = &quot;queue_topic2&quot;;\n    private final static String EXCHANGE_NAME = &quot;exchange_topic&quot;;\n    private final static String EXCHANGE_TYPE = &quot;topic&quot;;\n\n    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.queueDeclare(QUEUE_NAME, false, false, false, null);\n        \/\/ *\u53f7\u4ee3\u8868\u5355\u4e2a\u5355\u8bcd\uff0c\u53ef\u4ee5\u63a5\u6536key.1\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;*.*&quot;);\n        \/\/ #\u53f7\u4ee3\u8868\u591a\u4e2a\u5355\u8bcd\uff0c\u53ef\u4ee5\u63a5\u6536key.1.2\n        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, &quot;*.#&quot;);\n\n        channel.basicQos(1);\n\n        DeliverCallback deliverCallback = (consumerTag, delivery) -&gt; {\n            String message = new String(delivery.getBody(), &quot;UTF-8&quot;);\n            System.out.println(&quot; [x] Received &#039;&quot; +\n                    delivery.getEnvelope().getRoutingKey() + &quot;&#039;:&#039;&quot; + message + &quot;&#039;&quot;);\n        };\n        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -&gt; {\n        });\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import com.rabbitmq.client.Channel;\nimport com.rabbitmq.client.Connection;\nimport com.rabbitmq.client.ConnectionFactory;\n\nimport java.io.IOException;\nimport java.util.concurrent.TimeoutException;\n\npublic class Sender {\n\n    private final static String EXCHANGE_NAME = &quot;exchange_topic&quot;;\n    private final static String EXCHANGE_TYPE = &quot;topic&quot;;\n\n    public static void main(String[] args) throws IOException, TimeoutException {\n        ConnectionFactory factory = new ConnectionFactory();\n        factory.setHost(&quot;localhost&quot;);\n        factory.setPort(5672);\n        Connection connection = factory.newConnection();\n        Channel channel = connection.createChannel();\n\n        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);\n\n        String message = &quot;topics model message with key.1&quot;;\n        channel.basicPublish(EXCHANGE_NAME, &quot;key.1&quot;, null, message.getBytes());\n        System.out.println(&quot;[x] Sent &#039;&quot; + message + &quot;&#039;&quot;);\n        String message2 = &quot;topics model message with key.1.2&quot;;\n        channel.basicPublish(EXCHANGE_NAME, &quot;key.1.2&quot;, null, message2.getBytes());\n        System.out.println(&quot;[x] Sent &#039;&quot; + message2 + &quot;&#039;&quot;);\n\n        channel.close();\n        connection.close();\n    }\n}<\/code><\/pre>\n<h2>\u56db\u79cd\u4ea4\u6362\u673a\u4ecb\u7ecd<\/h2>\n<ul>\n<li>\u76f4\u8fde\u4ea4\u6362\u673a\uff08Direct exchange\uff09\uff1a\u5177\u6709\u8def\u7531\u529f\u80fd\u7684\u4ea4\u6362\u673a\uff0c\u7ed1\u5b9a\u5230\u6b64\u4ea4\u6362\u673a\u7684\u65f6\u5019\u9700\u8981\u6307\u5b9a\u4e00\u4e2a<code>routing_key<\/code>\uff0c\u4ea4\u6362\u673a\u53d1\u9001\u6d88\u606f\u7684\u65f6\u5019\u9700\u8981<code>routing_key<\/code>\uff0c\u4f1a\u5c06\u6d88\u606f\u53d1\u9001\u9053\u5bf9\u5e94\u7684\u961f\u5217<\/li>\n<li>\u6247\u5f62\u4ea4\u6362\u673a\uff08Fanout exchange\uff09\uff1a\u5e7f\u64ad\u6d88\u606f\u5230\u6240\u6709\u961f\u5217\uff0c\u6ca1\u6709\u4efb\u4f55\u5904\u7406\uff0c\u901f\u5ea6\u6700\u5feb<\/li>\n<li>\u4e3b\u9898\u4ea4\u6362\u673a\uff08Topic exchange\uff09\uff1a\u5728\u76f4\u8fde\u4ea4\u6362\u673a\u57fa\u7840\u4e0a\u589e\u52a0\u6a21\u5f0f\u5339\u914d\uff0c\u4e5f\u5c31\u662f\u5bf9<code>routing_key<\/code>\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\uff0c<code>*<\/code>\u4ee3\u8868\u4e00\u4e2a\u5355\u8bcd\uff0c<code>#<\/code>\u4ee3\u8868\u591a\u4e2a\u5355\u8bcd<\/li>\n<li>\u9996\u90e8\u4ea4\u6362\u673a\uff08Headers exchange\uff09\uff1a\u5ffd\u7565<code>routing_key<\/code>\uff0c\u4f7f\u7528Headers\u4fe1\u606f\uff08\u4e00\u4e2aHash\u7684\u6570\u636e\u7ed3\u6784\uff09\u8fdb\u884c\u5339\u914d\uff0c\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u6709\u66f4\u591a\u66f4\u7075\u6d3b\u7684\u5339\u914d\u89c4\u5219<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd9\u4e48\u591a\u79cd\u961f\u5217\u6a21\u5f0f\u4e2d\u90fd\u6709\u5176\u5e94\u7528\u573a\u666f\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u573a\u666f\u793a\u4f8b\u4e2d\u8fdb\u884c\u9009\u62e9<\/p>\n<p>\u53c2\u8003<\/p>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.rabbitmq.com\/getstarted.html\" title=\"RabbitMQ\u5b98\u65b9\u6559\u7a0b\">RabbitMQ\u5b98\u65b9\u6559\u7a0b<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/rabbitmq\/rabbitmq-tutorials\" title=\"\u5b98\u65b9\u6559\u7a0b\u6e90\u7801\">\u5b98\u65b9\u6559\u7a0b\u6e90\u7801<\/a><\/li>\n<\/ul>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/blog.csdn.net\/qq_32828253\/article\/details\/110450249\">https:\/\/blog.csdn.net\/qq_32828253\/article\/details\/110450249<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e03\u79cd\u6a21\u5f0f\u4ecb\u7ecd\u4e0e\u5e94\u7528\u573a\u666f \u7b80\u5355\u6a21\u5f0f\uff08Hello World\uff09 \u505a\u6700\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\u5bf9\u5e94\u4e00\u4e2a\u6d88\u8d39\u8005\uff0cRabb [&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-2161","post","type-post","status-publish","format-standard","hentry","category-rabbitmq"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2161","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=2161"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2161\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}