学习SpringBoot使用Rabbitmq

Springboot 使用Rabbitmq

Rabbitmq官网文档
jstobigdata 大佬写的案列
一文带你搞定RabbitMQ死信队列
本文源码地址

技术要点

  1. 四种交换机声明与队列绑定;
  2. 消息的发送与接收, 自定义消息队列;

一、消息队列的声明与绑定

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 注解方式消息打印
请添加图片描述
自定义消息监听打印
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值