RabbitMQ

一、RabbitMQ的架构图

image

二、RabbitMQ的五种模式
前置准备:
依赖

        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

yaml

logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.138.100
    port: 5672
    virtual-host: /hmall #虚拟主机
    username: rabbitmq账号用户名
    password: rabbitmq账号密码

1. 简单模型

image
步骤:

  1. 创建队列
    image
  2. 生产者

image

    @Test
    public void simpleTest() {
        String queueName = "simple.queue";
        String message = "Hello, Rabbit!";
        rabbitTemplate.convertAndSend(queueName, message);
    }
  1. 消费者
    image
@Component
@Slf4j
public class SpringRabbitListener {
   @RabbitListener(queues = "simple.queue")
    public void handleMessage(String message) {
       log.warn("spring rabbitmq 接收到simple.queue消息:{}", message);
   }
}

2. WorkQueues模型

image
步骤:

  1. 创建队列
    image
  2. 生产者
    @Test
    void workQueueTest() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work.queue", "are you ok" + i);
        }
    }
  1. 消费者
@Component
@Slf4j
public class SpringRabbitListener {
    @RabbitListener(queues = "work.queue")
    public void handleWorkMessage1(String message) throws InterruptedException {
        log.warn("消费者1 接收到work.queue消息:{}", message);
        Thread.sleep(10);
    }

    @RabbitListener(queues = "work.queue")
    public void handleWorkMessage2(String message) {
        log.warn("消费者2 接收到work.queue消息:{}", message);
    }
}
  1. 运行结果
    消费者1和消费者2存在争抢,但都抢到了五个,无论执行速度的快慢,都是对半分。
    但我们希望执行速度快的消费更多,怎么办?,在消费者yml加个prefetch:1 配置即可。
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.138.100
    port: 5672
    virtual-host: /hmall
    username: rabbitmq账号用户名
    password: rabbitmq账号密码
    listener:
      simple:
        prefetch: 1 #消费完消息后再拉取

3. Fanout交换机

image

4. Direct交换机

image
队列:
image
交换机:
image

生产者:

    @Test
    void DirectTest1() {
        rabbitTemplate.convertAndSend("hmall.direct", "red", "red message");
        rabbitTemplate.convertAndSend("hmall.direct", "blue", "blue message");
        rabbitTemplate.convertAndSend("hmall.direct", "yellow", "yellow message");
    }

消费者:

    //=====================direct===========
    @RabbitListener(queues = "direct.queue1")
    public void handleDirectQueueMessage1(String message){
        log.warn("消费者1 接收到direct.queue2消息:{}",message);
    }
    @RabbitListener(queues = "direct.queue2")
    public void handleDirectQueueMessage2(String message){
        log.warn("消费者2 接收到direct.queue2消息:{}",message);
    }

5. Topic交换机

image
通配符规则:
“#”:匹配一个或多个词
“*”:匹配不多不少恰好1个词
创键步骤省略。
image

  1. 生产者,发送消息
/**
 * topicExchange
 */
@Test
public void testSendTopicExchange() {
    // 交换机名称
    String exchangeName = "hmall.topic";
    // 消息
    String message = "喜报!孙悟空大战哥斯拉,胜!";
    // 发送消息
    rabbitTemplate.convertAndSend(exchangeName, "china.news", message);
}

2.消费者,接收消息

@RabbitListener(queues = "topic.queue1")
public void listenTopicQueue1(String msg){
    System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
}

@RabbitListener(queues = "topic.queue2")
public void listenTopicQueue2(String msg){
    System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
}

本文作者:若为自由故

本文链接:https://www.cnblogs.com/yiwangshi/p/18466067

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   是橙子呐  阅读(13)  评论(0编辑  收藏  举报
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.
点击右上角即可分享
微信分享提示