RabbitMQ:SpringAMQP 声明队列和交换机

目录

  • 一、基本概述
  • 二、使用Bean声明
    • [2.1 广播交换机](#2.1 广播交换机)
    • [2.2 定向交换机](#2.2 定向交换机)
    • [2.3 主题交换机](#2.3 主题交换机)
  • 三、使用注解声明
    • [3.1 广播交换机](#3.1 广播交换机)
    • [3.2 定向交换机](#3.2 定向交换机)
    • [3.4 主题交换机](#3.4 主题交换机)

生产者源码
消费者源码

一、基本概述

SpringAMQP提供了及各类,用来声明队列、交换机及其绑定关系:

  • Queue:用于声明队列,可以用工厂类QueueBuilder构建。
  • Exchange:用于声明交换机,可以用工厂类ExchangeBuilder构建。
  • Binding:用于声明队列和交换机的绑定关系,可以用工厂类BindingBuilder构建。

注意:一般情况下,队列和交换机都在消费者服务中注册。

二、使用Bean声明

2.1 广播交换机

java 复制代码
package com.ming.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 广播交换机配置
 */
@Configuration
public class FanoutConfiguration {
    /**
     * 创建广播交换机
     * name: 交换机的名称
     * isDurable: 是否持久化 默认true
     * @return FanoutExchange
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        // new FanoutExchange("fanoutExchange");  // 创建方式1
        return ExchangeBuilder.fanoutExchange("mt.fanout2").durable(true).build();  // 创建方式2
    }

    /**
     * 创建队列
     * @return Queue
     */
    @Bean
    public Queue fanoutQueue3() {
        // new Queue("mt.fanout3");  // 创建方式1
        return QueueBuilder.durable("fanout.queue3").build();  // 创建方式2
    }

    @Bean
    public Queue fanoutQueue4() {
        // new Queue("mt.fanout3");  // 创建方式1
        return QueueBuilder.durable("fanout.queue4").build();  // 创建方式2
    }

    /**
     * 绑定交换机与队列之间的关系 方式1
     * @return Binding
     */
    @Bean
    public Binding fanoutBinding3() {
        return BindingBuilder.bind(fanoutQueue3()).to(fanoutExchange());
    }

    /**
     * 绑定交换机与队列之间的关系 方式2
     * @param fanoutQueue4 队列
     * @param fanoutExchange 交换机
     * @return Binding
     */
    @Bean
    public Binding fanoutBinding4(Queue fanoutQueue4, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue4).to(fanoutExchange);
    }
}

2.2 定向交换机

java 复制代码
package com.ming.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定向交换机配置
 */
@Configuration
public class DirectConfiguration {
    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange("mt.direct2").durable(true).build();
    }

    @Bean
    public Queue directQueue3() {
        return QueueBuilder.durable("direct.queue3").build();
    }

    @Bean
    public Binding directBinding3_red() {
        return BindingBuilder.bind(directQueue3()).to(directExchange()).with("red");
    }

    @Bean
    public Binding directBinding3_ble() {
        return BindingBuilder.bind(directQueue3()).to(directExchange()).with("blue");
    }
}

2.3 主题交换机

java 复制代码
package com.ming.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机配置
 */
@Configuration
public class TopicConfiguration {
    @Bean
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange("mt.topic2").durable(true).build();
    }

    @Bean
    public Queue topicQueue3() {
        return QueueBuilder.durable("topic.queue3").build();
    }

    @Bean
    public Binding topicBinding3_1() {
        return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("china.#");
    }

    @Bean
    public Binding topicBinding3_2() {
        return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("#.news");
    }
}

三、使用注解声明

3.1 广播交换机

java 复制代码
@RabbitListener(bindings = @QueueBinding(
    value = @Queue(name = "fanout.queue5", durable = "true"),
    exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
))
public void listenFanoutQueue3(String message) {
    System.out.println(String.format("消费者3,收到了fanout.queue3: %s", message));
}

@RabbitListener(bindings = @QueueBinding(
   value = @Queue(name = "fanout.queue6", durable = "true"),
   exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
))
public void listenFanoutQueue4(String message) {
    System.err.println(String.format("消费者4,收到了fanout.queue4: %s", message));
}

3.2 定向交换机

java 复制代码
@RabbitListener(bindings = @QueueBinding(
       value = @Queue(name = "direct.queue4", durable = "true"),
        exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),
        key = {"red", "blue"}
))
public void listenDirecttQueue3(String message) {
    System.err.println(String.format("消费者3,收到了direct.queue3: %s", message));
}

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.queue5", durable = "true"),
        exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),
        key = {"red", "yellow"}
))
public void listenDirecttQueue4(String message) {
    System.err.println(String.format("消费者4,收到了direct.queue4: %s", message));
}

3.4 主题交换机

java 复制代码
@RabbitListener(bindings = @QueueBinding(
       value = @Queue(name = "topic.queue4", durable = "true"),
        exchange = @Exchange(name = "mt.topic3", type = ExchangeTypes.TOPIC),
        key = {"china.#", "#.news"}
))
public void listenTopicQueue3(String message) {
    System.out.println(String.format("消费者3,收到了topic.queue3: %s", message));
}