1、RabbitMQ实战Springboot整合RabbitMQ依赖 org.springframework.boot spring-boot-starter-amqp 配置spring:application:name:rabbitmqrabbitmq:host: 127.0.0.1port: 5672username: guestpassword: guestpublisher-returns:truepublisher-confirms:trueQueue配置packagecom.neo.config;importorg.springframework.amqp.core.Queue;imp
2、ortorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassQueueConfig Bean public Queue createQueue() returnnew Queue(hello-queue); Bean public Queue create2Queue() returnnew Queue(hello2-queue); Sender:packagecom.neo.sender;imp
3、ortorg.springframework.amqp.core.AmqpTemplate;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;Componentpublicclass Sender Autowired AmqpTemplateamqp; publicvoid send(String message) amqp.convertAndSend(hello-queue, message); public String s
4、endAndReceive(String message) return (String) amqp.convertSendAndReceive(hello2-queue, message); Receiver:packagecom.neo.receiver;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;Componentpublicclass Receiver RabbitListener(queues=hello-q
5、ueue) publicvoid receive(String message) System.out.println(接收消息:+message); RabbitListener(queues=hello2-queue) public String receiveAndSend(String message) System.out.println(接收消息:+message); returnmessage+receiveBack; Direct交换器:(直接匹配)(一对一)Direct Exchange 处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个
6、完整的匹配。如果一个队列绑定到该交换机上要求路由键 “test”,则只有被标记为“test”的消息才被转发,不会转发test.aaa,也不会转发dog.123,只会转发test。创建队列和交换机package com.neo.config;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Exchange;import org.springframework.amqp.core.Queue;import org.springframework.context.anno
7、tation.Bean;import org.springframework.context.annotation.Configuration;Configurationpublic class DirectQueueConfig Bean public Queue createInfoQueue() return new Queue(info.queue); Bean public Queue createErrorQueue() return new Queue(error.queue); Bean public Exchange createLogExchange() return ne
8、w DirectExchange(log.direct); 编写receiverInfoReceiver:packagecom.neo.receiver;importorg.springframework.amqp.core.ExchangeTypes;importorg.springframework.amqp.rabbit.annotation.Exchange;importorg.springframework.amqp.rabbit.annotation.Queue;importorg.springframework.amqp.rabbit.annotation.QueueBindin
9、g;importorg.springframework.amqp.rabbit.annotation.RabbitHandler;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;Component/* *RabbitListener bindings:绑定队列 *QueueBinding exchange:交换器 *QueueBinding key:指定路由 *QueueBinding value:队列名 *Exchang
10、e type:指定具体的交换器类型 *QueueautoDelete:是否是一个可删除的临时队列,springboot2.0报错 */RabbitListener( bindings=QueueBinding( exchange = Exchange(name=log.direct,type=ExchangeTypes.DIRECT), value = Queue(name=info.queue), key=info.routing.key ) )publicclassInfoReceiver RabbitHandler publicvoid receive(String message) S
11、ystem.out.println(接收info消息:+message); ErrorReceiver:package com.neo.receiver;import org.springframework.amqp.core.ExchangeTypes;import org.springframework.amqp.rabbit.annotation.Exchange;import org.springframework.amqp.rabbit.annotation.Queue;import org.springframework.amqp.rabbit.annotation.QueueBi
12、nding;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;ComponentRabbitListener( bindings=QueueBinding( exchange = Exchange(name=log.direct,type=ExchangeTypes.DIRECT), value
13、 = Queue(name=error.queue), key=error.routing.key ) )public class ErrorReceiver RabbitHandler public void receive(String message) System.out.println(接收eror消息:+message); Sender:packagecom.neo.sender;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.
14、Autowired;import org.springframework.stereotype.Component;Componentpublic class Sender Autowired AmqpTemplateamqp; public void sendInfo(String message) amqp.convertAndSend(log.direct, info.routing.key, message); public void sendError(String message) amqp.convertAndSend(log.direct, error.routing.key,
15、 message); Topic交换器:(规则匹配)(多对多)Topic Exchange 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*” 只会匹配到“audit.irs”。创建队列和路由packagecom.neo.config;importorg.springframework.amqp.core.Exchange;importorg.springframework.amqp.core.Queue;importorg.spri
16、ngframework.amqp.core.TopicExchange;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassTopicQueueConfig Bean public Queue createInfoQueue() returnnew Queue(log.info); Bean public Queue createErrorQueue() returnnew Queu
17、e(log.error); Bean public Queue createDebugQueue() returnnew Queue(log.debug); Bean public Queue createAllQueue() returnnew Queue(log.all); Bean public Exchange createLogExchange() returnnewTopicExchange(log.topic); 创建senderUserSenderpackage com.neo.sender;import org.springframework.amqp.core.AmqpTe
18、mplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;Componentpublic class UserSender Autowired AmqpTemplateamqp; public void sendInfo(String message) amqp.convertAndSend(log.topic, user.log.info, user.log.info+message); amqp.convertAnd
19、Send(log.topic, user.log.error, user.log.error+message); amqp.convertAndSend(log.topic, user.log.debug, user.log.debug+message); amqp.convertAndSend(log.topic, user.log.warn, user.log.warn+message); ProductSenderpackage com.neo.sender;import org.springframework.amqp.core.AmqpTemplate;import org.spri
20、ngframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;Componentpublic class ProductSender Autowired AmqpTemplateamqp; public void sendInfo(String message) amqp.convertAndSend(log.topic, product.log.info, product.log.info+message); amqp.convertAndSend(log.topi
21、c, product.log.error, product.log.error+message); amqp.convertAndSend(log.topic, product.log.debug, product.log.debug+message); amqp.convertAndSend(log.topic, product.log.warn, product.log.warn+message); OrderSender:package com.neo.sender;import org.springframework.amqp.core.AmqpTemplate;import org.
22、springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;Componentpublic class OrderSender Autowired AmqpTemplateamqp; public void sendInfo(String message) amqp.convertAndSend(log.topic, order.log.info, order.log.info+message); amqp.convertAndSend(log.topic,
23、 order.log.error, order.log.error+message); amqp.convertAndSend(log.topic, order.log.debug, order.log.debug+message); amqp.convertAndSend(log.topic, order.log.warn, order.log.warn+message); 创建ReceiverLogsReceiverpackage com.neo.receiver;import org.springframework.amqp.core.ExchangeTypes;import org.s
24、pringframework.amqp.rabbit.annotation.Exchange;import org.springframework.amqp.rabbit.annotation.Queue;import org.springframework.amqp.rabbit.annotation.QueueBinding;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;impor
25、t org.springframework.stereotype.Component;ComponentRabbitListener( bindings=QueueBinding( exchange = Exchange(name=log.topic,type=ExchangeTypes.TOPIC), value = Queue(name=log.all), key=*.log.* ) )public class LogsReceiver RabbitHandler public void receive(String message) System.out.println(接收log.al
26、l消息:+message); InfoReceiverpackage com.neo.receiver;import org.springframework.amqp.core.ExchangeTypes;import org.springframework.amqp.rabbit.annotation.Exchange;import org.springframework.amqp.rabbit.annotation.Queue;import org.springframework.amqp.rabbit.annotation.QueueBinding;import org.springfr
27、amework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;ComponentRabbitListener( bindings=QueueBinding( exchange = Exchange(name=log.topic,type=ExchangeTypes.TOPIC), value = Queue(name=log.info), ke
28、y=*.log.info ) )public class InfoReceiver RabbitHandler public void receive(String message) System.out.println(接收log.info消息:+message); ErrorReceiverpackage com.neo.receiver;import org.springframework.amqp.core.ExchangeTypes;import org.springframework.amqp.rabbit.annotation.Exchange;import org.spring
29、framework.amqp.rabbit.annotation.Queue;import org.springframework.amqp.rabbit.annotation.QueueBinding;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;ComponentRabbitListener( bindings=QueueBinding( exchange = Exchange(name=log.topic,type=ExchangeTypes.TOPIC), value = Queue(name=log.error), key=*.log.error
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1