RabbitMQ学习-Seven

再SpringBoot中使用MQ

1.创建SpringBoot项目

除了我们平常使用的一些工具依赖,还需要选择这个Spring for RabbitMQ依赖

2.需要在application.yml文件中进行配置

复制代码
server:
 port :9090
spring:
 application:
  name:producer
 rabbitmq:
  host: 你的主机名
  port: 5672
  virtual-host: 虚拟主机名称
  username: admin
  password: admin123

3.测试使用

复制代码
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
public class TestController {

    @Resource
    private TestService testService;

    @RequestMapping("test")
    public String test(String msg){
        testService.sendMsg(msg);
        return "success";
    }
}

@Service
public class TestService {
    
    @Resource
    private AmqpTemplate amqpTemplate; // 注入RabbitMQ的模板对象,用于发送消息
 
    @Override
    public void sendMsg(String msg) {
        // 发送消息到队列
        amqpTemplate.convertAndSend("queue1", msg);
 
        // 发送消息到交换机(订阅交换机,第二个参数为空)
        amqpTemplate.convertAndSend("wx1", "", msg);
 
        // 发送消息到交换机(路由交换机,第二个参数为路由key)
        amqpTemplate.convertAndSend("wx2", "a", msg);
    }
}

完善一下测试使用类

复制代码
import javax.annotation.Resource;

@Service
public class TestService {

    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMsg(String msg) {
        if (msg.startsWith("q_")) {
            // 发送消息到队列
            amqpTemplate.convertAndSend("queue1", msg);
        } else if (msg.startsWith("f_")) {
            // 发送消息到交换机(订阅交换机)
            amqpTemplate.convertAndSend("ex1", "", msg);
        } else if (msg.startsWith("r_")) {
            // 发送消息到交换机(路由交换机)
            if (msg.startsWith("r_a")) {
                amqpTemplate.convertAndSend("ex2", "a", msg);
            } else if (msg.startsWith("r_b")) {
                amqpTemplate.convertAndSend("ex2", "b", msg);
            }
        }
    }
}

此时从浏览器发送请求:localhost:9090/test?msg=....就可以发送消息到队列中

4.然后创建消费者项目

前期步骤一致,只是配置文件的端口号进行变更

复制代码
package com.qfedu.consumer.service;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
//@RabbitListener(queues ={"queue1","queue2"})
// 首先通过RabbitListener注解监听队列
@RabbitListener(queues = "queue1")
public class ReceiveMsgService {

    // 然后通过RabbitHandler注解将队列中的数据作为入参使用
    @RabbitHandler
    public void receiveMsg(String msg) {
        System.out.println("接收Msg:" + msg);
    }

    // 另一个可能的消息接收方法,处理字节数组类型的数据
    //@RabbitHandler
    //public void receiveMsg(byte[] bs) {
    //    // 处理字节数组的逻辑
    //}
}

此时就在SpringBoot中实现了生产者发送消息,然后消费者消费消息!!

相关推荐
薛先生_0991 小时前
js学习语法第一天
开发语言·javascript·学习
寒秋花开曾相惜4 小时前
(学习笔记)3.8 指针运算(3.8.3 嵌套的数组& 3.8.4 定长数组)
java·开发语言·笔记·学习·算法
是翔仔呐5 小时前
第11章 显示外设驱动:I2C协议OLED屏、SPI协议LCD屏字符/图片/中文显示
c语言·开发语言·stm32·单片机·嵌入式硬件·学习·gitee
_李小白5 小时前
【AI大模型学习笔记之平台篇】第五篇:Trae常用模型介绍与性能对比
人工智能·笔记·学习
承渊政道5 小时前
【优选算法】(实战体会位运算的逻辑思维)
数据结构·c++·笔记·学习·算法·leetcode·visual studio
AI-Ming5 小时前
程序员转行学习 AI 大模型: 踩坑记录:服务器内存不够,程序被killed
服务器·人工智能·python·gpt·深度学习·学习·agi
m0_716765236 小时前
C++提高编程--STL常用容器(set/multiset、map/multimap容器)详解
java·开发语言·c++·经验分享·学习·青少年编程·visual studio
2501_945318496 小时前
零基础学习AI的选型指南:CAIE认证与编程型AI认证如何取舍
人工智能·学习
承渊政道6 小时前
【优选算法】(实战推演模拟算法的蕴含深意)
数据结构·c++·笔记·学习·算法·leetcode·排序算法
Keep learning!6 小时前
PCA主成分分析学习
学习·算法