{"id":570,"date":"2024-09-13T23:47:35","date_gmt":"2024-09-13T15:47:35","guid":{"rendered":"https:\/\/forelink.top\/?p=570"},"modified":"2024-09-13T23:47:38","modified_gmt":"2024-09-13T15:47:38","slug":"%e6%98%a5%e9%9d%b4%e5%85%94-topicmode","status":"publish","type":"post","link":"https:\/\/forelink.top\/index.php\/2024\/09\/13\/%e6%98%a5%e9%9d%b4%e5%85%94-topicmode\/","title":{"rendered":"\u6625\u9774+\u5154 TopicMode"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"\u7b80\u4ecb\">\u7b80\u4ecb<\/h1>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913205308.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913205308.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image.png\"\/><\/div><\/figure>\n\n\n\n<p>\u4f7f\u7528SpringBoot + RabbitMQ\n\u5b9e\u73b0\u9700\u6c42\uff1a\n\u901a\u8fc7 \u5728HTML\u9875\u9762\u4e2d\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4 \u53d1\u8d77 HTTP \u8bf7\u6c42\u6765\u8c03\u7528\u751f\u4ea7\u8005\u65b9\u6cd5\u53d1\u9001\u6d88\u606f\n\u683c\u5f0f\u5982 test.orange.test\n\u5e76\u5c06Message\u8f93\u51fa\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"topic-\u7a0b\u5e8f\">Topic \u7a0b\u5e8f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u914d\u7f6e\u7c7b\">\u914d\u7f6e\u7c7b<\/h2>\n\n\n\n<p>\u5728 spring\u6846\u67b6\u7684amcq\u4e2d\uff0c\u63d0\u4f9b\u4e86 \u961f\u5217\uff0c\u4ea4\u6362\u673a\u548c\u7ed1\u5b9a\u5173\u7cfb\u7684\u6784\u5efa\u65b9\u6cd5\uff08Builder\uff09\n\u5728\u4f7f\u7528 SpringBoot \u63d0\u4f9b RabbitMQ \u670d\u52a1\u65f6\uff0c\u6d3b\u7528\u6784\u5efa\u5668\u53ef\u4ee5\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n\n\n\n<p>Spring\u6846\u67b6\u7684amqp.core\u5305\u63d0\u4f9b\u4e86\u5982\u4e0b\u6784\u5efa\u5668\uff1a\nQueueBuilder\nExchangeBuilder\nBindindBuilder\n\u4ee5\u70b9\u5206\u9694\u63a5\u6536\u53c2\u6570\uff0c\u961f\u5217\u4e0e\u4ea4\u6362\u673a\u4ee5 <code>build()<\/code> \u7ed3\u5c3e\n\u7ed1\u5b9a\u5173\u7cfbBinding\u4ee5 <code>noargs()<\/code> \u6216\u8005 <code>and()<\/code> \u7ed3\u5c3e<\/p>\n\n\n\n<p>\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u67e5\u770b\u540e\u7eed\u65b9\u6cd5\u7684\u7c7b\u578b\u3002\n<img decoding=\"async\" alt=\"image.png\" src=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913224424.png\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.amqp.core.*;  \nimport org.springframework.beans.factory.annotation.Qualifier;  \nimport org.springframework.context.annotation.Bean;  \nimport org.springframework.context.annotation.Configuration;  \n  \n\/**  \n * *\/  \n@Configuration  \npublic class TopicConfig {  \n    \/\/ 1.\u4ea4\u6362\u673a \u6807\u8bc6\u4f7f\u7528@Qualify\u6ce8\u89e3\u8c03\u7528\u8fd9\u4e2abean  \n    @Bean(\"topicExchange\")  \n    public Exchange topicExchange(){  \n        \/\/ \u521b\u5efa\u4e00\u4e2a topic\u7c7b\u578b \u7684\u4ea4\u6362\u673a  \n        return ExchangeBuilder.topicExchange(\"topicExchange\").durable(false).build();  \n    }  \n    \/\/ 2.\u961f\u5217  \n    @Bean(\"topicQueue1\")  \n    public Queue topicQueue1(){  \n        return QueueBuilder.durable(\"topicQueue1\").build();  \n    }  \n    @Bean(\"topicQueue2\")  \n    public Queue topicQueue2(){  \n        return QueueBuilder.durable(\"topicQueue2\").build();  \n    }  \n    \/\/ 3.\u7ed1\u5b9aTopic\u5173\u7cfb  \n    @Bean  \n    public Binding Binding1(@Qualifier(\"topicQueue1\") Queue queue,@Qualifier(\"topicExchange\") Exchange exchange){  \n        return BindingBuilder.bind(queue).to(exchange).with(\"*.orange.*\").noargs();  \n    }  \n  \n    @Bean  \n    public Binding binding2(@Qualifier(\"topicQueue2\") Queue queue,@Qualifier(\"topicExchange\") Exchange exchange){  \n        return BindingBuilder.bind(queue).to(exchange).with(\"*.*.rabbit\").noargs();  \n    }  \n  \n    @Bean  \n    public Binding binding3(@Qualifier(\"topicQueue2\") Queue queue,@Qualifier(\"topicExchange\") Exchange exchange){  \n        return BindingBuilder.bind(queue).to(exchange).with(\"lazy.#\").noargs();  \n    }  \n  \n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"html-\u9875\u9762\">HTML \u9875\u9762<\/h2>\n\n\n\n<p>\u51c6\u5907\u4e00\u4e2a HTML \u9875\u9762\uff0c\u5305\u542b<code>routingKey<\/code> <code>Message<\/code> \u7684\u8868\u5355\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;  \n&lt;html lang=\"en\"&gt;  \n&lt;head&gt;  \n    &lt;meta charset=\"UTF-8\"&gt;  \n    &lt;title&gt;Topic Test&lt;\/title&gt;  \n&lt;\/head&gt;  \n&lt;body&gt;  \n    &lt;form action=\"http:\/\/localhost:8080\/topic\" method=\"get\" &gt;  \n        routing key: &lt;input type=\"text\" name=\"routingKey\"&gt;  \n        message \uff1a &lt;input type=\"text\" name=\"message\"&gt;&lt;\/input&gt;  \n        &lt;input type=\"submit\" value=\"\u63d0\u4ea4\"&gt;  \n    &lt;\/form&gt;&lt;\/body&gt;  \n&lt;\/html&gt;\n&lt;!--\n    form \u8868\u5355\n    &nbsp; &nbsp; action\u53d1\u9001\u8868\u5355\u4e2d\u7684\u6570\u636e\u5230action\u7684url\u91cc\uff0c\n    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url\u4e3a\u7a7a\u65f6\u53d1\u9001\u5230\u5f53\u524d\u9875\u9762\n    &nbsp; &nbsp; method\n    &nbsp; &nbsp; &nbsp; &nbsp; get \u6709url\u957f\u5ea6\u9650\u5236\n    &nbsp; &nbsp; &nbsp; &nbsp; post\u53d1\u9001\u5230f12\u4e2d\u7f51\u7edc\u8bf7\u6c42\u4e2d\uff0c\u65e0\u7f51\u7edc\u9650\u5236\n    input\n    &nbsp; &nbsp; type\n    &nbsp; &nbsp; &nbsp; &nbsp; text \u6587\u672c\u7c7b\u578b\uff0c\u53ef\u64cd\u4f5c\n    &nbsp; &nbsp; &nbsp; &nbsp; submit \u6309\u94ae\u7c7b\u578b\uff0c\u53ef\u70b9\u51fb\n    &nbsp; &nbsp; name\n    &nbsp; &nbsp; &nbsp; &nbsp; \u662f\u5fc5\u987b\u7684\u5c5e\u6027\n    &nbsp; &nbsp; value\n    &nbsp; &nbsp; &nbsp; &nbsp; \u6846\u6846\u4e2d\u7684\u503c\n--&gt;\n<\/code><\/pre>\n\n\n\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u7684\u6548\u679c\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913224816.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913224816.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image.png\"\/><\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u751f\u4ea7\u8005\">\u751f\u4ea7\u8005<\/h2>\n\n\n\n<p>\u65b0\u5efa\u751f\u4ea7\u8005\u7c7b <code>ropicProducer<\/code>\n\u5148\u521b\u5efa\u4e00\u4e2aSpring\u6846\u67b6\u4e2d\u7684 <code>RabbitTemplate<\/code> \u5b9e\u73b0\u7c7b\n\u8be5\u7c7b\u5df2\u88c5\u8f7d\u5728 Bean \u5bb9\u5668\u4e2d\uff0c\u8bbe\u7f6eAutoWired\u81ea\u52a8\u83b7\u53d6\u8be5\u5bf9\u8c61<\/p>\n\n\n\n<p>\u7136\u540e\u63d0\u4f9b\u4e00\u4e2a <code>sendMessage<\/code> \u65b9\u6cd5\uff0c\u5305\u542b <code>routingKey<\/code> \u548c <code>message<\/code> \u5f62\u53c2\uff0c\u65b9\u4fbf\u5916\u90e8\u4f20\u53c2\u8c03\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.amqp.rabbit.core.RabbitTemplate;  \nimport org.springframework.beans.factory.annotation.Autowired;  \nimport org.springframework.stereotype.Component;  \n  \n@Component  \npublic class TopicProducer {  \n    \/\/ \u521b\u5efa\u4e00\u4e2arabbitTemplate\u5bf9\u8c61  \n    private RabbitTemplate rabbitTemplate;  \n    private String exchangeName = \"topicExchange\";  \n  \n    @Autowired  \n    public TopicProducer(RabbitTemplate rabbitTemplate) {  \n        this.rabbitTemplate = rabbitTemplate;  \n    }  \n    \/\/ sendMessage \u65b9\u6cd5  \n    public void sendMessage(String routingKey,String message) {  \n        rabbitTemplate.convertAndSend(exchangeName, routingKey, message);  \n    }  \n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u63a7\u5236\u5668\">\u63a7\u5236\u5668<\/h2>\n\n\n\n<p>\u65b0\u5efa\u63a7\u5236\u5668 <code>TopicController<\/code> \u7528\u4e8e\u54cd\u5e94HTTP\u8bf7\u6c42\uff0c\u5e76\u6267\u884c\u751f\u4ea7\u8005\u53d1\u9001\u6d88\u606f\u7684 <code>sendMessage<\/code> \u65b9\u6cd5\n\u521b\u5efa\u4e00\u4e2a\u751f\u4ea7\u8005\u5bf9\u8c61 <code>topicProducer<\/code> \u7528\u4ee5\u8c03\u7528\u53d1\u9001\u6d88\u606f\u7684\u65b9\u6cd5<\/p>\n\n\n\n<p>\u5e76\u63d0\u4f9b\u4e00\u4e2a <code>GetMapping<\/code> \u65b9\u6cd5, \u5728\u63a5\u6536\u5230HTTP\u9875\u9762\u53d1\u6765\u7684\u8bf7\u6c42\u540e, \u5c06\u8868\u5355\u4e2d\u5305\u542b\u7684 <code>routingKey<\/code> \u548c <code>Message<\/code> \u4f20\u7ed9 <code>sendMessage()<\/code> \u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.web.bind.annotation.GetMapping;  \nimport org.springframework.web.bind.annotation.RestController;  \n  \n@RestController  \npublic class TopicController {  \n  \n    private final TopicProducer topicProducer;  \n  \n    public TopicController(TopicProducer topicProducer) {  \n        this.topicProducer = topicProducer;  \n    }  \n\n    @GetMapping(\"\/topic\")  \n    public String sendMessage(String routingKey,String message){  \n        topicProducer.sendMessage(routingKey,message);  \n        return \"routingKey: \"+routingKey+\"\\nmessage: \"+message;  \n    }  \n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6d4b\u8bd5\u6548\u679c\">\u6d4b\u8bd5\u6548\u679c<\/h2>\n\n\n\n<p>\u5c06\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u8d77\u6765, \u6253\u5f00 html \u9875\u9762, \u901a\u8fc7\u8868\u5355\u63d0\u4ea4\u4e09\u6761\u6d4b\u8bd5\u4fe1\u606f\u548c\u4e00\u6761\u65e0\u5173\u4fe1\u606f.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913231308.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913231308.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image.png\"\/><\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5728\u63a7\u5236\u53f0\u4e2d\u53ef\u4ee5\u5927\u81f4\u786e\u8ba4\u4fe1\u606f\u8fbe\u5230\u4e86\u961f\u5217<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913231407.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913231407.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image.png\"\/><\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u6d88\u8d39\u8005\">\u6d88\u8d39\u8005<\/h2>\n\n\n\n<p>\u65b0\u5efa\u4e24\u4e2a\u6d88\u8d39\u8005\u7c7b, \u5206\u522b\u7528\u4ee5\u5904\u7406\u4e24\u4e2a\u961f\u5217\u7684\u6d88\u606f\n\u5e76\u901a\u8fc7@RabbitListener \u6ce8\u89e3\u76d1\u542c\u961f\u5217, \u5f53\u53d6\u5230\u961f\u5217\u7684\u4fe1\u606f\u65f6, \u4f1a\u5c06\u53c2\u6570\u4f20\u5165\u6ce8\u89e3\u4e0b\u7684\u65b9\u6cd5\u5e76\u81ea\u52a8\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.amqp.rabbit.annotation.RabbitListener;  \nimport org.springframework.stereotype.Component;  \n  \n@Component  \npublic class TopicConsumer1 {  \n    private final String queueName = \"topicQueue1\";  \n    @RabbitListener(queues = queueName)  \n    public void recv(String message) {  \n        System.out.println(\"C1 receive message : \" + message);  \n    }  \n}\n<\/code><\/pre>\n\n\n\n<p>\u6d88\u8d39\u80052\u4e2d\u7684 <code>QueueName<\/code> \u6539\u4e3a <code>topicQueue2<\/code>\n\u63a7\u5236\u53f0\u8f93\u51fa\u4fe1\u606f\u4e2d\u7684 C1 \u6539\u4e3a C2 \u52a0\u4ee5\u533a\u5206<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8fd0\u884c\">\u8fd0\u884c<\/h2>\n\n\n\n<p>\u8fd0\u884c\u4ee3\u7801,\u62ff\u5230\u5e76\u5904\u7406\u4e86\u961f\u5217\u4e2d\u79ef\u538b\u7684\u4fe1\u606f\n<img decoding=\"async\" alt=\"image.png\" src=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913232410.png\"><\/p>\n\n\n\n<p>\u6253\u5f00 html \u9875\u9762\u518d\u6d4b\u8bd5\u51e0\u7ec4\u6570\u636e,\u8fd4\u56de\u4e86\u6b63\u786e\u7684\u7ed3\u679c\n<img decoding=\"async\" alt=\"image.png\" src=\"https:\/\/forelink-picgo.oss-cn-beijing.aliyuncs.com\/img\/20240913232446.png\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u4f7f\u7528SpringBoot + RabbitMQ \u5b9e\u73b0\u9700\u6c42\uff1a \u901a\u8fc7 \u5728HTML\u9875\u9762\u4e2d\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4 \u53d1\u8d77  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[48,28,17],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-rabbitmq","category-spring","category-17"],"_links":{"self":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":1,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions\/571"}],"wp:attachment":[{"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/forelink.top\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}