Spring Boot RabbitMQ - 优先级队列

Spring Boot RabbitMQ - 优先级队列_第1张图片

Docker With RabbitMQ

官方 Docker 镜像仓库地址

https://hub.docker.com/_/rabbitmq

本地运行 RabbitMQ

docker run -d \
--name rabbitmq \
--restart always \
-p 5672:5672 \
-p 15672:15672 \
-e RABBITMQ_DEFAULT_USER=user \
-e RABBITMQ_DEFAULT_PASS=password \
rabbitmq:3-management

访问可视化面板

地址:http://127.0.0.1:15672/

账号:user

密码:password

Spring Boot With RabbitMQ

Spring Boot 集成 RabbitMQ

     
            org.springframework.boot
            spring-boot-starter-amqp
        

基本参数配置

# host & port
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672

Queue / Exchange / Routing 配置

/**
 * RabbitMQ 配置
 */
@Configuration
public class RabbitMQConfig {

    private static final String EXCHANGE = "priority-exchange";

    public static final String QUEUE = "priority-queue";

    private static final String ROUTING_KEY = "priority.queue.#";

    /**
     * 定义优先级队列
     */
    @Bean
    Queue queue() {
        Map args= new HashMap<>();
        args.put("x-max-priority", 100);
        return new Queue(QUEUE, false, false, false, args);
    }

    /**
     * 定义交换器
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(EXCHANGE);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

}

priority queue 定义参考官方文档:https://www.rabbitmq.com/priority.html

Spring Boot RabbitMQ - 优先级队列_第2张图片

Spring Boot 应用启动后,会自动创建 Queue 和 Exchange ,并相互绑定,优先级队列会有如图所示标识。

RabbitMQ Publisher

Spring Boot 相关配置

你可能感兴趣的:(Spring Boot RabbitMQ - 优先级队列)