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);
    }
}
相关推荐
Ivanqhz27 分钟前
Unigram 算法
开发语言·人工智能·python·深度学习·mlir
程序员阿鹏27 分钟前
双亲委派机制
java·jvm·数据结构·后端
Jazz_z37 分钟前
如何用 C# 读取 Word 中的表格数据
开发语言·c#
65岁退休Coder38 分钟前
把 Agent 框架拆开:PI 开发生产级 Harness
后端·node.js·agent
步行cgn44 分钟前
Spring Bean 的生命周期详解
java·后端·spring
郝学胜-神的一滴1 小时前
C++20模板元编程 04:变量模板 别名模板与模板Lambda实战
服务器·开发语言·数据结构·c++·程序人生
驭渊的小故事1 小时前
Java 项目-jckson序列化工具
java·开发语言
君顾11 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到功能实现
java·开发语言·健身房
爱勇宝1 小时前
用了一个月 WorkBuddy,聊聊我的真实感受
前端·后端·程序员
qq_344920561 小时前
Qt事件过滤器
开发语言·c++·qt