rabbitMQ基础篇学习

[复制链接]
发表于 2024-11-15 12:45:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
rabbitMQ基础篇学习

代码下载链接

http://121.37.250.237:8080/files/download?filename=mq-demo.zip
假如下载不了,进入http://121.37.250.237:8080/login,用户名root,密码123456
登录后再访问上面链接下载
1.rabbitMQ基础

rabbitMQ分为同步通信和异步通信

同步通信:好比打微信电话,对方的消息这边实时接受,只有两边都在线的情况才能通信乐成

异步通信:微信发送消息,对方不在线,但是消息一直存储在rabbitMQ里,对方上线后,rabbitMQ发送消息,这种就不会存在因为对方不在线出现消息丢失的情况

2.rabbitMQ安装

我们一样平常都在docker里安装mq,详情见docker的rabbitMQ安装

3.rabbitMQ模式

3.1简朴模式

登录rabbitMQ网页创建一个队列,创建消耗者和生产者,生产者通过此队列给消耗者发消息。
消耗者代码:
  1.     @RabbitListener(queues = "simple.queue")
  2.     public void listenSimpleQueue(String msg){
  3.         System.out.println("消费者收到了simple.queue的消息:【" + msg +"】");
  4.     }
复制代码
生产者代码:
  1.     @Test
  2.     public void testSendMessage2Queue() {
  3.         String queueName = "simple.queue";
  4.         String msg = "hello, amqp!";
  5.         try {
  6.             rabbitTemplate.convertAndSend(queueName, msg);
  7.             System.out.println("Message sent successfully");
  8.         } catch (Exception e) {
  9.             e.printStackTrace();
  10.             System.out.println("Error occurred while sending message");
  11.         }
  12.     }
复制代码
该模式下,生产者通过rabbitTemplate.convertAndSend函数,向simple.queue队列发送消息,消耗者通过@RabbitListener绑定simple.queue队列,接受消息
3.2 工作模式

一个生产者,多个消耗者,一个队列,生产者发送消息,所有消耗者都吸收到
消耗者代码:
  1.     @RabbitListener(queues = "work.queue")
  2.     public void listenWorkQueue1(String msg) throws InterruptedException {
  3.         System.out.println("消费者1 收到了 work.queue的消息:【" + msg +"】");
  4.         Thread.sleep(20);
  5.     }
  6.     @RabbitListener(queues = "work.queue")
  7.     public void listenWorkQueue2(String msg) throws InterruptedException {
  8.         System.err.println("消费者2 收到了 work.queue的消息...... :【" + msg +"】");
  9.         Thread.sleep(200);
  10.     }
复制代码
生产者代码:
  1.     @Test
  2.     void testWorkQueue() throws InterruptedException {
  3.         String queueName = "work.queue";
  4.         for (int i = 1; i <= 50; i++) {
  5.             String msg = "hello, worker, message_" + i;
  6.             rabbitTemplate.convertAndSend(queueName, msg);
  7.             Thread.sleep(20);
  8.         }
  9.     }
复制代码
该模式下,生产者发送的队列,所有消耗者都能吸收到消息。
3.发布/订阅模式(fanout)

一个生产者、一个 fanout 范例的交换机、多个队列、多个消耗者。
在网页上生成队列queue1和queue2,再生成一个fanout范例的交换机amq.fanout,用amq.fanout绑定队列queue1和queue2,此时生产者通过交换机amq.fanout发送消息,只有与amq.fanout交换机绑定的队列会吸收到消息(注意消耗者监听的是队列,生产者发送的是交换机)
消耗者代码:
  1.     @RabbitListener(queues = "fanout.queue1")
  2.     public void listenFanoutQueue1(String msg) throws InterruptedException {
  3.         System.out.println("消费者1 收到了 fanout.queue1的消息:【" + msg +"】");
  4.     }
  5.     @RabbitListener(queues = "fanout.queue2")
  6.     public void listenFanoutQueue2(String msg) throws InterruptedException {
  7.         System.out.println("消费者2 收到了 fanout.queue2的消息:【" + msg +"】");
  8.     }
