RabbitMQ 基于AMQP协议, 默认的交换机类型direct,direct 直接转发,fanout广播模式,topic带通配符匹配的广播模式
direct
这种发送消息的模式,是最简单也是rabbitmq默认的一种,直接创建队列向其发送消息,等待监听者消费
// 直接注册一个常规队列,向这个队列发送消息
@Component
public class SendMessage {
@Bean
public Queue getQueues() {
return new Queue(RabbitConstant.HELLO_QUEUE);
}
@Autowired
private AmqpTemplate amqpTemplate;
public void send() {
try {
User user = new User();
user.setAddress("地址");
user.setAge("1");
user.setName("mq");
amqpTemplate.convertAndSend(RabbitConstant.HELLO_QUEUE, JSON.toJSONString(user));
} catch (Exception e) {
e.getStackTrace();
}
}
}
这种队列是点对点的发送与接收,一个消息只能被所有监听者中的一个消费
// 监听这个队列
@Component
@RabbitListener(queues = RabbitConstant.HELLO_QUEUE)
public class ReceiverMessageOne {
@RabbitHandler
public void receiverHello(String val) {
System.out.println("one ReceiverMessage: " + val);
}
}
从结果中看出消费者接收消息也是有顺序规律的,消费者轮询拿消息,
one ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
Thrid ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
two ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
one ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
Thrid ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
two ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
one ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
Thrid ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
two ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
one ReceiverMessage: {"address":"地址","age":"1","name":"mq"}
topic
topic 类型的消息模式可以使用通配符,设置交换机转发消息到队列的规则,重点在于交换机和队列的绑定上,配置.#后这个队列可以接受来自其他相似队列的消息
RabbitMqTopicConfiguration 配置交换机与队列
@Configuration
public class RabbitMqTopicConfiguration {
/**
* topic交换机注册
*/
@Bean(name = "paySuccessTopicExchange")
public TopicExchange paySuccessFanoutExchange() {
return new TopicExchange(RabbitConstant.PAY_SUCCESS_TOPIC_EXCHANGE);
}
/**
* topic队列注册
*/
@Bean(name = "paySuccessTopicQueue")
public Queue paySuccessQueue() {
return new Queue(RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE);
}
/**
* topic队列注册
*/
@Bean(name = "paySuccessTopicQueue2")
public Queue paySuccessQueue2() {
return new Queue(RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE_TWO);
}
/**
*
*/
@Bean
public Binding bingPaySuccessFanoutExchangeAndQueue(
@Qualifier("paySuccessTopicExchange") TopicExchange paySuccessTopicExchange,
@Qualifier("paySuccessTopicQueue") Queue paySuccessTopicQueue) {
return BindingBuilder.bind(paySuccessTopicQueue).to(paySuccessTopicExchange).with("com.xin.pay.success.topic.#");
}
@Bean
public Binding bingPaySuccessFanoutExchangeAndQueue2(
@Qualifier("paySuccessTopicExchange") TopicExchange paySuccessTopicExchange,
@Qualifier("paySuccessTopicQueue2") Queue paySuccessTopicQueue) {
return BindingBuilder.bind(paySuccessTopicQueue).to(paySuccessTopicExchange).with(RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE_TWO);
}
}
RabbitMqTopicSendMessage 发送者
@Component
public class RabbitMqTopicSendMessage {
@Autowired
private AmqpTemplate amqpTemplate;
@Bean
public Queue getQueues() {
return new Queue(RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE);
}
@Bean
public Queue getQueues2() {
return new Queue(RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE_TWO);
}
public void send() {
try {
User user = new User();
user.setName("我是队列11111111111111111");
user.setAddress("11111111111111111");
user.setAge("1");
User user2 = new User();
user2.setName("我是队列22222222222222222");
user2.setAddress("22222222222222222");
user2.setAge("2");
amqpTemplate.convertAndSend(
RabbitConstant.PAY_SUCCESS_TOPIC_EXCHANGE, RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE, JSON.toJSONString(user));
amqpTemplate.convertAndSend(
RabbitConstant.PAY_SUCCESS_TOPIC_EXCHANGE, RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE_TWO, JSON.toJSONString(user2));
} catch (Exception e) {
e.getStackTrace();
}
}
RabbitMqTopicReceiverMessage 消费者
@Component
@RabbitListener(queues = RabbitConstant.PAY_SUCCESS_TOPIC_QUEUE)
public class RabbitMqTopicReceiverMessage {
@RabbitHandler
public void receiver(String val){
System.out.println("1 receiver :"+val);
}
}
结果如下,绑定在同一个topic类型交换机下的队列,配置通配符的队列可以接收到,发送在其他队列的消息
2 receiver :{"address":"22222222222222222","age":"2","name":"我是队列22222222222222222"}
1 receiver :{"address":"11111111111111111","age":"1","name":"我是队列11111111111111111"}
1 receiver :{"address":"22222222222222222","age":"2","name":"我是队列22222222222222222"}
Fanout
fanout类型的交换机,是标准的广播模式,订阅了交换机的队列都能拿到消息
RabbitMqFanoutConfiguration
/**
* @Author:
* @Description: 广播交换机没有那么复杂 ,只是将消息推送给所有绑定这个交换机的队列
*/
@Configuration
public class RabbitMqFanoutConfiguration {
/**
* 广播交换机注册
*/
@Bean(name = "paySuccessFanoutExchange")
public FanoutExchange paySuccessFanoutExchange() {
return new FanoutExchange(RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE);
}
/**
* 订阅广播队列注册
*/
@Bean(name = "paySuccessQueue")
public Queue paySuccessQueue() {
return new Queue(RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE_QUEUE);
}
/**
* 订阅广播队列注册
*/
@Bean(name = "paySuccessQueue2")
public Queue paySuccessQueue2() {
return new Queue(RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE_QUEUE2);
}
/**
* 将广播的交换机和队列绑定,一个交换机可以绑定多个队列
*/
@Bean
public Binding bingPaySuccessFanoutExchangeAndQueue(
@Qualifier("paySuccessFanoutExchange") FanoutExchange paySuccessFanoutExchange,
@Qualifier("paySuccessQueue") Queue paySuccessQueue) {
return BindingBuilder.bind(paySuccessQueue).to(paySuccessFanoutExchange);
}
@Bean
public Binding bingPaySuccessFanoutExchangeAndQueue2(
@Qualifier("paySuccessFanoutExchange") FanoutExchange paySuccessFanoutExchange,
@Qualifier("paySuccessQueue2") Queue paySuccessQueue) {
return BindingBuilder.bind(paySuccessQueue).to(paySuccessFanoutExchange);
}
}
RabbitMFanoutSendMessage
@Component
public class RabbitMFanoutSendMessage {
@Autowired
private AmqpTemplate amqpTemplate;
public void send() {
try {
User user = new User();
user.setName("我是队列11111111111111111");
user.setAddress("11111111111111111");
user.setAge("1");
User user2 = new User();
user2.setName("我是队列22222222222222222");
user2.setAddress("22222222222222222");
user2.setAge("2");
amqpTemplate.convertAndSend(
RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE, RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE_QUEUE, JSON.toJSONString(user));
amqpTemplate.convertAndSend(
RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE, RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE_QUEUE2, JSON.toJSONString(user2));
} catch (Exception e) {
e.getStackTrace();
}
}
}
RabbitMqFanoutReceiverMessage
@Component
@RabbitListener(queues = RabbitConstant.PAY_SUCCESS_FANOUT_EXCHANGE_QUEUE2)
public class RabbitMqFanoutReceiverMessage {
@RabbitHandler
public void receiver(String val){
System.out.println("1 receiver :"+val);
}
}
https://blog.csdn.net/clypm/article/details/76178992
来源:CSDN
作者:小富DD
链接:https://blog.csdn.net/xinzhifu1/article/details/83786333