springboot整合rabbitmq的五种模式示例以及详解

1.基本消息模型:生产者–>队列–>一个消费者
2.work消息模型:生产者–>队列–>多个消费者共同消费
3.订阅模型-Fanout:广播,将消息交给所有绑定到交换机的队列,每个消费者都可以收到同一条消息
4.订阅模型-Direct:定向,把消息交给符合指定 rotingKey 的队列(路由模式)
5.订阅模型-Topic:通配符,把消息交给符合routing pattern(主题模式) 的队列
(3、4、5这三种都属于订阅模型,只不过进行路由的方式不同。)

添加依赖 mq 

   <!--mq-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
      <version>2.5.4</version>
    </dependency>

简单队列(模式)

一个生产者对应一个消费者
生产者

/*简单队列(模式)*/
 @Test
    public void contextLoads(){
        String msg = "这是一个简单队列模式";

        amqpTemplate.convertAndSend("spring.simple.queue", msg );

    }

消费者

@Component
public class SimpleListener {

    // 通过注解自动创建 spring.simple.queue 队列
    @RabbitListener(queuesToDeclare = @Queue("spring.simple.queue"))
    public void listen(String msg) {
        System.out.println("简单队列 接收到消息:" + msg);
    }
}

Work模式

一个生产者对应多个消费者
生产者

/*work 模式*/
    @Test
    public void work() throws InterruptedException {
        String msg = "这是一个work模式";
        for (int i = 0; i < 10; i++) {
            amqpTemplate.convertAndSend("spring.work.queue", msg + i);
        }
        Thread.sleep(5000);
    }

消费者

@Component
public class WorkListener {

    // 通过注解自动创建 spring.work.queue 队列
    @RabbitListener(queuesToDeclare = @Queue("spring.work.queue"))
    public void listen(String msg) {
        System.out.println("work模式 接收到消息:" + msg);
    }

    // 创建两个队列共同消费
    @RabbitListener(queuesToDeclare = @Queue("spring.work.queue"))
    public void listen2(String msg) {
        System.out.println("work模式二 接收到消息:" + msg);
    }
}

消费者

@Component
public class FanoutListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.fanout.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.fanout.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            )
    ))
    public void listen(String msg) {
        System.out.println("订阅模式1 接收到消息:" + msg);
    }

    // 队列2(第二个人),同样能接收到消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.fanout2.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.fanout.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            )
    ))
    public void listen2(String msg) {
        System.out.println("订阅模式2 接收到消息:" + msg);
    }
}

订阅模型-Direct (路由模式)
在Fanout模式中,一条消息,会被所有订阅的队列都消费。但是,在某些场景下,我们希望不同的消息被不同的队列消费。这时就要用到Direct类型的Exchange。给特定的消费者消费
在Direct模型下:

1.队列与交换机的绑定,不能是任意绑定了,而是要指定一个RoutingKey(路由key)
2.消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey。
3.Exchange不再把消息交给每一个绑定的队列,而是根据消息的Routing Key进行判断,只有队列的 Routingkey与消息的 Routing key完全一致,才会接收到消息
生产者

/*订阅模型-Direct (路由模式)*/
    @Test
    public void direct() throws InterruptedException {
        String msg = "路由模式";
        for (int i = 0; i < 10; i++) {
            amqpTemplate.convertAndSend("spring.direct.exchange", "direct", msg + i);
        }
        Thread.sleep(5000);
    }

消费者

@Component
public class DirectListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.direct.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.direct.exchange",
                    ignoreDeclarationExceptions = "true"
            ),
            key = {"direct"}
    ))
    public void listen(String msg) {
        System.out.println("路由模式1 接收到消息:" + msg);
    }

    // 队列2(第二个人),key值不同,接收不到消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.direct2.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.direct.exchange",
                    ignoreDeclarationExceptions = "true"
            ),
            key = {"direct-test"}
    ))
    public void listen2(String msg) {
        System.out.println("路由模式2 接收到消息:" + msg);
    }
}

订阅模型-Topic (主题模式)
Topic类型的Exchange与Direct相比,都是可以根据RoutingKey把消息路由到不同的队列。只不过Topic类型Exchange可以让队列在绑定Routing key 的时候使用通配符!

Routingkey 一般都是有一个或多个单词组成,多个单词之间以”.”分割,例如: user.insert

通配符规则    举例
#:匹配一个或多个词    person.#:能够匹配person.insert.save 或者 person.insert
*:匹配不多不少恰好1个词    person.*:只能匹配person.insert

生产者

/* 订阅模型-Topic (主题模式)*/
    @Test
    public void topic() throws InterruptedException {
        amqpTemplate.convertAndSend("spring.topic.exchange", "person.insert", "增加人员");
        amqpTemplate.convertAndSend("spring.topic.exchange", "person.delete", "删除人员");
        amqpTemplate.convertAndSend("spring.topic.exchange", "money.insert", "加钱");
        amqpTemplate.convertAndSend("spring.topic.exchange", "money.delete", "减钱");
        Thread.sleep(5000);
    }

消费者

@Component
public class TopicListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.topic.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.topic.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"person.*"}
    ))
    public void listen(String msg) {
        System.out.println("person 接收到消息:" + msg);
    }

    // 通配规则不同,接收不到消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "spring.topic.queue", durable = "true"),
            exchange = @Exchange(
                    value = "spring.topic.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"money.*"}
    ))
    public void listen2(String msg) {
        System.out.println("money Student 接收到消息:" + msg);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章