首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >SpringBoot 整合 RabbitMQ

SpringBoot 整合 RabbitMQ

作者头像
收心
发布2022-01-17 14:40:30
发布2022-01-17 14:40:30
47900
代码可运行
举报
文章被收录于专栏:Java实战博客Java实战博客
运行总次数:0
代码可运行

说一下 整合的流程: 创建一个配置类,管理 RabbitTemplate 然后 利用 RabbitTemplate的 convertAndSend 方法 发送消息 再利用 @RabbitListener 注解 去监听 消息

首先 Maven springboot 的 parent依赖

代码语言:javascript
代码运行次数:0
运行
复制
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

项目的相关依赖

代码语言:javascript
代码运行次数:0
运行
复制
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.3</version>
            <scope>test</scope>
        </dependency><dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies> <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

然后创建 配置文件 yml

代码语言:javascript
代码运行次数:0
运行
复制
server:
  port: 8080
spring:
  rabbitmq:
    host: 118.31.127.248
    username: 你设置的账号
    password: 你设置的密码
    virtual-host: /govbuy
    port: 5672

去添加一个配置类

代码语言:javascript
代码运行次数:0
运行
复制
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 声明为 配置类
public class RabbitMQConfig {
    /**
     * 设置三部分
     * 交换机 Exchange
     * 队列 Queue
     * 绑定关系 Binding
     */

    // 声明 交换机名称
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    // 声明 队列名称
    public static final String QUEUE_NAME = "boot_queue";

    //1 配置交换机
    @Bean("bootExchange") //设置BeanName 为 bootExchanghe
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    //2 Queue 队列
    @Bean("bootQueue") //设置BeanName 为 bootQueue
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    //3 队列 与交换机的绑定
    /*
     1、要知道那个队列
     2、要知道那个交换机
     3、知道那个路由Key
     @Qualifier  自动装配
     */
    @Bean("bootBind") //设置BeanName 为 bootBind
    public Binding bootBindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }


}

配置类完成,去测试一下吧。

发送端

代码语言:javascript
代码运行次数:0
运行
复制
import com.zanglikun.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProducerTest {
    // 注入 rabbitMQtemplate模板类 ,不注入 就是空指针异常等你
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){
        // 发送消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.hello","这是整合:testSend方法发送的消息");
    }

}

测试完成!

这里 发送端 生产者的整合已经完毕了。

接收端

注意:其他配置 都一样,我们只需要 加上一个 @RabbitListener 注解 完成监听。

因为 @Test 不能写参数

所以 我们 不能再创建一个 单元测试去使用了。

创建一个 TestResive

代码语言:javascript
代码运行次数:0
运行
复制
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author : zanglikun
 * @date : 2021/2/2 14:28
 * @Version: 1.0
 * @Desc : 费劲,没啥好说的
 */
@Component
public class TestResive {

    @RabbitListener(queues = "boot_queue")
    public void getMessage(Message message) {
        System.out.println(message); // 这里 不只是输出 单个 发送的信息,而是 全部输出 消息里面的内容数据
    }
}

输出 的 结果:

代码语言:javascript
代码运行次数:0
运行
复制
(Body:'这是整合:testSend方法发送的消息' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hello, deliveryTag=1, consumerTag=amq.ctag-uiKwYKNNZ-oM9xXITZtxoA, consumerQueue=boot_queue])

简单的 整合就算成功了!!!

完整代码下载:

下载上文源码

特殊说明: 解决问题的光鲜,藏着磕Bug的痛苦。 万物皆入轮回,谁也躲不掉! 以上文章,均是我实际操作,写出来的笔记资料,不会出现全文盗用别人文章!烦请各位,请勿直接盗用!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 发送端
  • 接收端
    • 注意:其他配置 都一样,我们只需要 加上一个 @RabbitListener 注解 完成监听。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档