Spring boot rabbitmq FanoutExchange发布订阅模式

3 篇文章 0 订阅
2 篇文章 0 订阅
  • 引入jar包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  • 配置连接信息
spring: 
  rabbitmq:
    host: 192.168.1.222
    port: 5672
    username: by_ing
    password: by_ing
    virtual-host: /
  • 创建配置类
package com.ha.elevator.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 订阅模式
 */
@Component
public class RabbitmqFanout {

    public final static String FANOUT_BY = "fanout.by";
    public final static String FANOUT_ING = "fanout.ing";

    /**
     * 订阅模式交换机名称
     */
    private final static String FANOUT_EXCHANGE = "FANOUT_EXCHANGE";

    /**
     * 声明队列
     *
     * @return
     */
    @Bean
    public Queue queueFanoutBy() {
        return new Queue(FANOUT_BY);
    }

    @Bean
    public Queue queueFanoutIng() {
        return new Queue(FANOUT_ING);
    }


    /**
     * Fanout类型的交换机,发布订阅模式
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    /**
     * 绑定交换机队列
     */
    @Bean
    public Binding bindingBy() {
        return BindingBuilder.bind(queueFanoutBy()).to(fanoutExchange());
    }

    /**
     * 绑定交换机队列
     */
    @Bean
    public Binding bindingIng() {
        return BindingBuilder.bind(queueFanoutIng()).to(fanoutExchange());
    }


}
  • 生产者,发送消息到指定交换机中,由交换机去分发到各个队列,使用接口测试工具触发生产者生产消息
package com.ha.mqtt.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitmqController {

    @Autowired
    private AmqpTemplate rabbitmqTemplate;


    @PostMapping(value = "/fanoutSend")
    public String fanoutSend(String msg) {
        rabbitmqTemplate.convertAndSend("FANOUT_EXCHANGE", "", msg);
        return "SUCCESS";
    }

}
  • 定义消费者,消费队列的消息
package com.ha.elevator.rabbitmq;

import com.ha.elevator.config.RabbitmqFanout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 主题订阅模式消费者
 */
@Component
public class FanoutReceiver {

    private Logger logger = LoggerFactory.getLogger(FanoutReceiver.class);

    @RabbitListener(queues = RabbitmqFanout.FANOUT_BY)
    public void fanoutBy(String msg) {
        logger.info("\r\n{} - {}", RabbitmqFanout.FANOUT_BY, msg);
    }

    @RabbitListener(queues = RabbitmqFanout.FANOUT_ING)
    public void fanoutIng(String msg) {
        logger.info("\r\n{} - {}", RabbitmqFanout.FANOUT_ING, msg);
    }

}
  • 测试结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值