RabbitMQ------SpringBoot整合5种模式

目录

      • 推荐公众号
      • 依赖
      • 简单队列
      • work队列
      • 订阅模型-Fanout(广播模式)
      • 订阅模型-Direct(路由模式)
      • 订阅模型-Topic(主题模式)
      • 关于配置队列/交换机/绑定关系

推荐公众号

有彩蛋哦!!!(或者公众号内点击网赚获取彩蛋)
程序员探索之路
如有其它意见欢迎指正微信 zzh_1_2_3

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
版本根据spring自己选择

简单队列

一个生产者对应一个消费者

生产者
@Autowired
private AmqpTemplate amqpTemplate;
    
public String test(@RequestParam(name = "content")String content) throws UnsupportedEncodingException {
        amqpTemplate.convertAndSend("test_simple_queue, content.getBytes("UTF-8"));
        return content;
    }
消费者
@RabbitListener(queuesToDeclare = @Queue(value = "test_simple_queue" declare = "true"))
    public void listen(String message) {
        System.out.println(String.format("简单队列接收消息=%s", message));
    }
结果
简单队列接收消息=1552,abc测试

work队列

一个生产者对应多个消费者,一个消息只会让一个消费者接收

生产者
@Autowired
private AmqpTemplate amqpTemplate;
    
public String test(@RequestParam(name = "content")String content) throws UnsupportedEncodingException {
        amqpTemplate.convertAndSend("test_simple_queue, content.getBytes("UTF-8"));
        return content;
    }
    
定义多个消费者
@RabbitListener(queuesToDeclare = @Queue(value = "test_simple_queue", declare = "true"))
    public void listen(String message) {
        System.out.println(String.format("work队列1接收消息=%s", message));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queuesToDeclare = @Queue(value = "test_simple_queue", declare = "true"))
    public void listen2(String message) {
        System.out.println(String.format("work队列2接收消息=%s", message));
    }

    @RabbitListener(queuesToDeclare = @Queue(value = "test_simple_queue", declare = "true"))
    public void liste3(String message) {
        System.out.println(String.format("work队列3接收消息=%s", message));
    }
结果
work队列1接收消息=1552,abc测试
work队列2接收消息=1552,abc测试

订阅模型-Fanout(广播模式)

概念
1.可以有多个消费者
2.每个消费者有自己的队列
3. 每个队列都要绑定到Exchange(交换机)
4. 生产者发送的消息,只能发送到交换机,交换机来决定要发给哪个队列,生产者无法决 定。
5. 交换机把消息发送给绑定过的所有队列
6. 队列的消费者都能拿到消息。实现一条消息被多个消费者消费
生产者

生产者
String a = "你好,routing";
        amqpTemplate.convertAndSend("TestExchange_fanout", "", a.getBytes("UTF-8"));
消费者
@RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "TestQueue_fanout", durable = "true"),
            exchange = @Exchange(
                    value = "TestExchange_fanout",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            )
    ))
    public void listen(String msg) {
        System.out.println("person 接收到消息:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "TestQueue_fanout2", durable = "true"),
            exchange = @Exchange(
                    value = "TestExchange_fanout",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            )
    ))
    public void fanout(String msg) {
        System.out.println("person 接收到消息:" + msg);
    }
结果两个消费者同时收到
person 接收到消息:你好,routing
person 接收到消息:你好,routing

订阅模型-Direct(路由模式)

每个队列绑定一个rouingKey,交换机向绑定了rouingKey队列发消息
生产者
String a = "TestDirectRout1,你好,routing";
        amqpTemplate.convertAndSend("TestExchangeDirect", "TestRout1", a.getBytes("UTF-8"));
        String b = "你好,TestDirectRout2,routing";
        amqpTemplate.convertAndSend("TestExchangeDirect", "TestRout2", b.getBytes("UTF-8"));
消费者
@RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "TestQueue_direct1", durable = "true"),
            exchange = @Exchange(
                    value = "TestDirectExchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.DIRECT
            ),
            key ={"TestDirectRout1"}
    ))
    public void listen(String msg) {
        System.out.println("TestQueue_direct1 接收到消息:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "TestQueue_direct2", durable = "true"),
            exchange = @Exchange(
                    value = "TestDirectExchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.DIRECT
            ),
            key ={"TestDirectRout2"}
    ))
    public void fanout(String msg) {
        System.out.println("TestQueue_direct2 接收到消息:" + msg);
    }
结果
TestQueue_direct1 接收到消息:TestDirectRout1,你好,routing
TestQueue_direct2 接收到消息:你好,TestDirectRout2,routing

订阅模型-Topic(主题模式)

1.配置文件
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: user
    password: 123
    virtual-host: /userhost
2.配置config绑定关系
public static final String TEST_DIRECT_EXCHANGE = "TestExchange";

    public static final String QUEUE = "TestQueue";
    public static final String ROUTING = "TestRouting";
/**
     * 声明交换机
     * @return
     */
    @Bean
    public DirectExchange ex() {
        return new DirectExchange(TEST_DIRECT_EXCHANGE);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true);
    }

    /**
     * 路由绑定关系
     * @return
     */
    @Bean
    public Binding bind() {
        return BindingBuilder.bind(queue()).to(ex())
                .with(ROUTING);
    }
3.发送消息
	@Autowired
    private AmqpTemplate amqpTemplate;
    
    public String test() throws UnsupportedEncodingException {
        String a = "你好,routing";
        amqpTemplate.convertAndSend(RabbitConfig.TEST_DIRECT_EXCHANGE, RabbitConfig.ROUTING, a.getBytes("UTF-8"));
        return a;
    }
4.消费者
@RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "TestQueue", durable = "true"),
            exchange = @Exchange(
                    value = "TestExchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"TestRou*"}
    ))
    public void listen(String msg) {
        System.out.println("person 接收到消息:" + msg);
    }

关于配置队列/交换机/绑定关系

1.通过纯注解方式
例如下面,直接创建队列Test_direct2,交换机TestExchangeDirect,并且绑定TestRout2
@RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "Test_direct2", durable = "true"),
            exchange = @Exchange(
                    value = "TestExchangeDirect",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.DIRECT
            ),
            key ={"TestRout2"}
    ))
2.通过声明bean方式
 /**
     * 声明交换机
     * @return
     */
    @Bean
    public DirectExchange ex() {
        return new DirectExchange("TestExchange_fanout");
    }
/**
     * 声明队列
     * @return
     */
    @Bean("TestQueue_fanout2")
    public Queue queue() {
        return new Queue("TestQueue_fanout2", true);
    }

    /**
     * 路由绑定关系
     * @return
     */
    @Bean
    public Binding bind() {
        return BindingBuilder.bind(queue()).to(ex())
                .with(ROUTING);
    }

你可能感兴趣的:(RabbitMQ)