RabbitMQ之Topic发布订阅模式
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
rabbitmq:
host: 192.168.1.38
port: 5672
username: wanson
password: 1
virtual-host: /wanson
TopicRabbitMQConfig.java
package com.uos.handletopic.dao;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopicRabbitMQConfig {
final static String messages1="topic.color";
final static String messages2="topic.color.red";
final static String messages3="topic.msg.feedback";
// 定义队列
@Bean
public Queue queueMessage1(){
return new Queue(TopicRabbitMQConfig.messages1);
}
@Bean
public Queue queueMessage2(){
return new Queue(TopicRabbitMQConfig.messages2);
}
@Bean
public Queue queueMessage3(){
return new Queue(TopicRabbitMQConfig.messages3);
}
// 交换机
@Bean
TopicExchange exchange(){
return new TopicExchange("topicExchange");
}
// 将队列和交换机绑定
@Bean
Binding bindingExchangeMessage1(Queue queueMessage1,TopicExchange exchange){
// queueMessage1绑定topic.color.*
return BindingBuilder.bind(queueMessage1).to(exchange).with("topic.color.*");
}
@Bean
Binding bindingExchangeMessage2(Queue queueMessage2,TopicExchange exchange){
// queueMessage2绑定topic.color.red
return BindingBuilder.bind(queueMessage2).to(exchange).with("topic.color.red");
}
@Bean
Binding bindingExchangeMessage3(Queue queueMessage3,TopicExchange exchange){
// queueMessage3绑定topic.msg.*
return BindingBuilder.bind(queueMessage3).to(exchange).with("topic.msg.*");
}
}
Producer.java 生产者
package com.uos.handletopic.dao;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void produce(String routingKey) {
String context = "topic msg:\t 中国人民万岁!";
System.out.println("Topic Sender,routingKey:\t" + routingKey + ", context:\t" + context);
rabbitTemplate.convertAndSend("topicExchange", routingKey, context);
}
}
Consumer 消费者
package com.uos.handletopic.dao;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void produce(String routingKey) {
String context = "topic msg:\t 中国人民万岁!";
System.out.println("Topic Sender,routingKey:\t" + routingKey + ", context:\t" + context);
rabbitTemplate.convertAndSend("topicExchange", routingKey, context);
}
}
单元测试
package com.uos.handletopic;
import com.uos.handletopic.dao.Producer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HandleTopicApplicationTests {
@Autowired
Producer producer;
@Test
void contextLoads() {
}
@Test
void contextLoadsTopic1() throws InterruptedException {
/*
* 消息routingKey为topic.color.green,交换机匹配到了topic.color.*的bindingKey的队列,
* 所以只有topic.color队列接受到该消息。
* */
producer.produce("topic.color.green");
Thread.sleep(3*1000);
}
@Test
void contextLoadsTopic2() throws InterruptedException {
/*
* 消息routingKey为topic.color.red,交换机匹配到了topic.color.*和topic.color.red两个bindingKey的队列,
* 所以topic.color和topic.color.red两个队列接受到消息。
* */
producer.produce("topic.color.red");
Thread.sleep(3*1000);
}
@Test
void contextLoadsTopic3() throws InterruptedException {
/*
* 消息routingKey为topic.msg.feedback,交换机匹配到了topic.msg.feedback的bindingKey的队列,
* 所以只有topic.msg.feedback队列接受到该消息。
* */
producer.produce("topic.msg.feedback");
Thread.sleep(3*1000);
}
}
posted on 2023-05-22 14:05 Indian_Mysore 阅读(11) 评论(0) 收藏 举报
浙公网安备 33010602011771号