RabbitMQ的五种模式

一、简单模式

简单模式(Simple):一个生产者,一个消费者

java 复制代码
package com.qiangesoft.rabbitmq.mode.simple;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 简单模式
 * ps:一个生产者,一个消费者
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/simple")
@RestController
public class SimpleMode {

    private final String QUEUE = "simple.queue";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void send(String message) {
        rabbitTemplate.convertAndSend(QUEUE, message);
    }

    /**
     * 消费者
     */
    @RabbitListener(queuesToDeclare = @Queue(name = QUEUE))
    public void receiveMessage(String message) {
        log.info("Received Message: " + message);
    }

}

二、工作模式

工作队列模式(Work Queue): 多个消费者竞争消息

java 复制代码
package com.qiangesoft.rabbitmq.mode.work;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工作模式
 * ps:多个消费者竞争消息
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/work")
@RestController
public class WorkMode {

    private final String QUEUE = "work.queue";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void send(String message) {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(QUEUE, message + i);
        }
    }

    /**
     * 消费者1
     */
    @RabbitListener(queuesToDeclare = @Queue(name = QUEUE))
    public void receiveMessage1(String message) {
        log.info("Received Message by consumer1: " + message);
    }

    /**
     * 消费者2
     */
    @RabbitListener(queuesToDeclare = @Queue(name = QUEUE))
    public void receiveMessage2(String message) {
        log.info("Received Message by consumer2: " + message);
    }

}

三、发布订阅模式

发布/订阅模式(Publish/Subscribe):一个生产者,多个消费者

java 复制代码
package com.qiangesoft.rabbitmq.mode.pubsub;

import lombok.extern.slf4j.Slf4j;
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.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发布/订阅模式
 * ps:一个生产者,多个消费者
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/pubsub")
@RestController
public class PubSubMode {

    private final String EXCHANGE = "fanout.exchange";

    private final String KEY = "";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void sendM(String message) {
        rabbitTemplate.convertAndSend(EXCHANGE, KEY, message);
    }

    /**
     * 消费者1
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.FANOUT)))
    public void receiveMessage1(String message) {
        log.info("Received Message by consumer1: " + message);
    }

    /**
     * 消费者2
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.FANOUT)))
    public void receiveMessage2(String message) {
        log.info("Received Message by consumer2: " + message);
    }

}

四、路由模式

路由模式(Routing):根据路由键将消息转发到对应队列

java 复制代码
package com.qiangesoft.rabbitmq.mode.routing;

import lombok.extern.slf4j.Slf4j;
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.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 路由模式
 * ps:根据路由键将消息转发到对应队列
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/routing")
@RestController
public class RoutingMode {

    private final String EXCHANGE = "direct.exchange";

    private final String KEY1 = "direct1";

    private final String KEY2 = "direct2";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void sendM(String message) {
        rabbitTemplate.convertAndSend(EXCHANGE, KEY1, message);
    }

    /**
     * 消费者
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.DIRECT), key = {KEY1, KEY2}))
    public void receiveMessage(String message) {
        log.info("Received Message: " + message);
    }

}

五、主题模式

通配符模式(Topics):使用通配符匹配路由键

java 复制代码
package com.qiangesoft.rabbitmq.mode.topic;

import lombok.extern.slf4j.Slf4j;
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.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 主题模式
 * ps: 使用通配符匹配路由键
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/topic")
@RestController
public class TopicMode {

    private final String EXCHANGE = "topic.exchange";

    private final String QUEUE = "topic.queue";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void send(String message) {
        rabbitTemplate.convertAndSend(EXCHANGE, QUEUE, message);
    }

    /**
     * 消费者
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.TOPIC), key = {"topic.*", "#.topic"}))
    public void receiveMessage(String message) {
        log.info("Received Message: " + message);
    }

}
相关推荐
小五Z1 小时前
Redis--事务
redis·分布式·后端·缓存
明月看潮生4 小时前
青少年编程与数学 02-016 Python数据结构与算法 23课题、分布式算法
分布式·python·算法·青少年编程·编程与数学
[email protected]5 小时前
ASP.NET Core 性能优化:分布式缓存
分布式·缓存·性能优化·asp.net·.netcore
冰 河10 小时前
《Mycat核心技术》第22章:搭建Mycat+Zookeeper+HAProxy+Keepalived+MySQL高可用架构
分布式·微服务·程序员·架构师·mycat
一只叫煤球的猫10 小时前
分布式-跨服务事务一致性的常见解决方案
java·分布式·后端
java1234_小锋11 小时前
Zookeeper的通知机制是什么?
linux·分布式·zookeeper
bjzhang7511 小时前
rqlite:一个基于SQLite构建的分布式数据库
数据库·分布式·rqlite
掘金-我是哪吒21 小时前
分布式微服务系统架构第105集:协议,高性能下单系统示例项目
分布式·微服务·架构·系统架构·linq
风铃儿~1 天前
Java微服务注册中心深度解析:环境隔离、分级模型与Eureka/Nacos对比
java·分布式·微服务·面试