SpringBoot整合RabbitMQ

发布于:2024-08-24 ⋅ 阅读:(80) ⋅ 点赞:(0)

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完毕!


网站公告

今日签到

点亮在社区的每一天
去签到