SpringBoot集成RabbitMQ

在Spring Boot项目中集成RabbitMQ并实现消息的生产和消费是一个常见的任务。以下是一个完整的Spring Boot集成RabbitMQ并实现消息生产者和消费者的案例。


步骤 1:添加依赖

pom.xml中添加Spring Boot RabbitMQ的依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

步骤 2:配置 RabbitMQ

在Spring Boot的application.ymlapplication.properties中配置RabbitMQ连接信息。

使用application.yml
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

步骤 3:创建配置类

创建RabbitMQ的配置类,定义队列、交换机和绑定。

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

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "example.queue";
    public static final String EXCHANGE_NAME = "example.exchange";
    public static final String ROUTING_KEY = "example.routingKey";

    // 定义队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, true);
    }

    // 定义交换机
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    // 定义绑定
    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

步骤 4:生产者

创建一个生产者用于发送消息到RabbitMQ。

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

@Service
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_NAME, 
                RabbitMQConfig.ROUTING_KEY, 
                message
        );
        System.out.println("Message sent: " + message);
    }
}

步骤 5:消费者

创建一个消费者,用于监听RabbitMQ队列并消费消息。

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

@Service
public class RabbitMQConsumer {

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

步骤 6:启动类

确保主类上有@SpringBootApplication注解,并运行项目。


步骤 7:测试生产和消费

在任意地方(例如Controller或CommandLineRunner)调用生产者发送消息。

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 RabbitMQProducer producer;

    @GetMapping("/send")
    public String sendMessage(@RequestParam String message) {
        producer.sendMessage(message);
        return "Message sent: " + message;
    }
}
运行测试:
  1. 启动Spring Boot应用。
  2. 打开浏览器或Postman,访问 http://localhost:8080/send?message=HelloRabbitMQ
  3. 查看控制台输出,消费者会自动接收到消息并打印出来。

完整代码结构

src/main/java
└── com.example.rabbitmqdemo
    ├── RabbitMQConfig.java       # 配置类
    ├── RabbitMQProducer.java     # 生产者
    ├── RabbitMQConsumer.java     # 消费者
    ├── RabbitMQController.java   # 测试用的Controller
    └── RabbitmqDemoApplication.java # Spring Boot主类

运行结果

  1. 生产者
    Message sent: HelloRabbitMQ
    
  2. 消费者
    Received message: HelloRabbitMQ
    

这样,您已经完成了Spring Boot集成RabbitMQ并实现生产和消费的功能!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值