RabbitMQ 5种消息分发机制
application.yml
spring:
application:
name: rabbitmq-ying
rabbitmq:
host: localhost
port: 5672
username: ying
password: ying
virtual-host: /ying
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.第一种
生产者生成消息后直接放入通道中,消费者绑定该通道,当有消息进来就进行消费
创建消费者
package com.ying.rabbitmq.example;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class HelloWorld {
//对hello通道进行监听,有消息就消费它
@RabbitListener(queuesToDeclare = @Queue("hello"))
public void receive1(String message){
System.out.println("HelloWorld-----"+message);
}
}
创建生产者
package com.ying.rabbitmq;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void helloworld() {
rabbitTemplate.convertAndSend("hello","hello world");
}
Test执行结果如下,发送消息到hello通道成功被消费
2.第二种
生产者生成消息后直接放入通道中,2个或者多个消费者绑定该通道,消息会通过轮询或者形式分发给不同消费者
创建消费者
package com.ying.rabbitmq.example;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class WorkQueue {
//对work通道进行监听,有消息就消费它
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message) throws InterruptedException {
System.out.println("workqueue 1-----"+message);
}
//对work通道进行监听,有消息就消费它
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message){
System.out.println("workqueue 2-----"+message);
}
}
创建生产者
@Test
void workqueue() {
for (int i = 1; i <= 10; i++) {
rabbitTemplate.convertAndSend("work","hello world "+i);
}
}
Test执行结果如下,发送消息到work通道成功被消费
3.第三种
生产者将消息发送给指定交换机,由交换机把消息通过临时通道给绑定该交换机的消费者进行消息消费。类似广播和订阅消息。
创建消费者
package com.ying.rabbitmq.example;
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 Subscribe {
@RabbitListener(bindings = @QueueBinding(
value = @Queue,//不指定名字,就是临时通道
exchange = @Exchange(name = "Subscribe", type = "fanout") //绑定交换机类型,指定广播类型
))
public void receive1(String message) {
System.out.println("Subscribe1 = " + message);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(name = "Subscribe", type = "fanout")
))
public void receive2(String message) {
System.out.println("Subscribe2 = " + message);
}
}
创建生产者
@Test
public void Subscribe() {
rabbitTemplate.convertAndSend("Subscribe","","类似订阅和广播");
}
Test执行结果如下,发送消息到Subscribe交换机进行广播,
4.第四种
生产者将消息发送给指定交换机,并限制要指定发给那个通道,通道在把消息给绑定他的消费者进行消费
创建消费者
package com.ying.rabbitmq.example;
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 Routing {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),//临时通道
key = {"user", "error"},//绑定要监听的通道
exchange = @Exchange(type = "direct", name = "directs") //指定交换机类型 交换机名字
)})
public void receive1(String message) {
System.out.println("Routing1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(),
key = {"error"},
exchange = @Exchange(type = "direct", name = "directs")
)})
public void receive2(String message) {
System.out.println("Routing2 = " + message);
}
}
创建生产者
@Test
public void Routing() {
rabbitTemplate.convertAndSend("directs", "error", "error 的信息");
rabbitTemplate.convertAndSend("directs", "user", "user 的信息");
}
Test执行结果如下,发送消息到directs交换机进行分发,
第五种
生产者将消息发送给指定交换机,进行匹配通配符和占位符来分发消息给不同的通道,通道在把消息给绑定他的消费者进行消费
创建消费者
package com.ying.rabbitmq.example;
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 Topics {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
key = {"user.*"},//*占位符
exchange = @Exchange(type = "topic", name = "topics")
)
})
public void receive1(String message) {
System.out.println("Topics1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
key = {"order.#"},//#通配符
exchange = @Exchange(type = "topic", name = "topics")
)
})
public void receive2(String message) {
System.out.println("Topics2 = " + message);
}
}
创建生产者
@Test
public void Topics() {
rabbitTemplate.convertAndSend("topics", "user.update", "user.update 的消息");
rabbitTemplate.convertAndSend("topics", "user.update.name", "user.update 的消息");
rabbitTemplate.convertAndSend("topics", "order.test", "order.test 的消息");
rabbitTemplate.convertAndSend("topics", "order.test.jst", "order.test.jst 的消息");
}
Test执行结果如下,发送消息到topics交换机进行分发。