Springboot集成RabbitMQ

Springboot集成RabbitMQ

概述

Springboot作为一个流行的轻量级框架,对RabbitMQ提供了很好的支持。本篇我们就介绍如何在Spring Boot中集成RabbitMQ。

项目搭建

1、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.javamike</groupId>
    <artifactId>springboot-rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-rabbitmq</name>
    <description>Demo project for Rabbitmq</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、MyAMQPConfig

package com.javamike.config;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 * 自定义消息转换器,默认是jdk的序列化转换器,我们自定义为json的
 */
@Configuration
public class MyAMQPConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}

3、新建一个Springboot测试类,并注入RabbitTemplate和AmqpAdmin,用来操作交换器,队列和绑定等逻辑

	@Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AmqpAdmin amqpAdmin;

4、创建rabbitmq发送消息需要的交换器,队列和绑定

	@Test
    public void create() {
        //创建direct类型Exchange,名称为exchange.direct
        amqpAdmin.declareExchange(new DirectExchange("exchange.direct"));
        //创建fanout类型Exchange,名称为exchange.fanout
        amqpAdmin.declareExchange(new FanoutExchange("exchange.fanout"));
        //创建topic类型Exchange,名称为exchange.topic
        amqpAdmin.declareExchange(new TopicExchange("exchange.topic"));

        //创建名称为direct Queue的队列,durable为true表示持久化
        amqpAdmin.declareQueue(new Queue("direct.queue", true));
        //创建名称为fanout Queue的队列,durable为true表示持久化
        amqpAdmin.declareQueue(new Queue("fanout.queue", true));

        //绑定Queue
        amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.direct", "direct.queue", null));
        amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.direct", "fanout.queue", null));
        amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.fanout", "", null));
        amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.fanout", "", null));
        amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.topic", "direct.#", null));
        amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.topic", "direct.*", null));
    }

5、发送direct类型消息

	@Test
    public void send2Direct() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "这是一条direct消息");
        map.put("data", Arrays.asList("Hello World", 123456, true));

        //对象被序列化以后发送出去
        rabbitTemplate.convertAndSend("exchange.direct", "direct.queue", map);

    }

6、发送fanout类型消息

	@Test
    public void send2Fanout() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "这是一条fanout消息");
        map.put("data", Arrays.asList("Hello World", 123456, true));

        //对象被序列化以后发送出去
        rabbitTemplate.convertAndSend("exchange.fanout", "", map);

    }

7、发送topic类型消息

	@Test
    public void send2Topic() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "这是一条topic消息");
        map.put("data", Arrays.asList("Hello World", 123456, true));

        //对象被序列化以后发送出去
        rabbitTemplate.convertAndSend("exchange.topic", "direct.test", map);

    }

8、接收队列的消息

	@Test
    public void receive() {
        //根据队列名称获取消息
        Object o = rabbitTemplate.receiveAndConvert("direct.queue");
        o.getClass();
        System.out.println(o.getClass());
        System.out.println(o);
    }

9、也可以通过监听消息队列来接收某个队列的消息,queuesToDeclare=@Queue(“fanout.queue”)表示自动创建队列

	@RabbitListener(queuesToDeclare=@Queue("fanout.queue"))
    public void receive(Message message) {
        System.out.println("收到fanout.queue消息 : " + new String(message.getBody()));
    }

代码下载:https://github.com/javaMikes/springboot-rabbitmq

参考资料:https://juejin.im/post/5c7788f3f265da2dab17e1ad

  • 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、付费专栏及课程。

余额充值