springboot整合rabbitmq

1. 添加依赖

首先,在你的 pom.xml 文件中添加 RabbitMQ 的依赖:

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 配置 RabbitMQ 连接

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

RabbitMQ 连接地址

bash 复制代码
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: admin

3. 创建 RabbitMQ 配置类

创建一个配置类来定义队列、交换器和绑定关系:

java 复制代码
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "testQueue";
    public static final String EXCHANGE_NAME = "testExchange";
    public static final String ROUTING_KEY = "testRoutingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

4. 创建消息生产者

创建一个服务类来发送消息到 RabbitMQ:

java 复制代码
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);
    }
}

5. 创建消息消费者

创建一个监听器类来接收消息:

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQConsumer {

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

6. 测试 RabbitMQ 整合

你可以编写一个简单的测试类来验证 RabbitMQ 的整合是否成功:

java 复制代码
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class RabbitMQTest {

    @Autowired
    private RabbitMQProducer rabbitMQProducer;

    @Test
    public void testRabbitMQ() {
        String message = "Hello RabbitMQ";
        rabbitMQProducer.sendMessage(message);
    }
}
相关推荐
小句4 分钟前
RabbitMQ对接MQTT消息发布指南
分布式·rabbitmq·ruby
little_xianzhong11 分钟前
步骤流程中日志记录方案(类aop)
java·开发语言
抓饼先生13 分钟前
C++ 20 视图view笔记
linux·开发语言·c++·笔记·c++20
大鱼七成饱17 分钟前
再也不怕 Rust Box<T>!这篇文章让你一看就懂
后端
Bunny021219 分钟前
DockerCompose搭建MySQL集群+MongoDB集群+Redis集群+Elasticsearch集群等
后端
沢田纲吉30 分钟前
MySQL 学习二:数据库的操作
数据库·后端·mysql
用户60830892904737 分钟前
Java中的接口(Interface)与抽象类(Abstract Class)
java·后端
小叶lr43 分钟前
python 从pycharm部署到新环境
开发语言·python·pycharm
R_.L1 小时前
【项目】 :C++ - 仿mudou库one thread one loop式并发服务器实现(代码实现)
服务器·开发语言·c++
绝无仅有1 小时前
Redis高级面试题解析:深入理解Redis的工作原理与优化策略
后端·面试·github