Spring Boot整合 RabbitMQ

文章目录

一. 引入依赖

创建spring项目

或者直接引入依赖

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

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>

二. 添加配置

yml 复制代码
#配置RabbitMQ的基本信息
spring:
 rabbitmq:
 host: 110.41.51.65
 port: 5672 #默认为5672
 username: study
 password: study
 virtual-host: bite #默认值为 /
 # 或者:
 #amqp://username:password@Ip:port/virtual-host
spring:
 rabbitmq:
 addresses: amqp://admin:admin@139.9.84.204:5673/good

三. Work Queue(工作队列模式)

声明队列

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //声明队列
    @Bean("workQueue")
    public Queue workQueue(){
        return QueueBuilder.durable(Constants.WORK_QUEUE).build();
    }
}

生产者

发送消息

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/work")
    public String work(){
        for(int i = 0 ; i < 10; i++){
            //使用内置交换机发送消息
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello spring amqp: work....");
        }
        return "发送成功";
    }
}

消费者

java 复制代码
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkListener {
    @RabbitListener(queues = Constants.WORK_QUEUE)
    public void listenerQueue1(Message message){
        System.out.println("listener 1[" + Constants.WORK_QUEUE + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.WORK_QUEUE)
    public void listenerQueue2(Message message){
        System.out.println("listener 2[" + Constants.WORK_QUEUE + "]收到消息: " + message);
    }
}


四. Publish/Subscribe(发布订阅模式)

声明队列和交换机

java 复制代码
//发布订阅模式
    //声明队列
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
    }
    //声明交换机
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder
                .fanoutExchange(Constants.FANOUT_EXCHANGE)
                .durable(true)
                .build();
    }
    //队列和交换机绑定
    @Bean
    public Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange exchange, 
                                 @Qualifier("fanoutQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange exchange,
                                 @Qualifier("fanoutQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

生产者

java 复制代码
 @RequestMapping("/fanout")
    public String fanoutProduct() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello spring boot: fanout....");
        return "发送成功";
    }

消费者

java 复制代码
@Component
public class FanoutListener {
    
    @RabbitListener(queues = Constants.FANOUT_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.FANOUT_QUEUE1 + "]收到消息: " + message);
    }
    
    @RabbitListener(queues = Constants.FANOUT_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 1[" + Constants.FANOUT_QUEUE2 + "]收到消息: " + message);
    }

}

五. Routing(路由模式)

声明队列和交换机

java 复制代码
//路由模式
    @Bean("directQueue1")
    public Queue directQueue1(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
    }
    @Bean("directQueue2")
    public Queue directQueue2(){
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
    }
    @Bean("directExchange")
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
    }
    @Bean
    public Binding directBinding1(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("aaa");
    }
    @Bean
    public Binding directBinding2(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb");
    }
    @Bean
    public Binding directBinding3(@Qualifier("directExchange")DirectExchange exchange,
                                  @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("ccc");
    }

生产者

java 复制代码
@RequestMapping("/direct")
    public String directProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello spring boot: direct " + routingKey);
        return "发送成功";
    }

消费者

java 复制代码
@Component
public class DirectListener {
    @RabbitListener(queues = Constants.DIRECT_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.DIRECT_QUEUE1 + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.DIRECT_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 2[" + Constants.DIRECT_QUEUE2 + "]收到消息: " + message);
    }
}

六. Topics(通配符模式)

声明队列和交换机

java 复制代码
    //通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }
    @Bean
    public Binding topicBinding1(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("*.aaa");
    }
    @Bean
    public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb.*");
    }
    @Bean
    public Binding topicBinding3(@Qualifier("topicExchange") TopicExchange exchange,
                                 @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("bbb.#");
    }

生产者

java 复制代码
 @RequestMapping("/topic")
    public String topicProduct(String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring boot: topic " + routingKey);
        return "发送成功";
    }


消费者

java 复制代码
@Component
public class TopicListener {
    @RabbitListener(queues = Constants.TOPIC_QUEUE1)
    public void listenerQueue1(Message message) {
        System.out.println("listener 1[" + Constants.TOPIC_QUEUE1 + "]收到消息: " + message);
    }

    @RabbitListener(queues = Constants.TOPIC_QUEUE2)
    public void listenerQueue2(Message message) {
        System.out.println("listener 2[" + Constants.TOPIC_QUEUE2 + "]收到消息: " + message);
    }
}
相关推荐
陈皮波比茶23 分钟前
RabbitMQ 消息队列
分布式·rabbitmq
杨运交25 分钟前
[064][缓存模块]两级缓存实战:基于 Caffeine 和 Redis 的多级缓存设计与实现
spring boot
paopaokaka_luck26 分钟前
基于springboot3+vue3+uniapp的剧本杀预约小程序(AI角色对话、协同过滤算法、地图Api、Echarts图形化分析)
java·前端·spring boot·学习·echarts
Java内核笔记42 分钟前
Spring Boot 4 空安全源码剖析:JSpecify 是怎么让全生态 API null-safe 的
spring boot·后端
MetaLite1 小时前
SpringBoot底座为什么要接管默认配置-自动装配与安全默认值
spring boot·安全·spring
MetaLite1 小时前
SpringBoot项目Maven-BOM统一版本就不会冲突吗
spring boot·后端·maven
李昊哲小课1 小时前
SpringMVC 完整执行流程
spring boot·spring·mvc
蒲锘2 小时前
DevOps 实验阶段四:Infra 节点 + MySQL + Spring Boot 容器化
spring boot·mysql·docker
咖啡八杯3 小时前
统一返回对象设计:AjaxResult 与 R 的取舍
java·spring boot·框架·若依·返回值