在Spring Boot中集成RabbitMQ,可以利用Spring AMQP项目提供的spring-boot-starter-amqp依赖来简化配置和使用。以下是基本步骤和代码示例,帮助你在Spring Boot应用中集成RabbitMQ。

1. 添加Maven依赖

首先,在你的pom.xml文件中添加spring-boot-starter-amqp依赖:

登录后复制
<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-amqpartifactId>
    dependency>
dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
2. 配置RabbitMQ连接信息

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

application.properties:

登录后复制
springproperties.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  • 1.
  • 2.
  • 3.
  • 4.

application.yml:

登录后复制
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

请根据你的实际情况调整这些设置。

3. 创建RabbitMQ配置类

创建一个配置类来定义队列、交换器以及绑定关系等。

登录后复制
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 RabbitConfig {

    public final static String queueName = "your.queue.name";

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

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("your.exchange.name");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
4. 发送消息

创建一个服务类用于发送消息到指定队列。

登录后复制
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private final String exchangeName = "your.exchange.name";
    private final String routingKey = "your.queue.name";

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(exchangeName, routingKey, message);
        System.out.println("Sent message: " + message);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
5. 接收消息

创建一个监听类用于接收队列中的消息。

登录后复制
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {

    @RabbitListener(queues = "#{queue.name}")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

以上就是Spring Boot集成RabbitMQ的基本流程。通过这种方式,你可以轻松地在Spring Boot应用中实现消息的发送与接收。根据实际需求,你可能还需要对上述配置进行相应的调整和扩展,比如处理不同的消息格式、增加错误处理机制等。