ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:678.76KB ,
资源ID:5822918      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5822918.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(RabbitMQ实战.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

RabbitMQ实战.docx

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