-
Topic
类型的Exchange
与Direct
相比,都是可以根据RoutingKey
把消息路由到不同的队列。只不过Topic
类型Exchange
可以让队列在绑定Routing key
的时候使用通配符! -
Routingkey
一般都是有一个或多个单词组成,多个单词之间以”.”分割,例如:item.insert
通配符规则:
-
#
:匹配一个或多个词 -
*
:匹配不多不少恰好1个词
举例:
-
item.#
:能够匹配item.spu.insert
或者item.spu
-
item.*
:只能匹配item.spu
解释:
- Queue1:绑定的是
china.#
,因此凡是以china.
开头的routing key
都会被匹配到。包括china.news和china.weather - Queue2:绑定的是
#.news
,因此凡是以.news
结尾的routing key
都会被匹配。包括china.news和japan.news
举例:
public_topic 消息生产者
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMqTopic {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testTopic1(){//发送的目标交换机String exchange = "itcast.topic";String message="我王二天下无敌1";rabbitTemplate.convertAndSend(exchange,"china.RAP",message);}@Testpublic void testTopic2(){//发送的目标交换机String exchange = "itcast.topic";String message="我王二天下无敌2";rabbitTemplate.convertAndSend(exchange,"yangshi.news",message);}@Testpublic void testTopic3(){//发送的目标交换机String exchange = "object.json";Student stu = new Student("王二", 1, new String[]{"sing", "dance", "rap"});rabbitTemplate.convertAndSend(exchange,"china.json",stu);}
}
配置
spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /
consumer_topic 消息消费者
@Component
public class SpringRabbitListener {//- Topic交换机接收的消息RoutingKey必须是多个单词,以 `**.**` 分割//- Topic交换机与队列绑定时的bindingKey可以指定通配符//- `#`:代表0个或多个词//- `*`:代表1个词@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue1"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"china.#"}))public void listenerTopicQueue1(String msg){System.out.println("topicQueue1接收的消息为"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue2"),exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),key = {"#.news"}))public void listenerTopicQueue2(String msg){System.out.println("topicQueue2接收的消息为"+msg);}@RabbitListener(bindings = @QueueBinding(value = @Queue("topicQueue3"),exchange = @Exchange(value = "object.json",type = ExchangeTypes.TOPIC),key = {"#.json"}))public void listenerTopicQueue3(Student msg){System.out.println("topicQueue3接收的对象为"+msg);}
}
启动类配置对象转化可在可视化界面查看
@SpringBootApplication
public class MqTopicApplication {public static void main(String[] args) {SpringApplication.run(MqTopicApplication.class);}//配置消息转换器@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();}
}
配置
spring:rabbitmq:host: 192.168.23.130port: 5672username: itcastpassword: 123321virtual-host: /
所需依赖
<!--AMQP依赖,包含RabbitMQ-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency><!--json转化依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
父依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEASE</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><!--AMQP依赖,包含RabbitMQ--><dependencies><!--json转化依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!--单元测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies>