一 . RabbitMq的Topic Exchange 模式
生产者将消息发送给Exchange,依据是路由关键字 routingkey (如 : topic.message);
绑定Exchange与Queues队列的依据是bindingKey(如 :topic.#)
代码发送的时候还是一样,第一个参数表示交换机,第二个参数表示routing key,第三个参数即消息。如下:
rabbitTemplate.convertAndSend("testTopicExchange","key1.a.c.key2", " this is RabbitMQ!");
重新认识routingkey和bindingKey:
RabbitMq配置类:
发送消息类的时候:
发送消息时候的routingkey是topic.message,既满足 topic.message,又满足 topic.#, 所以两个消息队列都能收到消息;
如改成routingkey是topic.messages, 则只满足topic.#而不满足topic.message,所以运行结果只有messages那一条:
二 . 代码
1.导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 在yaml文件中配置rabbitMq的基本配置;
server:
port: 8080
spring:
application:
name: springboot_rabbitmq_topic
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
3. 书写RabbitMq的配置类: