SpringBoot整合RabbitMQ之发布订阅模式

 更新时间:2025年06月12日 09:13:47   作者:新绿MEHO  
本文主要介绍了SpringBoot整合RabbitMQ之发布订阅模式,涵盖依赖配置、队列与交换机声明绑定、生产者与双消费者代码实现,具有一定的参考价值,感兴趣的可以了解一下

发布订阅模式

引入依赖

<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>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit-test</artifactId>
    <scope>test</scope>
</dependency>

添加配置

spring:
  application:
    name: rabbitmq-springboot
  rabbitmq:
    addresses: amqp://study:study@47.98.109.138:5672/aaa

常量类

public class Constants {

    //发布订阅模式
    public static final String FANOUT_QUEUE1 = "fanout.queue1";
    public static final String FANOUT_QUEUE2 = "fanout.queue2";
    public static final String FANOUT_EXCHANGE = "fanout.exchange";
}

声明队列和交换机并绑定二者关系

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 rabbitmq.constant.Constants;

@Configuration
public class RabbitMQConfig {

    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }

    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build();
    }

    @Bean("fanoutQueueBinding1")
    public Binding fanoutQueueBinding1(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }
    @Bean("fanoutQueueBinding2")
    public Binding fanoutQueueBinding2(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }
}

编写生产者代码

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rabbitmq.constant.Constants;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/fanout")
    public String fanout(){
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"", "hello spring amqp:fanout...");
        return "发送成功";
    }
}

编写消费者代码(含两个队列)

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import rabbitmq.constant.Constants;

@Component
public class FanoutListener {

    @RabbitListener(queues = Constants.FANOUT_QUEUE1)
    public void queueListener1(String message){
        System.out.println("队列["+Constants.FANOUT_QUEUE1+"] 接收到消息:" +message);
    }

    @RabbitListener(queues = Constants.FANOUT_QUEUE2)
    public void queueListener2(String message){
        System.out.println("队列["+Constants.FANOUT_QUEUE2+"] 接收到消息:" +message);
    }
}

生产消息

消费消息

两个队列都收到并消费了消息,且结果符合预期。 

到此这篇关于SpringBoot整合RabbitMQ之发布订阅模式的文章就介绍到这了,更多相关SpringBoot RabbitMQ发布订阅模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深度优先与广度优先Java实现代码示例

    深度优先与广度优先Java实现代码示例

    这篇文章主要介绍了深度优先与广度优先Java实现代码示例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • java 格式化输出数字的方法

    java 格式化输出数字的方法

    在实际工作中,常常需要设定数字的输出格式,如以百分比的形式输出,或者设定小数位数等,现稍微总结如下
    2014-01-01
  • Java Optional用法面试题精讲

    Java Optional用法面试题精讲

    这篇文章主要为大家介绍了Java Optional用法面试题精讲,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Spring Boot项目集成Knife4j接口文档的实例代码

    Spring Boot项目集成Knife4j接口文档的实例代码

    Knife4j就相当于是swagger的升级版,对于我来说,它比swagger要好用得多<BR>,这篇文章主要介绍了Spring Boot项目集成Knife4j接口文档的示例代码,需要的朋友可以参考下
    2021-12-12
  • Commons beanutils组件简介

    Commons beanutils组件简介

    这篇文章主要介绍了commons beanutils组件的相关内容,以及部分实例和基本用法,需要的朋友可以参考下
    2017-09-09
  • SpringBoot如何通过@Profile注解配置多环境

    SpringBoot如何通过@Profile注解配置多环境

    在Spring中,可以使用配置文件的方式来指定不同环境下所需要的配置信息,本文给大家介绍SpringBoot如何通过@Profile注解配置多环境,感兴趣的朋友跟随小编一起看看吧
    2023-06-06
  • 浅谈Java 并发的底层实现

    浅谈Java 并发的底层实现

    这篇文章主要介绍了浅谈Java 并发的底层实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • java采用中文方式显示时间的方法

    java采用中文方式显示时间的方法

    这篇文章主要介绍了java采用中文方式显示时间的方法,实例分析了java时间操作及字符串转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 如何利用NetworkInterface获取服务器MAC地址

    如何利用NetworkInterface获取服务器MAC地址

    今天介绍一种通用的跨平台的操作方式,那就是JDK自带的NetworkInterface接口,该接口在JDK1.4已经出现,但是功能比较少,JDK1.6之后新增了不少新功能,比较不错
    2013-08-08
  • log4j.properties 配置(实例讲解)

    log4j.properties 配置(实例讲解)

    下面小编就为大家带来一篇log4j.properties 配置(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08

最新评论