RabbitMQ在springboot中的使用
RabbitMQ在springboot中的使用
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置文件
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: ip
port: 5672
username: ems
password: 123456
virtual-host: /ems
直连模型
生产者
@Autowired
RabbitTemplate rabbitTemplate;
//hello world
@Test
public void hello(){
rabbitTemplate.convertAndSend("hello", "hello rabbitmq");
}
消费者
@Component
//@Queue默认队列是持久化,非独占,不自动删除的
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloConsumer {
@RabbitHandler
public void receive(String message) {
System.out.println("消费者->" + message);
}
}
工作队列模型
生产者
//work queue
@Test
public void work() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work", "work work" + i);
}
}
消费者
//消费者1, @RabbitListener注解也可以使用在方法上
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message) {
System.out.println("消费者1->" + message);
}
//消费者2
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message) {
System.out.println("消费者2->" + message);
}
输出-公平消费
消费者1->work work0
消费者2->work work1
消费者1->work work2
消费者1->work work4
消费者1->work work6
消费者1->work work8
消费者2->work work3
消费者2->work work5
消费者2->work work7
消费者2->work work9
广播模型
生成者
//fanout
@Test
public void fanout() {
rabbitTemplate.convertAndSend("news","", "新闻广播");
}
消费者
//消费者1
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "news", type = "fanout") //指定交换机
)
})
public void receive1(String message) {
System.out.println("消费者1->" + message);
}
//消费者2
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "news", type = "fanout") //指定交换机
)
})
public void receive2(String message) {
System.out.println("消费者2->" + message);
}
直连路由模型
生产者
//route
@Test
public void direct() {
for (int i = 0; i < 10; i++) {
if (i == 4 | i == 8) {
rabbitTemplate.convertAndSend("mail", "vip", "vip邮件" + i);
continue;
}
rabbitTemplate.convertAndSend("mail", "user", "用户邮件" + i);
}
}
消费者
//消费者1
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "mail", type = "direct"), //指定交换机, type默认即为direct, 可省略
key = {"user"}
)
})
public void receive1(String message) {
System.out.println("消费者1->" + message);
}
//消费者2
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "mail"), //指定交换机, type默认即为direct, 可省略
key = {"user", "vip"}
)
})
public void receive2(String message) {
System.out.println("消费者2->" + message);
}
订阅模型
生产者
//topic
@Test
public void topic() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
rabbitTemplate.convertAndSend("report", "user.vip.msg", "vip消息" + i);
continue;
} if (i == 8){
rabbitTemplate.convertAndSend("report", "user.vip.gift", "vip礼物" + i);
continue;
}
rabbitTemplate.convertAndSend("report", "user.msg", "用户消息" + i);
}
}
消费者
//消费者2
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //临时队列
exchange = @Exchange(value = "report", type = "topic"), //指定交换机
key = {"user.#"}
)
})
public void receive2(String message) {
System.out.println("消费者2->" + message);
}