git: https://github.com/miniWiseNut/SpringBoot-RabbitMq
概念
RabbitMQ 是一个由 Erlang 语言开发的 AMQP 的开源实现。
AMQP : Advanced Message Queue,高级消息队列协议。它是应用层协议的一个开放标准,为
面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产品、开发语言
等条件的限制。
RabbitMQ 最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可
用性等方面表现不俗。具体特点包括:
- 可靠性(Reliability) : RabbitMQ 使用一些机制来保证可靠性,如持久化、传输确认、发布
确认。 - 灵活的路由(Flexible Routing) : 在消息进入队列之前,通过 Exchange 来路由消息的。对
于典型的路由功能, RabbitMQ 已经提供了一些内置的 Exchange 来实现。针对更复杂的路
由功能,可以将多个 Exchange 绑定在一起,也通过插件机制实现自己的 Exchange 。 - 消息集群(Clustering) : 多个 RabbitMQ 服务器可以组成一个集群,形成一个逻辑 Broker 。
- 高可用(Highly Available Queues) : 队列可以在集群中的机器上进行镜像, 使得在部分节
点出问题的情况下队列仍然可用。 - 多种协议(Multi-protocol) : RabbitMQ 支持多种消息队列协议,比如 STOMP、 MQTT
等等。 - 多语言客户端(Many Clients) : RabbitMQ 几乎支持所有常用语言,比如 Java、 .NET、
Ruby 等等。 - 管理界面(Management UI) :RabbitMQ 提供了一个易用的用户界面,使得用户可以监控
和管理消息 Broker 的许多方面。 - 跟踪机制(Tracing) :如果消息异常, RabbitMQ 提供了消息跟踪机制,使用者可以找出发生
了什么。 - 插件机制(Plugin System) :RabbitMQ 提供了许多插件,来从多方面进行扩展,也可以编
写自己的插件。
SpringBoot整合RabbitMq
- 配置YML文件:
server:
port: 7777
spring:
application:
name: hello-rabbitmq
rabbitmq:
host: localhost
port: 5672
username: admin
password: admin
listener:
simple:
retry:
enabled: true
max-attempts: 3
initial-interval: 3000
default-requeue-rejected: true
- 创建一个SpringBoot工程,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
- 创建消息接收类
package com.qnut.hellorabbitmq;
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;
/**
* @Description 接收mq消息
* @Author QNut
* @Date 15:23 2020/01/08
* @Version 1.0
**/
@Slf4j
@Component
public class MqReceiver {
// /**
// * @Description
// * 1. 手动创建,需在RabbitMQ中手动创建myQueue1 队列,否则报错
// * @Author QNut
// * @Date 2020/1/8 15:25
// * @param message
// * @return
// **/
// @RabbitListener(queues = "myQueue1")
// public void process1(String message){
// log.info("MqReceiver1: {}", message);
// }
/**
* @Description
* 2. 自动创建队列
* @Author QNut
* @Date 2020/1/8 15:25
* @param message
* @return
**/
@RabbitListener(queuesToDeclare = @Queue("hello"))
public void process2(String message){
log.info("hello接收消息=====》》》》》: {}", message);
}
/**
* @Description
* 3. 自动创建队列,Exchange 与 Queue绑定
* @Author QNut
* @Date 2020/1/8 15:24
* @param message 消息
* @return
**/
@RabbitListener(bindings = @QueueBinding(
value = @Queue("myQueue"),
exchange = @Exchange("testExChange")
))
public void process3(String message){
log.info("MqReceiver接收消息=====》》》》》: {}", message);
}
}
- 创建消息发送类
package com.qnut.hellorabbitmq;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @Description 消息生产者Sender使用AmqpTemplate接口的实例来实现消息的发送
* @Author QNut
* @Date 15:52 2020/01/08
* @Version 1.0
**/
@Slf4j
@Component
@Data
public class Sender {
private final AmqpTemplate amqpTemplate;
@RabbitHandler
public void sender(){
String context = "我欲成仙 " + new Date();
log.info("hello发送消息=========》》》》{}",context);
this.amqpTemplate.convertAndSend("hello",context);
}
@RabbitHandler
public void senderMyQueue(){
String context = "快乐齐天" + new Date();
log.info("myQueue发送消息=========》》》》{}",context);
this.amqpTemplate.convertAndSend("myQueue",context);
}
}
- 创建RabbitMq配置类
package com.qnut.hellorabbitmq;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description Rabbitmq 配置类
* @Author QNut
* @Date 16:15 2020/01/08
* @Version 1.0
**/
@Configuration
public class RabbitConfig {
@Bean
public Queue helloConfig(){
return new Queue("hello");
}
@Bean
public Queue myQueueConfig(){
return new Queue("myQueue");
}
}
- 创建单元测试类
package com.qnut.hellorabbitmq;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HelloRabbitmqApplicationTests {
@Autowired
private Sender sender;
@Test
void contextLoads() {
}
@Test
public void hello(){
sender.sender();
sender.senderMyQueue();
}
}
本文介绍了如何将RabbitMQ与SpringBoot进行整合,提供了配置YML文件、创建消息接收与发送类、RabbitMQ配置类以及单元测试类的详细步骤。RabbitMQ是一个基于AMQP协议的开源消息队列,具有高可靠性、灵活路由、消息集群等特点,广泛应用于分布式系统。
50万+

被折叠的 条评论
为什么被折叠?



