Spring Boot 集成 RabbitMQ

1.1 添加依赖

pom.xml 中添加 RabbitMQ 的依赖:

<dependencies>
    <!-- Spring Boot Starter for RabbitMQ -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

    <!-- Spring Boot Starter Web (可选,如果需要Web支持) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

1.2 配置 RabbitMQ

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

application.properties 配置示例:
# RabbitMQ 服务器地址
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

# 队列名称
spring.rabbitmq.queue.name=my-queue
application.yml 配置示例:
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    queue:
      name: my-queue

1.3 创建队列配置

在 Spring Boot 中,可以通过 @Bean 定义队列、交换机和绑定关系。

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

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue myQueue() {
        return new Queue("my-queue", true); // true 表示队列持久化
    }
}

1.4 创建消息生产者

使用 RabbitTemplate 发送消息。

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

@Service
public class RabbitMQProducerService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend("my-queue", message);
        System.out.println("Sent message: " + message);
    }
}

1.5 创建消息消费者

使用 @RabbitListener 监听队列中的消息。

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

@Service
public class RabbitMQConsumerService {

    @RabbitListener(queues = "my-queue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

1.6 创建 Controller(可选)

如果需要通过 HTTP 接口发送消息,可以创建一个 Controller。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitMQController {

    @Autowired
    private RabbitMQProducerService rabbitMQProducerService;

    @GetMapping("/send")
    public String sendMessage(@RequestParam String message) {
        rabbitMQProducerService.sendMessage(message);
        return "Message sent: " + message;
    }
}

1.7 启动 RabbitMQ

确保 RabbitMQ 服务已经启动。可以使用以下命令启动 RabbitMQ(需要提前安装 RabbitMQ):

# 启动 RabbitMQ
rabbitmq-server

1.8 运行 Spring Boot 项目

启动 Spring Boot 项目后,可以通过以下方式测试 RabbitMQ 集成:

  1. 访问 http://localhost:8080/send?message=HelloRabbitMQ 发送消息。

  2. 查看控制台日志,确认消费者是否接收到消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值