SpringBoot整合RabbitMQ,自动创建交换器和队列代码实现

本文介绍了如何在SpringBoot项目中整合RabbitMQ,包括添加依赖、配置YAML、创建队列和交换器、绑定队列到交换器,以及生产者和消费者的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.依赖:

        <!-- spring boot 整合rabbit MQ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2. yml配置

amqp:
  queue: DIRECT_QUEUE
  exchange: DIRECT_EXCHANGE
  routingkey: DIRECT_QUEUE_KEY

3. 配置类

package com.xx.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApiRabbitMqConfig {

    @Value("${amqp.queue}")
    private String queue;

    @Value("${amqp.exchange}")
    private String exchange;

    @Value("${amqp.routingkey}")
    private String routingKey;

    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue getApiQueue() {
        /*
         * name(必填): 创建消息队列 队列名称:CHANNEL_API_QUEUE
         * durable: 是否持久化,默认false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
         * exclusive: 默认false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
         * autoDelete: 是否自动删除,默认false,当没有生产者或者消费者使用此队列,该队列会自动删除。
         */
        return new Queue(queue, true);
    }

    /**
     * 创建直流交换器
     *
     * @return
     */
    @Bean
    public DirectExchange getApiDirectExchange() {
        /*
         * name(必填): 交换器名称
         * durable: 是否持久化,默认true
         * autoDelete: 是否自动删除,默认false
         */
        return new DirectExchange(exchange, true, false);
    }

    /**
     * 交换器绑定队列
     *
     * @return
     */
    @Bean
    public Binding bindingQueueToExchange() {
        /*
         * 将队列绑定到交换器上, 并设置路由键
         */
        return BindingBuilder.bind(getApiQueue()).to(getApiDirectExchange()).with(routingKey);
    }
}

4. 生产者代码Producer

/**
 * 消息生产者
 */
@Component
@Slf4j
public class Producer {
    @Value("${amqp.exchange}")
    private String exchange;

    @Value("${amqp.routingkey}")
    private String routingKey;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(Message message) {
        log.info("MQ异步推送exchange:{},routingKey:{},message:{}", exchange, routingKey, message);
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}

5.消费者代码

@Component
@Slf4j
public class APIChannelListener {
    @Autowired
    private MessageConverter messageConverter;
    @Autowired
    private APIChannelService apiChannelService;

    /**
     * 监听API服务进行投保单保存并转核保
     * @param message 消息
     */
    @RabbitListener(queues = "${amqp.queue}")
    public void proposalSaveAndSubmit(Message message) {
        try {
            ProposalVo proposalVo = (ProposalVo) messageConverter.fromMessage(message);
            log.info("消费消息: {}", JSONObject.toJSONString(proposalVo));
            // 业务            
            ResBean resBean = apiChannelService.addUser(User, true);
            log.info("响应结果: {}", JSONObject.toJSONString(resBean));
        } catch (Exception e) {
            log.info("错误信息:{}", e.getMessage());
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值