1 搭建初始环境
1.1 引入依赖
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
1.2 配置配置文件
spring:
application:
name: springboot-rabbitmq
rabbitmq:
host: localhost
port: 5672
username: xin
password: xin
virtual-host: xin
2 第一种hello world 模型的使用
2.1 开发生产者
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
//注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void sendMessage(){
rabbitTemplate.convertAndSend("hello","hello Han Han Xin");
}
}
2.2 开发消费者
@Component
//可以在@Queue中设置消息队列的一些属性,比如是否可以持久化,是否自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class Consumer1 {
@RabbitHandler
public void receive1(String message){
System.out.println("消费者获取的消息:" + message);
}
}
3 第二种 (Work Queue)工作队列
3.1 开发生产者
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
//注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void sendMessageForWork(){
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","发送work模型的消息" + i);
}
}
}
3.2 开发消费者
@Component
public class WorkMessage {
@RabbitListener(queuesToDeclare = @Queue(value = "work"))
public void consumer1(String message){
System.out.println("消费者 1:" + message);
}
@RabbitListener(queuesToDeclare = @Queue(value = "work"))
public void consumer2(String message){
System.out.println("消费者 2:" + message);
}
}
3.3 说明
默认在spring AMQP实现中work这个方式是公平调度,如果需要实现能者多劳需要额外配置。
4 第四种 广播模型
4.1 开发生产者
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
//注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//fanout 广播
@Test
public void sendMessageForFanout(){
rabbitTemplate.convertAndSend("logs","","Send Message For Fanout");
}
}
4.2 开发消费者
@Component
public class FanoutConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
)
})
public void consumer1(String message){
System.out.println("consumer1:" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
)
})
public void consumer2(String message){
System.out.println("consumer1:" + message);
}
}
5 第五种 Routing 路由模式
5.1 开发生产者
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
// 注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
// 路由模式 routing
@Test
public void SendMessageForRouting(){
rabbitTemplate.convertAndSend("directs","error","Send Message For Direct");
}
}
5.2 开发消费者
@Component
public class Consumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directs", type = "direct"),
key = {"info", "error", "warn"}
)
}
)
public void consumer1(String message){
System.out.println("consumer1:" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directs", type = "direct"),
key = {"error"}
)
}
)
public void consumer2(String message){
System.out.println("consumer1:" + message);
}
}
6 第六种 动态路由Topic
6.1 开发生产者
@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
// 注入RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
//动态路由 Topic
@Test
public void SendMessageForTopic(){
//参数二 设置不同的值,可以使不同的消费者接收到发送的消息
rabbitTemplate.convertAndSend("RTopics","order","Send Message For Topics");
}
}
6.2 开发消费者
@Component
public class TopicConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "RTopics", type = "topic"),
key = {"user.save", "user.*"}
)
})
public void consumer1(String message){
System.out.println("consumer1:" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "RTopics", type = "topic"),
key = {"order.#", "product.#","user.*"}
)
})
public void consumer2(String message){
System.out.println("consumer2:" + message);
}
}