目录
一、环境搭建
1.1 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.2 配置文件
spring:
rabbitmq:
host: 192.168.248.128
port: 5672
username: guest
password: guest
二、队列模式
2.1 简介
2.2生产者
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
@Autowired
private RabbitTemplate template;
@Test
public void contextLoads() {
//队列名: hello 消息: hello word
template.convertAndSend("hello","hello word");
}
}
2.3 消费者
@Component //默认: 持久化 非独占 不自动删除
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloConsumer {
@RabbitHandler
public void receive(String msg){
System.out.println(msg);
}
}
三、工作队列
3.1 简介
3.2 生产者
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
@Autowired
private RabbitTemplate template;
@Test
public void contextLoads() {
for (int i = 0; i < 10; i++) {
template.convertAndSend("work","work模型"+i);
}
}
}
3.3 消费者
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component //公平消费
public class HelloConsumer {
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String msg){
System.out.println("receive1: "+msg);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String msg){
System.out.println("receive2: "+msg);
}
}
3.4 结果
消息被两个消费者公平的消费完
四、发布订阅
4.1 简介
4.2 生产者
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
@Autowired
private RabbitTemplate template;
@Test //扇出 广播
public void contextLoads() {
for (int i = 0; i < 5; i++) {
template.convertAndSend("logs","","Fanout模型"+i);
}
}
}
4.3 消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class HelloConsumer {
@RabbitListener(
bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "logs",type = "fanout")
)
}
)
public void receive1(String msg){
System.out.println("receive1: "+msg);
}
@RabbitListener(
bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "logs",type = "fanout")
)
}
)
public void receive2(String msg){
System.out.println("receive2: "+msg);
}
}
4.4 结果
消费者1和2消费了同样的内容
五、路由
5.1 简介
5.2 生产者
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
@Autowired
private RabbitTemplate template;
@Test //路由
public void contextLoads() {
template.convertAndSend("directs","info","路由info");
template.convertAndSend("directs","error","路由error");
}
}
5.3 消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class HelloConsumer {
@RabbitListener(
bindings = {@QueueBinding(
value = @Queue,//临时队列
exchange = @Exchange(value = "directs",type = "direct"),
key = {"info","error"}
)}
)
public void receive1(String msg){
System.out.println("receive1: "+msg);
}
@RabbitListener(
bindings = {@QueueBinding(
value = @Queue,//临时队列
exchange = @Exchange(value = "directs",type = "direct"),
key = {"info"}
)}
)
public void receive2(String msg){
System.out.println("receive2: "+msg);
}
}
5.4 结果
发info时都可以收到,发error时只有消费者1才可以收到
六、动态路由
6.1 简介
*表示一个单词
#表示一个或者多个单词
6.2 生产者
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
@Autowired
private RabbitTemplate template;
@Test //路由
public void contextLoads() {
// user. 后面有一个单词
template.convertAndSend("topic","user.save","路由user.save");
// user. 后面有多个单词
template.convertAndSend("topic","user.save.ok","路由user.save.ok");
}
}
6.3 消费者
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class HelloConsumer {
@RabbitListener(
bindings = {@QueueBinding(
value = @Queue,//临时队列
exchange = @Exchange(value = "topic",type = "topic"),
key = {"user.*"}
)}
)
public void receive1(String msg){
System.out.println("receive1: "+msg);
}
@RabbitListener(
bindings = {@QueueBinding(
value = @Queue,//临时队列
exchange = @Exchange(value = "topic",type = "topic"),
key = {"user.#"}
)}
)
public void receive2(String msg){
System.out.println("receive2: "+msg);
}
}
6.4 结果
消费者2可以匹配多个单词,所以都可以消费
七、MQ的应用场景
7.1 异步处理
7.2 应用解耦