当前位置: 首页 > news >正文

RabbitMQ快速上手及入门

概念

概念:

  • publisher:生产者,也就是发送消息的一方

  • consumer:消费者,也就是消费消息的一方

  • queue:队列,存储消息。生产者投递的消息会暂存在消息队列中,等待消费者处理

  • exchange:交换机,负责消息路由。生产者发送的消息由交换机决定投递到哪个队列。

  • virtual host:虚拟主机,起到数据隔离的作用。每个虚拟主机相互独立,有各自的exchange、queue

helloworld快速上手

启动mq服务

brew services start rabbitmq

浏览器打开:http://localhost:15672/

可以看到成功进入

Username和Password均为guest

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

配置文件

spring:rabbitmq:host: localhost #域名port: 5672 #端口username: guest #用户名password: guest #密码

生产者定义

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.stereotype.Component;@Component
@RequiredArgsConstructor
@Slf4j
public class SpringRabbitListener {@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "test.queue", durable = "true"),exchange = @Exchange(name = "test.direct"),key = "test.key"))public void listenPaySuccess(String msg){System.out.println("消费者接收到topic.queue的消息:【" + msg + "】");}
}

消费者定义

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class SpringAmqtest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testQueue(){rabbitTemplate.convertAndSend("test.direct","test.key", "测试下发送消息");}
}

启动springboot服务

控制台消息

可以看到生产者发送消息且成功被消费者消费

参考资料:

黑马RabbitMQ

http://www.lryc.cn/news/533072.html

相关文章:

  • 4种架构的定义和关联
  • 109,【1】攻防世界 web 题目名称-文件包含
  • leetcode90 子集II
  • DeepSeek模型构建与训练
  • PyTorch torch.unbind、torch.split 和 torch.chunk函数介绍
  • 【愚公系列】《循序渐进Vue.js 3.x前端开发实践》061-Vue Router的动态路由
  • 杭州某小厂面试
  • C基础寒假练习(8)
  • 设计模式 ->模板方法模式(Template Method Pattern)
  • Redis存储⑤Redis五大数据类型之 List 和 Set。
  • MySQL开窗函数种类和使用总结
  • DeepSeek——DeepSeek模型部署实战
  • zsh: command not found: pip
  • 机器学习数学基础:16.方程组
  • 即梦(Dreamina)技术浅析(四):生成对抗网络
  • 2025年软件测试五大趋势:AI、API安全、云测试等前沿实践
  • Vue混入(Mixins)与插件开发深度解析
  • 【C++】C++11
  • k8sollama部署deepseek-R1模型,内网无坑
  • mysql8 C++源码中创建表函数,表字段最大数量限制,表行最大存储限制
  • 胜任力冰山模型:深入探索职业能力的多维结构
  • 什么是三层交换技术?与二层有什么区别?
  • Linux+Docer 容器化部署之 Shell 语法入门篇 【Shell 替代】
  • DeepSeek LLM(初代)阅读报告
  • JAVA异步的TCP 通讯-服务端
  • 高效协同,Tita 助力项目管理场景革新
  • 【AIGC魔童】DeepSeek v3提示词Prompt书写技巧
  • Vue | 透传 Attributes(非 prop 的 attribute )
  • 启明星辰发布MAF大模型应用防火墙产品,提升DeepSeek类企业用户安全
  • Vuex 解析:从 Vue 2 到 Vue 3 的演变与最佳实践