博客
关于我
springCloud整合RabbitMQ实现消息中间件
阅读量:796 次
发布时间:2023-02-27

本文共 3590 字,大约阅读时间需要 11 分钟。

RabbitMQ入门及Spring Cloud整合实践指南

RabbitMQ概述

RabbitMQ是一款开源的消息中间件,基于AMQP协议实现,广泛应用于系统间消息传递的解耦。与Kafka、RocketMQ等工具不同,RabbitMQ在实时性要求不高或并发需求较高的场景中表现优异。以下是RabbitMQ的核心概念:

Broker、Exchange、Queue的关系

  • Broker:消息队列服务器实体,负责接收、存储和转发消息。
  • Exchange:消息交换机,消息首次到达Exchange,通过路由规则将消息发送至目标Queue。
  • Queue:消息队列,消息到达Queue后进入等待状态,等待消费者处理。

Exchange路由规则

  • Direct:严格按照Routing Key路由消息,需明确指定Key值。
  • Topic:基于关键字进行模糊匹配,支持使用通配符(如#*)。
  • Fanout:广播模式,无需配置Key,消息直接发送至所有绑定的Queue。

消息持久化配置

RabbitMQ支持消息持久化,以确保消息安全存储:

  • Exchange持久化:配置durable="true"
  • Queue持久化:配置durable="true"
  • 消息持久化:在发送消息时设置delivery_mode="PERSISTENT"

Spring Cloud整合RabbitMQ

1. 引入依赖

在项目依赖中添加RabbitMQ相关组件:

org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-starter-test

2. 配置RabbitMQ

application.properties中配置RabbitMQ参数:

spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3. 实现消息生产者

@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitmqTemplate;
public void send() {
String content = "hello" + new Date();
System.out.println("Sender: " + content);
rabbitmqTemplate.convertAndSend("hello", content);
}
}

4. 实现消息消费者

@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver: " + hello);
}
}

5. RabbitMQ配置类

@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}

6. 主类与测试类

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class HelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void hello() {
sender.send();
}
}

Direct和Topic交换机的应用

Direct交换机

@Bean
public Queue queueMessage() {
return new Queue("topic.message");
}
@Bean
public Queue queueMessages() {
return new Queue("topic.messages");
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("exchange");
}
@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}
public void send() {
String content = "hello" + new Date();
rabbitmqTemplate.convertAndSend("exchange", "topic.message", "topic_message");
rabbitmqTemplate.convertAndSend("exchange", "topic.messages", "topic_messages");
}

Topic交换机

@Component
@RabbitListener(queues = "topic_message")
public class Receiver1 {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver1: " + hello);
}
}
@Component
@RabbitListener(queues = "topic_messages")
public class Receiver2 {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver2: " + hello);
}
}

Fanout交换机

@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange);
}
public void send() {
String content = "hello" + new Date();
rabbitmqTemplate.convertAndSend("exchange");
}

通过以上配置,可以灵活地实现消息路由策略,充分发挥RabbitMQ的优势。

转载地址:http://rovfk.baihongyu.com/

你可能感兴趣的文章
SQL--mysql索引
查看>>
OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
查看>>
OSChina 周日乱弹 —— 2014 年各种奇葩评论集合
查看>>
OSChina 技术周刊第十期,每周技术抢先看!
查看>>
OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
查看>>
OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
查看>>
OSGi与Maven、Eclipse PlugIn的区别
查看>>
Osgi环境配置
查看>>
OSG——选取和拖拽
查看>>
OSG中找到特定节点的方法(转)
查看>>
OSG学习:C#调用非托管C++方法——C++/CLI
查看>>
OSG学习:几何体的操作(一)——交互事件、简化几何体
查看>>
OSG学习:几何体的操作(二)——交互事件、Delaunay三角网绘制
查看>>
OSG学习:几何对象的绘制(一)——四边形
查看>>
OSG学习:几何对象的绘制(三)——几何元素的存储和几何体的绘制方法
查看>>
OSG学习:几何对象的绘制(二)——简易房屋
查看>>
OSG学习:几何对象的绘制(四)——几何体的更新回调:旋转的线
查看>>
OSG学习:场景图形管理(一)——视图与相机
查看>>
OSG学习:场景图形管理(三)——多视图相机渲染
查看>>
OSG学习:场景图形管理(二)——单窗口多相机渲染
查看>>