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集成rabbitmqAMQP 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";

}
相关推荐
hai4058733 分钟前
Spring Boot中的响应与分层解耦架构
spring boot·后端·架构
海里真的有鱼2 小时前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq
工业甲酰苯胺2 小时前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
bjzhang754 小时前
SpringBoot开发——集成Tess4j实现OCR图像文字识别
spring boot·ocr·tess4j
flying jiang4 小时前
Spring Boot 入门面试五道题
spring boot
小菜yh4 小时前
关于Redis
java·数据库·spring boot·redis·spring·缓存
爱上语文6 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
荆州克莱6 小时前
springcloud整合nacos、sentinal、springcloud-gateway,springboot security、oauth2总结
spring boot·spring·spring cloud·css3·技术
小宋10216 小时前
玩转RabbitMQ声明队列交换机、消息转换器
服务器·分布式·rabbitmq
serve the people6 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端