请稍等...

小波Note

四川 · 成都市多云15 ℃
中文

Spring Boot RabbitMQ

成都 (cheng du)2024/8/28 10:51:423.32k预计阅读时间 11 分钟收藏Ctrl + D / ⌘ + D
cover
IT FB(up 主)
后端开发工程师

在 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);
    }
}