springboot rabbitmq 发布订阅 广播模式

根据amqp协议、rabbitmq入门、springboot集成rabbitmq 可知,rabbitmq的广播模式关键是使用fanout类型的exchange,fanout exchange会忽略message中的routing-key、queue中的binding-key,发给绑定exchange的全部queue。

创建fanout类型的exchange

复制代码
import org.springframework.amqp.core.*;
@Configuration
public class MqConfig {
	/**
     * 定义广播交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        final FanoutExchange fanoutExchange = new FanoutExchange("自定义广播类型的交换机名称");
        return fanoutExchange;
    }
}

发送

复制代码
 @Autowired
 private AmqpTemplate amqpTemplate;
    
 //发送到订阅数据的exchange中
 amqpTemplate.convertAndSend("自定义广播类型的交换机名称",
 //fanout类型的exchange会忽略routing-key,所以这里的binding key传空字符串
    "",
    message);

消费

复制代码
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
/**
  * 将数据发送给队列1
  * @param message
  */
 @RabbitListener(bindings = @QueueBinding(value = @Queue("自定义队列1"),
         exchange = @Exchange(value = "自定义广播类型的交换机名称",
                 type = ExchangeTypes.FANOUT),
                 //fanout类型exchange会忽略binding-key
         key = ""))
 public void doSynAddDataToJD(String message) {
     log.info("广播模式,同步数据给订阅方");
 }

  /**
  * 将数据发送给队列2
   * @param message
   */
  @RabbitListener(bindings = @QueueBinding(value = @Queue("自定义队列2"),
          exchange = @Exchange(value = "自定义广播类型的交换机名称",
                  type = ExchangeTypes.FANOUT),
          key = ""))
  public void doSynAddDataToJD(String message) {
      log.info("广播模式,同步数据给订阅方");
  }

总结

实现发布订阅(广播模式)的关键在于对exchange类型的理解,可参考amqp协议、rabbitmq入门、springboot集成rabbitmq 、AMQP 0-9-1 Model Explained,源码中的类型有如下几种

复制代码
package org.springframework.amqp.core;

/**
 * Constants for the standard Exchange type names.
 *
 * @author Mark Fisher
 * @author Gary Russell
 */
public abstract class ExchangeTypes {

	/**
	 * Direct exchange.
	 * routing key和binding key完全匹配
	 */
	public static final String DIRECT = "direct";

	/**
	 * Topic exchange.
	 * binding key可使用通配符来匹配routing key
	 */
	public static final String TOPIC = "topic";

	/**
	 * Fanout exchange.
	 * 会忽略routing key、binding key,消息发送到绑定exchange的全部queue
	 */
	public static final String FANOUT = "fanout";

	/**
	 * Headers exchange.
	 * 使用headers中的属性来匹配,有只匹配一项或者全部匹配可选
	 */
	public static final String HEADERS = "headers";

	/**
	 * System exchange.
	 * 这个类型,暂时缺乏相关资料。
	 */
	public static final String SYSTEM = "system";

}
相关推荐
码云社区17 分钟前
Spring Boot上门按摩服务系统源码:上门到店服务目录与门店协同方案
java·spring boot·后端·上门服务·上门按摩·上门小程序
她的男孩1 小时前
会签选了两个人,待办却生成一条谁也办不了:Forge Admin 按人拆开自选审批人
spring boot·低代码·工作流引擎
头发还在的女程序员2 小时前
Spring Boot云快充协议接入:Netty报文分帧、长度检查与校验处理设计
java·spring boot·后端·充电桩·充电桩小程序·云快充
captain3763 小时前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
جيون داد ناالام ميづ3 小时前
三、MyBatis 与 Spring Boot 集成:极简配置 + 分页 / 乐观锁实战
spring boot·后端·mybatis
行者-全栈开发3 小时前
SpringBoot API 网关实战:Spring Cloud Gateway 路由/限流/熔断/鉴权一体化方案
spring boot·后端·adb
Mr.Java.3 小时前
JetCache自定义序列化实战,Kryo/Protostuff编解码器一步接入
java·spring boot·spring
CoderYanger17 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
学长毕设21 小时前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
行者-全栈开发21 小时前
SpringBoot + GraalVM 原生编译实战:启动时间从8秒到0.5秒的极致优化
java·spring boot·后端