SpringBoot整合RabbitMQ 实现五种消息模型

简介: SpringBoot整合RabbitMQ 实现五种消息模型

SpringBoot中使用RabbitMQ


搭建初始环境


引入依赖

<!-- 引入 rabbitmq 集成依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>


配置配置文件

server:
  port: 9090
spring:
  application:
    # 微服务系统有意义, 养成好习惯, 先写出来
    name: rabbitmq-02-springboot
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: codingce
    password: 123456
    virtual-host: /codingce


测试类 注入 rabbitTemplate

// 注入 rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;


消息队列RabbitMQ之五种消息模型

第一种直连模型使用

开发生产者

/**
     * 直连
     */
    @Test
    void contextLoads() {
        rabbitTemplate.convertAndSend("hello", "hello word");
    }


开发消费者

/**
 * @author mxz
 */
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {
    /**
     * @param message
     */
    @RabbitHandler
    public void receivel(String message) {
        System.out.println("message: " + message);
    }
}


第二种work模型使用

开发生产者

/**
     * work
     */
    @Test
    void testWork() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work", "work模型");
        }
    }


开发消费者

/**
 * 第二种模型 work 模型
 *
 * @author mxz
 */
@Component
public class WorkCustomer {
    /**
     * 第1个消费者
     *
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }
    /**
     * 第2个消费者
     *
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel2(String message) {
        System.out.println("message2 = " + message);
    }
}


第三种 Fanout 广播模型

开发生产者

/**
     * fanout 广播
     */
    @Test
    void testFanout() {
        rabbitTemplate.convertAndSend("logs", "", "Fanout模型发送的消息");
    }


开发消费者

/**
 * fanout
 *
 * @author mxz
 */
@Component
public class FanoutCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  // 创建临时队列
                    exchange = @Exchange(value = "logs", type = "fanout")     // 绑定的交换机
            )
    })
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,  // 创建临时队列
                    exchange = @Exchange(value = "logs", type = "fanout")     // 绑定的交换机
            )
    })
    public void receive2(String message) {
        System.out.println("message2 = " + message);
    }
}


第四种 Route 路由模型

开发生产者

/**
     * route 路由
     */
    @Test
    void testRoute() {
        // exchange 交换机名称
        rabbitTemplate.convertAndSend("directs", "info", "info的key的路由消息");
    }


开发消费者

/**
 * @author mxz
 */
@Component
public class RouteCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 绑定临时队列
                    exchange = @Exchange(value = "directs", type = "direct"), // 自定义交换机名称和类型
                    key = {"info", "error", "warn"}
            )
    })
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 绑定临时队列
                    exchange = @Exchange(value = "directs", type = "direct"), // 自定义交换机名称和类型
                    key = {"error"}
            )
    })
    public void receivel2(String message) {
        System.out.println("message1 = " + message);
    }
}


第五种 Topic 订阅模型(动态路由模型)

开发生产者

/**
     * topic 订阅模式 动态路由
     */
    @Test
    void testTopic() {
        rabbitTemplate.convertAndSend("topics", "user.save", "user.save 路由消息");
    }


开发消费者

/**
 * 订阅模型
 *
 * @author mxz
 */
@Component
public class TopicCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(type = "topic", name = "topics"),
                    key = {"user.save", "user.*"}
            )
    }
    )
    public void receivel(String message) {
        System.out.println("message1" + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(type = "topic", name = "topics"),
                    key = {"produce.#", "order.#"}
            )
    }
    )
    public void receivel2(String message) {
        System.out.println("message2" + message);
    }
}

文章已上传gitee https://gitee.com/codingce/hexo-blog

项目地址github: https://github.com/xzMhehe/codingce-java


相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
目录
相关文章
|
2月前
|
消息中间件 Java Maven
一文搞懂Spring Boot整合RocketMQ
一文搞懂Spring Boot整合RocketMQ
97 0
|
1月前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
253 1
|
1月前
|
人工智能 JSON 前端开发
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
|
12天前
|
消息中间件
RabbitMQ消息模型之Routing-Direct
RabbitMQ消息模型之Routing-Direct
19 1
|
2月前
|
消息中间件 存储 监控
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
32 1
|
12天前
|
消息中间件
RabbitMQ消息模型之发布订阅Publish-Subscribe
RabbitMQ消息模型之发布订阅Publish-Subscribe
16 0
RabbitMQ消息模型之发布订阅Publish-Subscribe
|
3月前
|
消息中间件 存储 安全
SpringBoot与RabbitMQ详解与整合
SpringBoot与RabbitMQ详解与整合
57 0
|
2月前
|
消息中间件 监控 Java
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
Spring Boot中的RabbitMQ死信队列魔法:从异常到延迟,一网打尽【RabbitMQ实战 一】
60 0
|
1月前
|
消息中间件 Java
springboot整合消息队列——RabbitMQ
springboot整合消息队列——RabbitMQ
74 0
|
27天前
|
消息中间件 JSON Java
SpringBoot+RabbitMQ 方式收发消息
SpringBoot+RabbitMQ 方式收发消息
14 0