Springboot 使用Rabbitmq
Rabbitmq官网文档
jstobigdata 大佬写的案列
一文带你搞定RabbitMQ死信队列
本文源码地址
技术要点
- 四种交换机声明与队列绑定;
- 消息的发送与接收, 自定义消息队列;
一、消息队列的声明与绑定
spring:
rabbitmq:
host: localhost
port: 5672
password: admin123
username: admin
listener:
# 配置手动签收
simple:
acknowledge-mode: manual
# 预读取消息数量
prefetch: 1
direct:
acknowledge-mode: manual
template:
mandatory: true
# 确保消息能够推送到交换机中
publisher-returns: true
rabbitmq:
fanoutExchange:
name: fanoutExchange
queue: 'fanout_queueA,fanout_queueB,fanout_queueC'
directExchange:
name: directExchange
queue: direct_queue
routingKey: ""
topicExchange:
name: topicExchange
queue: topic_queue
# * 匹配一个单词 # 匹配多个单词
routingKey: item.#
headerExchange:
name: headerExchange
queue: header_queueA,header_queueB,header_queueC
routingKey:
ExchangeConfig
具体代码详细请查看 ExchangeConfig.java
初始化之后可在管理界面查看 交换机和队列 已经声明 并且持久化
Exchange
Queue
Queue 绑定的路由
二、消息的收发
注解方式监听消息队列, 此处使用的是,点对点消息传输
package org.blackdragon.listener;
import com.rabbitmq.client.Channel;
import org.blackdragon.entity.MessageBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author black-dragon
* @version V1.0
* @Package 队列监听器
* @date 2022/10/21
* @Copyright
*/
@Component
public class ReceiveListener {
private static final Logger log = LoggerFactory.getLogger(ReceiveListener.class);
/**
* @RabbitListener 注解
* @Exchange 注解 参数内 name: 交换机名称 type: 交换机类型 默认 direct 类型
* @param msg 自定义消息体
* @param tag 消息唯一序列
* @param channel
* @throws IOException
*/
@RabbitListener(bindings = {@QueueBinding(
value = @Queue("direct_queue"),
exchange = @Exchange(name = "directExchange")
)})
public void listenerDirect(MessageBody msg, @Header(AmqpHeaders.DELIVERY_TAG) long tag, Channel channel) throws IOException {
log.info("listenerDirect :tag: {}", tag);
log.info("channel {}", channel.toString());
log.info("msg: {} ", msg.toString());
channel.basicAck(tag, false);
}
}
自定义消息队列监听 需要实现 ChannelAwareMessageListener
package org.blackdragon.listener;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import static org.slf4j.LoggerFactory.getLogger;
/**
* @author black-dragon
* @version V1.0
* @Package 自定义 消息监听器
* @date 2022/10/25
* @Copyright
*/
public class MyListener implements ChannelAwareMessageListener {
private static final Logger log = getLogger(MyListener.class);
@Override
public void onMessage(Message message, Channel channel) throws Exception {
log.info("------------ MyListener -----------");
log.info("message: {}", message.getBody());
try {
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}catch (Exception e) {
log.error("error", e.getMessage());
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
}
}
}
RabbitmqConfig 配置
使用 @RabbitListener 注解方式消息打印
自定义消息监听打印