{"id":38016,"date":"2025-05-19T14:13:34","date_gmt":"2025-05-19T06:13:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38016.html"},"modified":"2025-05-19T14:13:34","modified_gmt":"2025-05-19T06:13:34","slug":"spring-boot-%e4%b8%8e-rabbitmq-%e7%9a%84%e6%b7%b1%e5%ba%a6%e9%9b%86%e6%88%90%e5%ae%9e%e8%b7%b5%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38016.html","title":{"rendered":"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09"},"content":{"rendered":"<h3>\u96c6\u6210\u6b65\u9aa4\u8be6\u89e3<\/h3>\n<h4>\u914d\u7f6e RabbitMQ \u8fde\u63a5\u4fe1\u606f<\/h4>\n<p>\u5728 Spring Boot \u9879\u76ee\u4e2d&#xff0c;\u901a\u5e38\u5728application.properties\u6216application.yml\u6587\u4ef6\u4e2d\u914d\u7f6e RabbitMQ \u7684\u8fde\u63a5\u4fe1\u606f\u3002\u4ee5application.yml\u4e3a\u4f8b&#xff0c;\u914d\u7f6e\u5982\u4e0b&#xff1a;<\/p>\n<p>spring:<\/p>\n<p>rabbitmq:<\/p>\n<p>host: localhost<\/p>\n<p>port: 5672<\/p>\n<p>username: guest<\/p>\n<p>password: guest<\/p>\n<p>virtual &#8211; host: \/<\/p>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4e2d&#xff1a;<\/p>\n<ul>\n<li>host\u6307\u5b9a\u4e86 RabbitMQ \u670d\u52a1\u5668\u7684\u5730\u5740&#xff0c;\u8fd9\u91cc\u5047\u8bbe RabbitMQ \u8fd0\u884c\u5728\u672c\u5730\u3002<\/li>\n<\/ul>\n<ul>\n<li>port\u6307\u5b9a\u4e86 RabbitMQ \u670d\u52a1\u7684\u7aef\u53e3&#xff0c;\u9ed8\u8ba4\u7aef\u53e3\u662f 5672\u3002<\/li>\n<\/ul>\n<ul>\n<li>username\u548cpassword\u662f\u8fde\u63a5 RabbitMQ \u670d\u52a1\u5668\u6240\u9700\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801&#xff0c;\u9ed8\u8ba4\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u90fd\u662fguest\u3002\u4e0d\u8fc7\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u5e94\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u7ec4\u5408\u3002<\/li>\n<\/ul>\n<ul>\n<li>virtual &#8211; host\u8868\u793a\u865a\u62df\u4e3b\u673a&#xff0c;\u5b83\u7528\u4e8e\u903b\u8f91\u9694\u79bb\u4e0d\u540c\u7684\u5e94\u7528\u6216\u4e1a\u52a1\u6a21\u5757\u4e4b\u95f4\u7684\u6d88\u606f\u961f\u5217\u3002\u8fd9\u91cc\u4f7f\u7528\u6839\u865a\u62df\u4e3b\u673a\/&#xff0c;\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u521b\u5efa\u4e0d\u540c\u7684\u865a\u62df\u4e3b\u673a\u3002\u4f8b\u5982&#xff0c;\u5bf9\u4e8e\u4e0d\u540c\u7684\u4e1a\u52a1\u7ebf&#xff0c;\u53ef\u4ee5\u5206\u522b\u4f7f\u7528\/business1\u3001\/business2\u7b49\u865a\u62df\u4e3b\u673a&#xff0c;\u6bcf\u4e2a\u865a\u62df\u4e3b\u673a\u6709\u72ec\u7acb\u7684\u961f\u5217\u3001\u4ea4\u6362\u673a\u548c\u6743\u9650\u63a7\u5236 \u3002<\/li>\n<\/ul>\n<h4>\u521b\u5efa\u6d88\u606f\u961f\u5217\u548c\u4ea4\u6362\u673a<\/h4>\n<p>\u901a\u8fc7\u914d\u7f6e\u7c7b\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u6d88\u606f\u961f\u5217\u3001\u4ea4\u6362\u673a\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7ed1\u5b9a\u5173\u7cfb\u3002\u5728 Spring Boot \u4e2d&#xff0c;\u4f7f\u7528&#064;Configuration\u6ce8\u89e3\u6765\u5b9a\u4e49\u914d\u7f6e\u7c7b&#xff0c;\u5e76\u4f7f\u7528&#064;Bean\u6ce8\u89e3\u521b\u5efa\u6240\u9700\u7684 Bean\u3002<\/p>\n<p>\u4e0b\u9762\u5206\u522b\u5c55\u793adirect\u3001topic\u3001fanout\u7c7b\u578b\u4ea4\u6362\u673a\u7684\u914d\u7f6e\u793a\u4f8b&#xff1a;<\/p>\n<p>Direct Exchange&#xff08;\u76f4\u8fde\u4ea4\u6362\u673a&#xff09;\u914d\u7f6e\u793a\u4f8b&#xff1a;<\/p>\n<p>import org.springframework.amqp.core.Binding;<\/p>\n<p>import org.springframework.amqp.core.BindingBuilder;<\/p>\n<p>import org.springframework.amqp.core.Queue;<\/p>\n<p>import org.springframework.amqp.core.DirectExchange;<\/p>\n<p>import org.springframework.context.annotation.Bean;<\/p>\n<p>import org.springframework.context.annotation.Configuration;<\/p>\n<p>&#064;Configuration<\/p>\n<p>public class DirectRabbitConfig {<\/p>\n<p>\/\/ \u5b9a\u4e49\u961f\u5217<\/p>\n<p>&#064;Bean<\/p>\n<p>public Queue directQueue() {<\/p>\n<p>return new Queue(&#034;direct.queue&#034;, true);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u76f4\u8fde\u4ea4\u6362\u673a<\/p>\n<p>&#064;Bean<\/p>\n<p>public DirectExchange directExchange() {<\/p>\n<p>return new DirectExchange(&#034;direct.exchange&#034;, true, false);<\/p>\n<p>}<\/p>\n<p>\/\/ \u7ed1\u5b9a\u961f\u5217\u548c\u4ea4\u6362\u673a&#xff0c;\u8bbe\u7f6e\u8def\u7531\u952e<\/p>\n<p>&#064;Bean<\/p>\n<p>public Binding directBinding() {<\/p>\n<p>return BindingBuilder.bind(directQueue()).to(directExchange()).with(&#034;direct.routing.key&#034;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u4e2d&#xff1a;<\/p>\n<ul>\n<li>directQueue\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3adirect.queue\u7684\u961f\u5217&#xff0c;true\u8868\u793a\u8be5\u961f\u5217\u662f\u6301\u4e45\u5316\u7684&#xff0c;\u5373 RabbitMQ \u670d\u52a1\u5668\u91cd\u542f\u540e\u961f\u5217\u4f9d\u7136\u5b58\u5728\u3002<\/li>\n<\/ul>\n<ul>\n<li>directExchange\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3adirect.exchange\u7684\u76f4\u8fde\u4ea4\u6362\u673a&#xff0c;\u540c\u6837\u8bbe\u7f6e\u4e3a\u6301\u4e45\u5316\u3002<\/li>\n<\/ul>\n<ul>\n<li>directBinding\u65b9\u6cd5\u901a\u8fc7BindingBuilder\u5c06\u961f\u5217\u548c\u4ea4\u6362\u673a\u8fdb\u884c\u7ed1\u5b9a&#xff0c;\u5e76\u6307\u5b9a\u4e86\u8def\u7531\u952edirect.routing.key\u3002\u76f4\u8fde\u4ea4\u6362\u673a\u7684\u7279\u70b9\u662f\u6839\u636e\u8def\u7531\u952e\u8fdb\u884c\u7cbe\u786e\u5339\u914d&#xff0c;\u53ea\u6709\u5f53\u6d88\u606f\u7684\u8def\u7531\u952e\u4e0e\u7ed1\u5b9a\u7684\u8def\u7531\u952e\u5b8c\u5168\u4e00\u81f4\u65f6&#xff0c;\u6d88\u606f\u624d\u4f1a\u88ab\u8def\u7531\u5230\u5bf9\u5e94\u7684\u961f\u5217\u3002<\/li>\n<\/ul>\n<p>Topic Exchange&#xff08;\u4e3b\u9898\u4ea4\u6362\u673a&#xff09;\u914d\u7f6e\u793a\u4f8b&#xff1a;<\/p>\n<p>import org.springframework.amqp.core.Binding;<\/p>\n<p>import org.springframework.amqp.core.BindingBuilder;<\/p>\n<p>import org.springframework.amqp.core.Queue;<\/p>\n<p>import org.springframework.amqp.core.TopicExchange;<\/p>\n<p>import org.springframework.context.annotation.Bean;<\/p>\n<p>import org.springframework.context.annotation.Configuration;<\/p>\n<p>&#064;Configuration<\/p>\n<p>public class TopicRabbitConfig {<\/p>\n<p>\/\/ \u5b9a\u4e49\u7b2c\u4e00\u4e2a\u961f\u5217<\/p>\n<p>&#064;Bean<\/p>\n<p>public Queue topicQueue1() {<\/p>\n<p>return new Queue(&#034;topic.queue1&#034;, true);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u7b2c\u4e8c\u4e2a\u961f\u5217<\/p>\n<p>&#064;Bean<\/p>\n<p>public Queue topicQueue2() {<\/p>\n<p>return new Queue(&#034;topic.queue2&#034;, true);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e3b\u9898\u4ea4\u6362\u673a<\/p>\n<p>&#064;Bean<\/p>\n<p>public TopicExchange topicExchange() {<\/p>\n<p>return new TopicExchange(&#034;topic.exchange&#034;, true, false);<\/p>\n<p>}<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7b2c\u4e00\u4e2a\u961f\u5217\u548c\u4ea4\u6362\u673a&#xff0c;\u8bbe\u7f6e\u8def\u7531\u952e\u6a21\u5f0f\u4e3atopic.#<\/p>\n<p>&#064;Bean<\/p>\n<p>public Binding topicBinding1() {<\/p>\n<p>return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(&#034;topic.#&#034;);<\/p>\n<p>}<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7b2c\u4e8c\u4e2a\u961f\u5217\u548c\u4ea4\u6362\u673a&#xff0c;\u8bbe\u7f6e\u8def\u7531\u952e\u6a21\u5f0f\u4e3atopic.man<\/p>\n<p>&#064;Bean<\/p>\n<p>public Binding topicBinding2() {<\/p>\n<p>return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(&#034;topic.man&#034;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u8fd9\u91cc&#xff1a;<\/p>\n<ul>\n<li>topicQueue1\u548ctopicQueue2\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u6301\u4e45\u5316\u961f\u5217\u3002<\/li>\n<\/ul>\n<ul>\n<li>topicExchange\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3atopic.exchange\u7684\u4e3b\u9898\u4ea4\u6362\u673a\u3002<\/li>\n<\/ul>\n<ul>\n<li>topicBinding1\u5c06topicQueue1\u4e0etopicExchange\u7ed1\u5b9a&#xff0c;\u5e76\u4f7f\u7528topic.#\u4f5c\u4e3a\u8def\u7531\u952e\u6a21\u5f0f&#xff0c;\u5176\u4e2d#\u662f\u901a\u914d\u7b26&#xff0c;\u8868\u793a\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u3002\u8fd9\u610f\u5473\u7740\u53ea\u8981\u6d88\u606f\u7684\u8def\u7531\u952e\u4ee5topic.\u5f00\u5934&#xff0c;\u5c31\u4f1a\u88ab\u8def\u7531\u5230topicQueue1\u3002<\/li>\n<\/ul>\n<ul>\n<li>topicBinding2\u5c06topicQueue2\u4e0etopicExchange\u7ed1\u5b9a&#xff0c;\u8def\u7531\u952e\u6a21\u5f0f\u4e3atopic.man&#xff0c;\u53ea\u6709\u5f53\u6d88\u606f\u7684\u8def\u7531\u952e\u4e3atopic.man\u65f6&#xff0c;\u6d88\u606f\u624d\u4f1a\u88ab\u8def\u7531\u5230topicQueue2\u3002\u4e3b\u9898\u4ea4\u6362\u673a\u901a\u8fc7\u901a\u914d\u7b26\u6a21\u5f0f\u5b9e\u73b0\u4e86\u66f4\u7075\u6d3b\u7684\u6d88\u606f\u8def\u7531&#xff0c;\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u4e0d\u540c\u89c4\u5219\u8fdb\u884c\u6d88\u606f\u5206\u53d1\u7684\u573a\u666f \u3002<\/li>\n<\/ul>\n<p>Fanout Exchange&#xff08;\u6247\u5f62\u4ea4\u6362\u673a&#xff09;\u914d\u7f6e\u793a\u4f8b&#xff1a;<\/p>\n<p>import org.springframework.amqp.core.Binding;<\/p>\n<p>import org.springframework.amqp.core.BindingBuilder;<\/p>\n<p>import org.springframework.amqp.core.Queue;<\/p>\n<p>import org.springframework.amqp.core.FanoutExchange;<\/p>\n<p>import org.springframework.context.annotation.Bean;<\/p>\n<p>import org.springframework.context.annotation.Configuration;<\/p>\n<p>&#064;Configuration<\/p>\n<p>public class FanoutRabbitConfig {<\/p>\n<p>\/\/ \u5b9a\u4e49\u7b2c\u4e00\u4e2a\u961f\u5217<\/p>\n<p>&#064;Bean<\/p>\n<p>public Queue fanoutQueue1() {<\/p>\n<p>return new Queue(&#034;fanout.queue1&#034;, true);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u7b2c\u4e8c\u4e2a\u961f\u5217<\/p>\n<p>&#064;Bean<\/p>\n<p>public Queue fanoutQueue2() {<\/p>\n<p>return new Queue(&#034;fanout.queue2&#034;, true);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5b9a\u4e49\u6247\u5f62\u4ea4\u6362\u673a<\/p>\n<p>&#064;Bean<\/p>\n<p>public FanoutExchange fanoutExchange() {<\/p>\n<p>return new FanoutExchange(&#034;fanout.exchange&#034;, true, false);<\/p>\n<p>}<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7b2c\u4e00\u4e2a\u961f\u5217\u548c\u4ea4\u6362\u673a<\/p>\n<p>&#064;Bean<\/p>\n<p>public Binding fanoutBinding1() {<\/p>\n<p>return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());<\/p>\n<p>}<\/p>\n<p>\/\/ \u7ed1\u5b9a\u7b2c\u4e8c\u4e2a\u961f\u5217\u548c\u4ea4\u6362\u673a<\/p>\n<p>&#064;Bean<\/p>\n<p>public Binding fanoutBinding2() {<\/p>\n<p>return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u4e2d&#xff1a;<\/p>\n<ul>\n<li>fanoutQueue1\u548cfanoutQueue2\u521b\u5efa\u4e86\u4e24\u4e2a\u6301\u4e45\u5316\u961f\u5217\u3002<\/li>\n<\/ul>\n<ul>\n<li>fanoutExchange\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3afanout.exchange\u7684\u6247\u5f62\u4ea4\u6362\u673a\u3002<\/li>\n<\/ul>\n<ul>\n<li>fanoutBinding1\u548cfanoutBinding2\u5206\u522b\u5c06\u4e24\u4e2a\u961f\u5217\u4e0e\u6247\u5f62\u4ea4\u6362\u673a\u8fdb\u884c\u7ed1\u5b9a\u3002\u6247\u5f62\u4ea4\u6362\u673a\u7684\u7279\u70b9\u662f\u5c06\u6d88\u606f\u5e7f\u64ad\u5230\u6240\u6709\u4e0e\u4e4b\u7ed1\u5b9a\u7684\u961f\u5217&#xff0c;\u800c\u4e0d\u8003\u8651\u8def\u7531\u952e&#xff0c;\u53ea\u8981\u6709\u961f\u5217\u7ed1\u5b9a\u5230\u8be5\u4ea4\u6362\u673a&#xff0c;\u6d88\u606f\u5c31\u4f1a\u88ab\u53d1\u9001\u5230\u8fd9\u4e9b\u961f\u5217&#xff0c;\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6d88\u606f\u540c\u65f6\u53d1\u9001\u7ed9\u591a\u4e2a\u6d88\u8d39\u8005\u7684\u573a\u666f \u3002<\/li>\n<\/ul>\n<h4>\u7f16\u5199\u6d88\u606f\u751f\u4ea7\u8005<\/h4>\n<p>\u521b\u5efa\u4e00\u4e2a\u6d88\u606f\u751f\u4ea7\u8005\u7c7b&#xff0c;\u901a\u8fc7\u6ce8\u5165RabbitTemplate\u6765\u53d1\u9001\u6d88\u606f\u3002RabbitTemplate\u662f Spring AMQP \u63d0\u4f9b\u7684\u7528\u4e8e\u53d1\u9001\u6d88\u606f\u7684\u6838\u5fc3\u7c7b&#xff0c;\u5b83\u5c01\u88c5\u4e86\u4e0e RabbitMQ \u4ea4\u4e92\u7684\u7ec6\u8282&#xff0c;\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u6d88\u606f\u53d1\u9001\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6d88\u606f\u751f\u4ea7\u8005\u7c7b\u7684\u793a\u4f8b&#xff0c;\u5305\u542b\u666e\u901a\u6d88\u606f\u53d1\u9001\u548c\u5e26\u81ea\u5b9a\u4e49\u5c5e\u6027\u6d88\u606f\u53d1\u9001\u7684\u793a\u4f8b&#xff1a;<\/p>\n<p>import org.springframework.amqp.rabbit.core.RabbitTemplate;<\/p>\n<p>import org.springframework.beans.factory.annotation.Autowired;<\/p>\n<p>import org.springframework.stereotype.Service;<\/p>\n<p>import java.util.HashMap;<\/p>\n<p>import java.util.Map;<\/p>\n<p>&#064;Service<\/p>\n<p>public class RabbitMQProducer {<\/p>\n<p>&#064;Autowired<\/p>\n<p>private RabbitTemplate rabbitTemplate;<\/p>\n<p>\/\/ \u53d1\u9001\u666e\u901a\u6d88\u606f<\/p>\n<p>public void sendMessage(String exchange, String routingKey, String message) {<\/p>\n<p>rabbitTemplate.convertAndSend(exchange, routingKey, message);<\/p>\n<p>System.out.println(&#034;Sent message: &#034; &#043; message);<\/p>\n<p>}<\/p>\n<p>\/\/ \u53d1\u9001\u5e26\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u6d88\u606f<\/p>\n<p>public void sendMessageWithProperties(String exchange, String routingKey, String message) {<\/p>\n<p>Map&lt;String, Object&gt; headers &#061; new HashMap&lt;&gt;();<\/p>\n<p>headers.put(&#034;customHeader&#034;, &#034;customValue&#034;);<\/p>\n<p>rabbitTemplate.convertAndSend(exchange, routingKey, message, messagePostProcessor -&gt; {<\/p>\n<p>messagePostProcessor.getMessageProperties().setHeaders(headers);<\/p>\n<p>return messagePostProcessor;<\/p>\n<p>});<\/p>\n<p>System.out.println(&#034;Sent message with properties: &#034; &#043; message);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u5728\u8fd9\u4e2a\u751f\u4ea7\u8005\u7c7b\u4e2d&#xff1a;<\/p>\n<ul>\n<li>sendMessage\u65b9\u6cd5\u63a5\u6536\u4ea4\u6362\u673a\u540d\u79f0\u3001\u8def\u7531\u952e\u548c\u6d88\u606f\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u901a\u8fc7rabbitTemplate.convertAndSend\u65b9\u6cd5\u5c06\u6d88\u606f\u53d1\u9001\u5230\u6307\u5b9a\u7684\u4ea4\u6362\u673a\u548c\u961f\u5217\u3002\u8be5\u65b9\u6cd5\u4f1a\u6839\u636e\u8def\u7531\u952e\u5c06\u6d88\u606f\u8def\u7531\u5230\u5bf9\u5e94\u7684\u961f\u5217&#xff0c;\u5982\u679c\u8def\u7531\u952e\u5339\u914d\u6210\u529f&#xff0c;\u6d88\u606f\u5c31\u4f1a\u88ab\u5b58\u50a8\u5728\u961f\u5217\u4e2d\u7b49\u5f85\u6d88\u8d39\u8005\u83b7\u53d6\u3002<\/li>\n<\/ul>\n<ul>\n<li>sendMessageWithProperties\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u53d1\u9001\u5e26\u6709\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u6d88\u606f\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2aMap\u6765\u5b58\u50a8\u81ea\u5b9a\u4e49\u5c5e\u6027&#xff0c;\u8fd9\u91cc\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3acustomHeader&#xff0c;\u503c\u4e3acustomValue\u7684\u5c5e\u6027\u3002\u7136\u540e\u4f7f\u7528rabbitTemplate.convertAndSend\u7684\u53e6\u4e00\u4e2a\u91cd\u8f7d\u65b9\u6cd5&#xff0c;\u8be5\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2aMessagePostProcessor\u4f5c\u4e3a\u53c2\u6570\u3002\u5728MessagePostProcessor\u7684\u56de\u8c03\u51fd\u6570\u4e2d&#xff0c;\u5c06\u81ea\u5b9a\u4e49\u5c5e\u6027\u8bbe\u7f6e\u5230\u6d88\u606f\u7684\u5c5e\u6027\u4e2d&#xff0c;\u4ece\u800c\u5b9e\u73b0\u53d1\u9001\u5e26\u6709\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u6d88\u606f \u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528&#xff0c;\u4f8b\u5982\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u5c5e\u6027\u6765\u4f20\u9012\u4e00\u4e9b\u989d\u5916\u7684\u5143\u6570\u636e&#xff0c;\u5982\u6d88\u606f\u7684\u4f18\u5148\u7ea7\u3001\u521b\u5efa\u65f6\u95f4\u7b49&#xff0c;\u4ee5\u4fbf\u6d88\u8d39\u8005\u5728\u5904\u7406\u6d88\u606f\u65f6\u6839\u636e\u8fd9\u4e9b\u5c5e\u6027\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u903b\u8f91\u3002<\/li>\n<\/ul>\n<h4>\u7f16\u5199\u6d88\u606f\u6d88\u8d39\u8005<\/h4>\n<p>\u521b\u5efa\u6d88\u606f\u6d88\u8d39\u8005\u7c7b&#xff0c;\u4f7f\u7528&#064;RabbitListener\u6ce8\u89e3\u6765\u76d1\u542c\u6307\u5b9a\u7684\u961f\u5217&#xff0c;\u5e76\u5904\u7406\u63a5\u6536\u5230\u7684\u6d88\u606f\u3002&#064;RabbitListener\u662f Spring AMQP \u63d0\u4f9b\u7684\u6ce8\u89e3&#xff0c;\u7528\u4e8e\u58f0\u660e\u4e00\u4e2a\u65b9\u6cd5\u4f5c\u4e3a\u6d88\u606f\u76d1\u542c\u5668&#xff0c;\u5f53\u961f\u5217\u4e2d\u6709\u65b0\u6d88\u606f\u65f6&#xff0c;\u8be5\u65b9\u6cd5\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6d88\u606f\u6d88\u8d39\u8005\u7c7b\u7684\u793a\u4f8b&#xff0c;\u6db5\u76d6\u6d88\u606f\u5904\u7406\u903b\u8f91\u548c\u5f02\u5e38\u5904\u7406&#xff1a;<\/p>\n<p>import org.springframework.amqp.rabbit.annotation.RabbitListener;<\/p>\n<p>import org.springframework.stereotype.Component;<\/p>\n<p>&#064;Component<\/p>\n<p>public class RabbitMQConsumer {<\/p>\n<p>&#064;RabbitListener(queues &#061; &#034;direct.queue&#034;)<\/p>\n<p>public void receiveMessage(String message) {<\/p>\n<p>try {<\/p>\n<p>\/\/ \u6d88\u606f\u5904\u7406\u903b\u8f91<\/p>\n<p>System.out.println(&#034;Received message: &#034; &#043; message);<\/p>\n<p>\/\/ \u6a21\u62df\u4e1a\u52a1\u5904\u7406<\/p>\n<p>Thread.sleep(1000);<\/p>\n<p>System.out.println(&#034;Message processed successfully.&#034;);<\/p>\n<p>} catch (Exception e) {<\/p>\n<p>\/\/ \u5f02\u5e38\u5904\u7406<\/p>\n<p>System.out.println(&#034;Error processing message: &#034; &#043; e.getMessage());<\/p>\n<p>\/\/ \u53ef\u4ee5\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u91cd\u8bd5\u3001\u8bb0\u5f55\u65e5\u5fd7\u6216\u5176\u4ed6\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u5728\u8fd9\u4e2a\u6d88\u8d39\u8005\u7c7b\u4e2d&#xff1a;<\/p>\n<ul>\n<li>&#064;RabbitListener(queues &#061; &#034;direct.queue&#034;)\u6ce8\u89e3\u8868\u793a\u8be5\u65b9\u6cd5\u76d1\u542c\u540d\u4e3adirect.queue\u7684\u961f\u5217\u3002\u5f53\u8be5\u961f\u5217\u4e2d\u6709\u6d88\u606f\u65f6&#xff0c;receiveMessage\u65b9\u6cd5\u4f1a\u88ab\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u5728receiveMessage\u65b9\u6cd5\u4e2d&#xff0c;\u9996\u5148\u6253\u5370\u63a5\u6536\u5230\u7684\u6d88\u606f&#xff0c;\u7136\u540e\u901a\u8fc7Thread.sleep(1000)\u6a21\u62df\u4e1a\u52a1\u5904\u7406\u8fc7\u7a0b&#xff0c;\u8fd9\u91cc\u8ba9\u7ebf\u7a0b\u7761\u7720 1 \u79d2\u6765\u6a21\u62df\u4e00\u4e9b\u8017\u65f6\u64cd\u4f5c\u3002\u5982\u679c\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38&#xff0c;\u4f1a\u6355\u83b7\u5f02\u5e38\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5f02\u5e38\u5904\u7406&#xff0c;\u4f8b\u5982\u5c06\u6d88\u606f\u653e\u5165\u6b7b\u4fe1\u961f\u5217\u8fdb\u884c\u540e\u7eed\u5904\u7406&#xff0c;\u6216\u8005\u8fdb\u884c\u6d88\u606f\u91cd\u8bd5\u3002\u5982\u679c\u662f\u56e0\u4e3a\u7f51\u7edc\u6ce2\u52a8\u7b49\u4e34\u65f6\u539f\u56e0\u5bfc\u81f4\u6d88\u606f\u5904\u7406\u5931\u8d25&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u91cd\u8bd5\u673a\u5236&#xff0c;\u8ba9\u6d88\u606f\u91cd\u65b0\u88ab\u6d88\u8d39&#xff1b;\u5982\u679c\u662f\u56e0\u4e3a\u6d88\u606f\u5185\u5bb9\u672c\u8eab\u9519\u8bef\u7b49\u4e0d\u53ef\u6062\u590d\u7684\u539f\u56e0&#xff0c;\u53ef\u4ee5\u5c06\u6d88\u606f\u653e\u5165\u6b7b\u4fe1\u961f\u5217&#xff0c;\u4ee5\u4fbf\u540e\u7eed\u8fdb\u884c\u4eba\u5de5\u5904\u7406\u6216\u5206\u6790 \u3002\u901a\u8fc7\u5408\u7406\u7684\u5f02\u5e38\u5904\u7406&#xff0c;\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027&#xff0c;\u786e\u4fdd\u6d88\u606f\u80fd\u591f\u88ab\u6b63\u786e\u5904\u7406\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb732\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u6ce8\u89e3\u9a71\u52a8\u5f00\u53d1\u3001\u6d88\u606f\u8f6c\u6362\u5668\u914d\u7f6e\u3001\u6d88\u8d39\u8005\u76d1\u542c\u5668\u4e0e\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3303,663,98],"topic":[],"class_list":["post-38016","post","type-post","status-publish","format-standard","hentry","category-server","tag-java-rabbitmq","tag-rabbitmq","tag-spring-boot"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38016.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb732\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u6ce8\u89e3\u9a71\u52a8\u5f00\u53d1\u3001\u6d88\u606f\u8f6c\u6362\u5668\u914d\u7f6e\u3001\u6d88\u8d39\u8005\u76d1\u542c\u5668\u4e0e\u4e8b\u52a1\u7ba1\u7406\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38016.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T06:13:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38016.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38016.html\",\"name\":\"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-19T06:13:34+00:00\",\"dateModified\":\"2025-05-19T06:13:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38016.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38016.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38016.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38016.html","og_locale":"zh_CN","og_type":"article","og_title":"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb732\u6b21\uff0c\u70b9\u8d5e8\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u6ce8\u89e3\u9a71\u52a8\u5f00\u53d1\u3001\u6d88\u606f\u8f6c\u6362\u5668\u914d\u7f6e\u3001\u6d88\u8d39\u8005\u76d1\u542c\u5668\u4e0e\u4e8b\u52a1\u7ba1\u7406\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/38016.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-19T06:13:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38016.html","url":"https:\/\/www.wsisp.com\/helps\/38016.html","name":"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-19T06:13:34+00:00","dateModified":"2025-05-19T06:13:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38016.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38016.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38016.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Spring Boot \u4e0e RabbitMQ \u7684\u6df1\u5ea6\u96c6\u6210\u5b9e\u8df5\uff08\u4e8c\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38016"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38016\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38016"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}