七、RabbitMQ 之 SpringAMQP 实现 Topic 交换机

该文章展示了一个使用SpringBoot集成RabbitMQ的示例,通过Topic类型的Exchange实现基于RoutingKey的消息路由。消费者包含两个方法,分别监听不同的队列(queue1和queue2),使用通配符如china.#和#.news来接收特定模式的消息。生产者根据消息内容决定将消息发送到对应的RoutingKey,如china.news、china.lala或lalla.news。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Topic 类型的 Exchange 与 Direct 相比,都是可以根据 RoutingKey 把消息路由到不同的队列。只不过Topic 类型 Exchange 可以让队列在绑定 Routing key 的时候使用通配符!
Routingkey 一般都是有一个或多个单词组成,多个单词之间以”.”分割,

例如: snow.com

通配符规则:

#:匹配一个或多个词

*:匹配不多不少恰好1个词

举例:
item.#:能够匹配 item.spu.insert 或者 item.spu
item.*:只能匹配 item.spu

案例需求:
实现思路如下:

  1. 并利用 @RabbitListener 声明 Exchange、Queue、RoutingKey
  2. 在 consumer 服务中,编写两个消费者方法,分别监听 business.test.topic.queue1business.test.topic.queue2
  3. 在 publisher 中编写测试方法,向business.test.topic发送消息

pom

    <dependencies>
        <!--RabbitMQ 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

yml

server:
  port: 8080
spring:
  rabbitmq:
    host: **.***.**.***
    port: 5672
    username: ****
    password: ****

生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Snow
 * @date: 2023/1/6
 * **************************************************
 * 修改记录(时间--修改人--修改说明):
 */
@RestController
@RequestMapping("/topic")
public class SendController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send/{message}")
    public void send(@PathVariable("message") String message){
        // 交换机名称
        String exchangeName = "business.test.topic";
        // 发送消息
        if(message.contains("china") && message.contains("news")){
            rabbitTemplate.convertAndSend(exchangeName, "china.news", message);
            return;
        }
        if(message.contains("china")){
            rabbitTemplate.convertAndSend(exchangeName, "china.lala", message);
            return;
        }
        if(message.contains("news")){
            rabbitTemplate.convertAndSend(exchangeName, "lalla.news", message);
            return;
        }

    }


}

消费者

import org.springframework.amqp.core.ExchangeTypes;
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;

/**
 * @author: Snow
 * @date: 2023/1/6
 * **************************************************
 * 修改记录(时间--修改人--修改说明):
 */
@Component
public class Consumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "business.test.topic.queue1"),
            exchange = @Exchange(name = "business.test.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息:【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "business.test.topic.queue2"),
            exchange = @Exchange(name = "business.test.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息:【" + msg + "】");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

直达CPU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值