复制代码
生产者代码:
  1.     @Test
  2.     void testSendFanout() {
  3.         String exchangeName = "amq.fanout";
  4.         String msg = "hello, everyone!";
  5.         rabbitTemplate.convertAndSend(exchangeName, null, msg);
  6.     }
复制代码
4.路由模式(direct)

一个生产者,一个 direct 范例的交换机,多个队列。
在网页上生成队列queue1和queue2,再生成一个direct范例的交换机hmall.direct,用hmall.direct绑定队列queue1和queue2,和fanout差别的是,direct交换机可以指定一个routing-key,好比与queue1绑定的routing-key=“red”, “blue”,与queue2绑定的routing-key=“red”, “yellow”。
此时生产者通过交换机hmall.direct发送消息并带上一个routing-key,只有与hmall.direct交换机绑定的队列并且routing-key相称的才会吸收到消息
消耗者代码:
  1.     @RabbitListener(bindings = @QueueBinding(
  2.             value = @Queue(name = "direct.queue1", durable = "true"),
  3.             exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
  4.             key = {"red", "blue"}
  5.     ))
  6.     public void listenDirectQueue1(String msg) throws InterruptedException {
  7.         System.out.println("消费者1 收到了 direct.queue1的消息:【" + msg +"】");
  8.     }
  9.     @RabbitListener(bindings = @QueueBinding(
  10.             value = @Queue(name = "direct.queue2", durable = "true"),
  11.             exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
  12.             key = {"red", "yellow"}
  13.     ))
  14.     public void listenDirectQueue2(String msg) throws InterruptedException {
  15.         System.out.println("消费者2 收到了 direct.queue2的消息:【" + msg +"】");
  16.     }
复制代码
生产者代码:
  1.     @Test
  2.     void testSendDirect() {
  3.         String exchangeName = "hmall.direct";
  4.         String msg = "蓝色通知,警报解除,哥斯拉是放的气球";
  5.         rabbitTemplate.convertAndSend(exchangeName, "blue", msg);
  6.     }
复制代码
此时只有绑定的key=blue的才能收到消息,也就是消耗者1,假如将生产者代码改为red,消耗者1和消耗者2都能收到消息。
5.主题模式(topic)

一个生产者,一个 topic 范例的交换机,多个队列,交换机与队列之间通过 routing-key 举行关联绑定,多个消耗者。生产者发送消息到交换机并且要指定 routing-key,然后消息根据这交换机与队列之间的 routing-key 绑定规则举行路由被指定消耗者消耗。与路由模式差别是 routing-key 有指定的队则,可以更加的通用,满意更过的场景。routing-key 的规则如下:
#:匹配一个大概多个词,例如lazy.# 可以匹配 lazy.xxx 大概 lazy.xxx.xxx
:只能匹配一个词,例如lazy. 只能匹配 lazy.xxx
消耗者代码:
  1.     @RabbitListener(queues = "topic.queue1")
  2.     public void listenTopicQueue1(String msg) throws InterruptedException {
  3.         System.out.println("消费者1 收到了 topic.queue1的消息:【" + msg +"】");
  4.     }
  5.     @RabbitListener(queues = "topic.queue2")
  6.     public void listenTopicQueue2(String msg) throws InterruptedException {
  7.         System.out.println("消费者2 收到了 topic.queue2的消息:【" + msg +"】");
  8.     }
复制代码
生产者代码:
  1.      @Test
  2.     void testSendTopic() {
  3.         String exchangeName = "hmall.topic";
  4.         String msg = "今天天气挺不错,我的心情的挺好的";
  5.         rabbitTemplate.convertAndSend(exchangeName, "china.weather", msg);
  6.     }
  7.     @Test
  8.     void testSendTopic1() {
  9.         String exchangeName = "hmall.topic";
  10.         String msg = "今天天气挺不错,我的心情的挺好的";
  11.         rabbitTemplate.convertAndSend(exchangeName, "japan.news", msg);
  12.     }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表