在 Spring Boot 中使用 RabbitMQ 非常方便。Spring Boot 提供了 spring-boot-starter-amqp 依赖项,它集成了 Spring AMQP 和 RabbitMQ。以下是一个简单的示例,展示了如何在 Spring Boot 应用程序中配置和使用 RabbitMQ。
在你的 pom.xml / build.gradle 文件中添加以下依赖项
pom.xml
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
    build.gradle
        dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
    配置 RabbitMQ 连接
application.yml
        spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
    创建配置类
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 = "hello";
    public static final String EXCHANGE_NAME = "hello-exchange";
    @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("hello.#");
    }
}
    创建生产者
java
        import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void send(String message) {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "hello.routingkey", message);
        System.out.println(" [x] Sent '" + message + "'");
    }
}
    创建消费者
java
        import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQReceiver {
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receive(String message) {
        System.out.println(" [x] Received '" + message + "'");
    }
}
    创建一个简单的控制器
java
        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 RabbitMQSender rabbitMQSender;
    @GetMapping("/send")
    public String send(@RequestParam String message) {
        rabbitMQSender.send(message);
        return "Message sent: " + message;
    }
}
    运行应用程序
java
        import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQApplication {
    public static void main(String[] args) {
        SpringApplication.run(RabbitMQApplication.class, args);
    }
}