SpringBoot整合RabbitMQ(基础)

**第一步:**添加依赖
pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>


4.0.0
pom

org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE


名字河蟹了
springboot1-rabbitmq
1.0-SNAPSHOT

//整合rabbitmq的核心包

org.springframework.boot
spring-boot-starter-amqp


org.springframework.boot
spring-boot-devtools


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-test



第二步
application.yml配置:

spring:
#整合mq
rabbitmq:
#注意设置自己的ip
host: 192.168.223.201
port: 5672
virtual-host: /qq
username: admin
password: admin
#配置的是等会儿要用的amqp模板
template:
retry:
enabled: true
initial-interval: 2s
#配置的是监听者,即消息接受者
listener:
direct:
retry:
enabled: true
initial-interval: 2s
#配置的时候要注意层级关系.listener是rabbitmq的下一级

第三步SpringBoot启动类
代码:
package spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import spring.sender.Sender;

@SpringBootApplication
public class SpringBootRun {
public static void main(String [] args){
SpringApplication.run(SpringBootRun.class,args);
}
}

第四步获取一个队列Bean
package spring.sender;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class QueueA {
//该方法获取一个叫做someQueue的队列
@Bean
Queue getQueue(){
return new Queue(“someQueue”);
}
}
第五步定义一个消息发送者
代码:
package spring.sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
//重要!!!spring官方提供的模板.
@Autowired
AmqpTemplate amqpTemplate;

public  void send(){
    for (int i= 0 ; i< 10 ;i++ ){
    	//向第四步的someQueue队列发送消息,内容为"再测试一下"
        amqpTemplate.convertAndSend("someQueue","再测试一下"+i);
    }
}

}
第六步再来一个消息监听者(即消息的消费者)
代码:
package spring.listener;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Listener {
@Autowired
AmqpTemplate amqpTemplate;
//注意:注解里面的queues必须要加上
@RabbitListener(queues = “someQueue”)
private void listen(String msg) {
//打印监控到的消息
System.out.println(“监听者收到数据:”+msg);
}
}
第七步开始测试:
package spring.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import spring.sender.Sender;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SenderTest {
@Autowired
Sender sender;
@Test
public void go(){
sender.send();
}
}
//注意:测试之前,先用主类SpringBootRun启动服务,让服务先跑一边,看是否有Bean没初始化.

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值