一、公共流程
1. 引入依赖
对于 SpringBoot 项目,首先需要引入 starter 依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp-test</artifactId>
<scope>test</scope>
</dependency>
2. 配置 application.yml 参数
以 Docker 启动的 RabbitMQ 服务为例,映射到本地。
spring:
application:
name: spring-rabbitmq-demo
rabbitmq:
host: localhost
port: 5672
username: admin
password: admin
virtual-host: ran
二、Work Queue(工作队列模式)
1. 声明队列
将队列的声明交给 Spring 管理,使用 Bean 修饰。
package com.ran.jh.rabbitmq.config;
com.ran.jh.rabbitmq.constant.Constants;
org.springframework.amqp.core.Queue;
org.springframework.amqp.core.QueueBuilder;
org.springframework.context.annotation.Bean;
org.springframework.context.annotation.Configuration;
{
Queue {
QueueBuilder.durable(Constants.WORK_QUEUE).build();
}
}


