SpringBoot集成RabbitMq

一:导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.0.6.RELEASE</version>
</dependency>

二:配置交换机及队列

package com.zy.rabbitMq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayConfig {
    //延迟交换机
    public static final String delayed_exchange="DELAYED.EXCHANGE";
    //延迟队列
    public static final String delayed_queue="DELAYED.QUEUE";
    //路由
    public static final String delayed_routingKey="delayed.routingKey";

        //声明交换机
    @Bean
    public CustomExchange dalayedExchange(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-delayed-type","direct");
        /**
         * 1:交换机的名称
         * 2:交换机类型
         * 3:是否需要持久化
         * 4:是否需要自动删除
         * 5:其他参数
         */
        return new CustomExchange(delayed_exchange,"x-delayed-message",true,false,map);
    }
    //声明队列
    @Bean
    public Queue getQueue(){
        return QueueBuilder.durable(delayed_queue).build();
    }
    //绑定
    @Bean
    public Binding queueBindingExchange(@Qualifier("getQueue") Queue queue,@Qualifier("dalayedExchange")CustomExchange customExchange){
        return BindingBuilder.bind(queue).to(customExchange).with(delayed_routingKey).noargs();
    }
}

三:创建生产者发送消息

package com.zy.rabbitMq.controller;

import com.zy.rabbitMq.config.ConfirmConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 
 * @createTime 
 * @description:
 */
@Slf4j
@RequestMapping("/confirm")
@RestController
@AllArgsConstructor
public class ConfirmController {
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/send/{msg}")
    public void senConfirmMsg(@PathVariable(name = "msg")String msg){
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE,ConfirmConfig.CONFIRM_ROUTING_KEY,msg,correlationData);
        log.info("发送的消息是:{},==id:{}",msg,"1");

        CorrelationData correlationData1 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE,ConfirmConfig.CONFIRM_ROUTING_KEY+"23","不可路由"+msg,correlationData1);
        log.info("发送错误的交换机的信息:{},==id为:{}",msg,"2");
    }
}

四:消费者接收消息

package com.zy.rabbitMq.delayed;

        import lombok.extern.slf4j.Slf4j;
        import org.springframework.amqp.core.Message;
        import org.springframework.amqp.rabbit.annotation.RabbitListener;
        import org.springframework.stereotype.Component;

        import java.util.Date;

/**
 * @author zyy
 * @createTime 
 * @description:
 */
@Component
@Slf4j
public class DelayedConwumer {

    //开始消费基于插件的延迟消息
    @RabbitListener(queues="DELAYED.QUEUE")
    public void getDelayedMsg(Message message){
        String s = new String(message.getBody());
        log.info("当前时间:{},接收到消息是:{}",new Date().toString(),s);
    }

}

注:根据自己业务逻辑配置交换机及队列,并根据不同业务逻辑设置交换机类型
目前常用的交换机类型为:direct fanout topic topic类型目前最强大

rabbitMq如何保证发送的消息不丢失:

1:队列持久化,2:消息持久化,3:生产者端发布确认

如何保证消息被正常的消费:

1:消费者端开启手动应答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot集成RabbitMQ可以通过以下步骤实现。首先,在配置文件中添加RabbitMQ的连接信息。例如,在application.yml文件中配置RabbitMQ的主机、端口、用户名和密码等信息。\[1\]然后,引入SpringBoot整合RabbitMQ的依赖,包括spring-boot-starter-amqp和spring-rabbit-test等依赖项。\[2\]接下来,可以编写代码来实现与RabbitMQ的交互,例如发送和接收消息等操作。通过使用RabbitTemplate和@RabbitListener等注解,可以方便地实现消息的发送和接收。最后,可以通过运行SpringBoot应用程序来测试RabbitMQ集成是否成功。 #### 引用[.reference_title] - *1* [SpringBoot 集成RabbitMQ](https://blog.csdn.net/July_whj/article/details/120634833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot整合RabbitMQ](https://blog.csdn.net/weixin_49076273/article/details/124991012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot教程(十五) | SpringBoot集成RabbitMq](https://blog.csdn.net/lsqingfeng/article/details/123652520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值