SpringBoot整合RabbitMQ

RabbitMQ是以AMQP协议实现的一种消息中间件产品,
AMQP是Advanced Message Queuing Protocol的简称,它是一个面向消息中间件的开放式标准应用层协议。AMQP中定义了以下标准特性:

消息方向
消息队列
消息路由(包括:点到点模式和发布-订阅模式)
可靠性
安全性

安装RabbitMQ[windows]

安装rabbitMQ需要依赖erlang语言环境,所以需要我们下载erlang的环境安装程序。
Erlang/OTP下载地址:

Downloads - Erlang/OTP

RabbitMQ Server 安装程序下载地址:
Installing on Windows | RabbitMQ

百度网盘下载

链接: https://pan.baidu.com/s/1vyBMJvNRlBuiqS27idFi1g?pwd=0512
提取码: 0512

进入rabbitmq安装目录的sbin目录,在此打开dos命令窗口,执行以下命令

"rabbitmq-plugins.bat" enable rabbitmq_management

如果出现一下提示,说明Erlang环境变量没有配置

接下来我们进行环境变量配置:

找到Erlang的bin安装路径

点击保存 然后 win+r  输入cmd 回车

输入erl测试一下

然后再重新试一下出现如下提示,说明web管理插件安装成功

点击启动rabbitmq

打开浏览器并访问:http://localhost:15672/,并使用默认用户guest登录,密码也为guest,即可进入管理界面

添加依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

rabbit mq连接配置
## rabbitmq config
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

创建Rabbit配置类
配置类主要用来配置队列、交换器、路由等高级信息

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
    @Bean
    public Queue firstQueue() {
      // 创建一个队列,名称为:first
        return new Queue("first");
    }
}

创建消息产生者类

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    public void send() {
        rabbitTemplate.convertAndSend("first", "test rabbitmq message !!!");
    }
}
说明:通过注入AmqpTemplate接口的实例来实现消息的发送, 
AmqpTemplate接口定义了一套针对AMQP协议的基础操作

创建消息消费者

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "first")
public class Receiver {
    @RabbitHandler
    public void process(String msg) {
        System.out.println("receive msg : " + msg);
    }
}
说明:
@RabbitListener注解:定义该类需要监听的队列
@RabbitHandler注解:指定对消息的处理

创建测试类

import com.hst.BootWeiHouApplication;
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.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootMain.class)
public class RabbitmqTest {
    @Autowired
    private Sender sender;
    @Test
    public void testRabbitmq() throws Exception {
        sender.send();
    }
}

启动主程序:SpringBootStart

控制台如果出现以下信息,则说明rabbitmq连接成功
Created new connection: rabbitConnectionFactory#5399f6c5:0/SimpleConnection@6dded900 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 52278]

运行Junit测试类

控制台输出:
receive msg : test rabbitmq message !!!
集成Rabbit MQ完毕!

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、付费专栏及课程。

余额充值