Springboot整合RabbitMQ

本文介绍了如何使用Spring Boot集成RabbitMQ,包括环境配置、生产者和消费者的实现,以及fanout、routing模式的演示。通过实例展示如何利用Spring的rabbitTemplate进行消息发送与接收,适合初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.搭建初始环境

  • 引入项目依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

注:需要新建项目

  • 配置环境
spring:
  application:
    name: rabbitmq-springboot
  rabbitmq:
    host: 192.168.74.153
    port: 5672
    username: sofia
    password: sofia
    virtual-host: /sofia

注:需要提前安装rabbitMQ,需要配置虚拟主机,本文配置虚拟主机名称为:sofia,安装及使用见
https://editor.csdn.net/md/?articleId=122265369
springboot为rabbitMQ提供了一个rabbittemplate对象,可以直接使用

2.小试一波

生产者:

/**
 * @className TestRabbitMQ
 * @Author sofia
 * @Date 2022/1/2
 **/
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //hello world模式
    @Test
    public void testHelloWorld(){
        rabbitTemplate.convertAndSend("hello","helloworld!");
    }
}

消费者:

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class helloConsumer {

    @RabbitHandler
    public void reciver(String message){
        System.out.println("message = " + message);
    }
}

queuesToDeclare :若队列不存在,则创建一个
还可设置队列的其他属性:

@RabbitListener(queuesToDeclare = @Queue(value = "hello", durable = "true", autoDelete = "false"))

durable :是否持久化、autoDelete :是否自动删除
2. springboot 不同模式API

  • fanout模式
    生产者测试代码:
//fanout模式
    @Test
    public void testFanout(){
        rabbitTemplate.convertAndSend("fanlutEx","","fanout模式下发送的消息");
    }

使用注解的方式将消费者队列绑定到交换机上(@QueueBinding),未声明队列的时候,rabbitMQ会使用临时队列,消息被发送给消费者后就自动删除:
在这里插入图片描述
消费者消费者测试代码:

@Component
public class FanoutConsumer {
    //第一个消费者
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue, //使用临时队列
            exchange = @Exchange(value = "fanlutEx", type = "fanout")
    ))
    public void fanoutReciver(String message){
        System.out.println("message1 is "+message);
    }
    
    //第二个消费者
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue, //使用临时队列
            exchange = @Exchange(value = "fanlutEx", type = "fanout")
    ))
    public void fanoutReciver2(String message){
        System.out.println("message2 is "+message);
    }
}

- routing模式
消费者代码:

@Component
public class routingConsumer {
	//消费者1,路由键"key1","key2","key3"
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "RoutingEx", type = "direct"),
            key = {"key1","key2","key3"}
        )}
    )
    public void routingReciver1(String message){
        System.out.println("routing message1 is " + message);
    }

	//消费者2,路由键"key2","key3"
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "RoutingEx", type = "direct"),
            key = {"key2","key3"}
    )}
    )
    public void routingReciver2(String message){
        System.out.println("routing message2 is " + message);
    }
}

生产者1,路由键key1

 @Test
    public void testRouting1(){
        rabbitTemplate.convertAndSend("RoutingEx","key1","Routing模式下发送的消息");
    }

运行结果:
在这里插入图片描述生产者2,路由键key2

 @Test
    public void testRouting2(){
        rabbitTemplate.convertAndSend("RoutingEx","key2","Routing模式下发送的消息");
    }

运行结果:
在这里插入图片描述这部分太简单了,不想写了

Spring Boot框架可以很容易地与RabbitMQ进行集成。为了实现这个目标,你需要在项目的依赖项中添加两个关键的依赖项。首先,你需要添加spring-boot-starter-amqp依赖项,它提供了与RabbitMQ进行通信的必要类和方法。其次,你还需要添加spring-boot-starter-web依赖项,以便在项目中使用Web功能。 在你的项目中创建两个Spring Boot应用程序,一个是RabbitMQ的生产者,另一个是消费者。通过这两个应用程序,你可以实现消息的发送和接收。生产者应用程序负责将消息发送到RabbitMQ的消息队列,而消费者应用程序则负责从队列中接收并处理消息。这样,你就可以实现基于RabbitMQ的消息传递系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合RabbitMQ](https://blog.csdn.net/K_kzj_K/article/details/106642250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot 整合RabbitMq ,用心看完这一篇就够了](https://blog.csdn.net/qq_35387940/article/details/100514134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值