rabbitmq基于springboot 发送和接收多个主题代码设计

为了在Spring Boot应用中使用RabbitMQ发送和接收多个主题的消息,我们需要进行几个关键步骤的设计,包括配置RabbitMQ、定义消息队列和交换器、发送消息、以及接收消息。下面是一个完整的示例,展示如何实现这一功能。

1. 引入依赖

确保在pom.xml中包含Spring AMQP和RabbitMQ的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

2. 配置RabbitMQ

application.propertiesapplication.yml中配置RabbitMQ连接信息:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3. 定义消息队列和交换器

创建配置类来定义队列、交换器及它们之间的绑定关系:

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public static final String TOPIC_EXCHANGE_NAME = "myTopicExchange";
    public static final String QUEUE_1_NAME = "queue1";
    public static final String QUEUE_2_NAME = "queue2";

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE_NAME);
    }

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE_1_NAME, true); // durable
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE_2_NAME, true); // durable
    }

    @Bean
    public Binding bindingQueue1ToTopicExchange() {
        return BindingBuilder.bind(queue1()).to(topicExchange()).with("topic1.*");
    }

    @Bean
    public Binding bindingQueue2ToTopicExchange() {
        return BindingBuilder.bind(queue2()).to(topicExchange()).with("topic2.*");
    }
}

4. 发送消息

创建一个服务类来处理消息的发送:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageSenderService {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public MessageSenderService(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessageToTopic1(String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE_NAME, "topic1.key", message);
    }

    public void sendMessageToTopic2(String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE_NAME, "topic2.key", message);
    }
}

5. 接收消息

创建监听器来接收不同队列中的消息:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiverService {

    @RabbitListener(queues = RabbitConfig.QUEUE_1_NAME)
    public void receiveMessageFromQueue1(String message) {
        System.out.println("Received from queue1: " + message);
    }

    @RabbitListener(queues = RabbitConfig.QUEUE_2_NAME)
    public void receiveMessageFromQueue2(String message) {
        System.out.println("Received from queue2: " + message);
    }
}

6. 使用MessageSenderService

在控制器或其他服务中注入MessageSenderService并调用相应的方法来发送消息:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/messages")
public class MessageController {

    private final MessageSenderService messageSenderService;

    @Autowired
    public MessageController(MessageSenderService messageSenderService) {
        this.messageSenderService = messageSenderService;
    }

    @PostMapping("/send-to-topic1")
    public String sendToTopic1(@RequestBody String message) {
        messageSenderService.sendMessageToTopic1(message);
        return "Message sent to topic1";
    }

    @PostMapping("/send-to-topic2")
    public String sendToTopic2(@RequestBody String message) {
        messageSenderService.sendMessageToTopic2(message);
        return "Message sent to topic2";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值