Springboot整合Rabbitmq(史上最详细)

前言

Rabbitmq简介这里就不多说了,安装可以见我另外一篇帖子使用Docker安装Rabbitmq,Springboot对Mq协议封装的比较好了,因此其实整合过程很简单,大家按步骤进行操作就可以完成快速整合。
文章末尾处提供了源代码地址,可以下载完整代码。

maven依赖

我这里放工程的完整依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
配置文件

这里在Spring中配置Mq的地址及密码,这里没有设置vhost,是使用默认的,如果你们创建了则可以通过配置指定对应的vhost。

spring:
  #rabbitmq配置
  rabbitmq:
    username: admin
    password: 12345678Aa
    addresses: 127.0.0.1:5672
工程目录结构

目录结构仅供参考。这里做个简单说明:

  • base包中定义了交换机名称、队列名称、路由键名称。
  • config包中是用来绑定交换机和队列以及路由键。
  • mq包中是生产者和消费者。
  • vo包中放着mq的传输对象。
    在这里插入图片描述
功能类详解

QueueConfig中使用@Bean注解将对应的queue、exchange、binding等交给容器统一管理。

@Configuration
public class QueueConfig {
    
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(ExchangeName.DEFAULT.name());
    }

    @Bean
    public DirectExchange notifyMsgDirectExchange() {
        return new DirectExchange(ExchangeName.NOTIFY_MSG.name());
    }

    @Bean
    public Queue notifyMsgQueue() {
        return new Queue(QueueNames.NOTIFY_MSG_QUEUE, true);
    }


    @Bean
    public Binding notifyMsgQueueBinding() {
        return BindingBuilder
                .bind(notifyMsgQueue())
                .to(notifyMsgDirectExchange())
                .with(RoutingKeyName.NOTIFY_MSG);
    }
}

Mq传输对象需要实现Serializable。

@Data
@ToString
public class NotifyMsgSendVO implements Serializable {

    private static final long serialVersionUID = 5905249092659173678L;

    private String priKey;

    private String businessType;

    private String phoneNum;

    private String msg;


}

Mq生产者,直接注入RabbitTemplate,调用方法传入交换机名称及路由键,它会根据之前设置的绑定关系将消息路由到对应的队列中由队列另一端的消费者消费。

@Slf4j
@Component
public class NotifyMsgProducer {
    @Resource
    private RabbitTemplate rabbitTemplate;

  
    public void send(NotifyMsgSendVO notifyMsgSendVO) {
        log.debug("生产消息【{}】",notifyMsgSendVO);
        this.rabbitTemplate.convertAndSend(ExchangeName.NOTIFY_MSG.name(),
                RoutingKeyName.NOTIFY_MSG.name(), notifyMsgSendVO);
    }
}

Mq消费者,使用@RabbitListener注解指定对应的队列即可消费消息,这里值得一说的是,在消费者中需要处理一下异常,如果不处理会导致消费者无法继续消费。

@Component
@Slf4j
public class NotifyMsgConsumer {

    @RabbitListener(queues = QueueNames.NOTIFY_MSG_QUEUE)
    public void msgSend(NotifyMsgSendVO vo) {
        System.out.println("消费者收到消息:"+vo);
    }
}
测试

这里提供了一个http接口,调用接口可以生成一个测试消息。

@RestController
public class TestMqController {

    @Resource
    private NotifyMsgProducer notifyMsgProducer;

    @GetMapping("/produce")
    public String produce() {
        NotifyMsgSendVO vo = new NotifyMsgSendVO();
        vo.setPriKey(UUID.randomUUID().toString());
        vo.setPhoneNum("191xxxxxxxx");
        vo.setBusinessType("msg_send");
        notifyMsgProducer.send(vo);
        return "success";
    }
}

调用接口:http://localhost:9091/produce
查看控制台日志:在这里插入图片描述
可以看到消费者已经收到了消息。
看完记得点赞哦!
下面是源代码地址:https://gitee.com/ErGouGeSiBaKe/springboot-rabbitmq

  • 23
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
Spring Boot框架可以很容易地与RabbitMQ进行集成。为了实现这个目标,你需要在项目的依赖项中添加两个关键的依赖项。首先,你需要添加spring-boot-starter-amqp依赖项,它提供了与RabbitMQ进行通信的必要类和方法。其次,你还需要添加spring-boot-starter-web依赖项,以便在项目中使用Web功能。 在你的项目中创建两个Spring Boot应用程序,一个是RabbitMQ的生产者,另一个是消费者。通过这两个应用程序,你可以实现消息的发送和接收。生产者应用程序负责将消息发送到RabbitMQ的消息队列,而消费者应用程序则负责从队列中接收并处理消息。这样,你就可以实现基于RabbitMQ的消息传递系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合RabbitMQ](https://blog.csdn.net/K_kzj_K/article/details/106642250)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot 整合RabbitMq ,用心看完这一篇就够了](https://blog.csdn.net/qq_35387940/article/details/100514134)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [undefined](undefined)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码大师麦克劳瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值