写点什么

Springboot 整合 RabbitMQ 生产端和消费端

作者:不觉心动
  • 2023-06-08
    北京
  • 本文字数:3102 字

    阅读完需:约 10 分钟

Springboot 整合 RabbitMQ(生产端)

步骤如下

1.创建生产者 SpringBoot 项目

2.引入依赖



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


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


<!--RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>


</dependencies>
复制代码

3.编写 yml 配置基本信息

 #配置rabbitmq基本信息 ip port  username password...
spring:
rabbitmq:
#ip 默认localhost 远程172.16.98.133
host: localhost
#密码 默认guest 游客
password: guest
#端口
port: 5672
#用户名 默认guest 游客
username: guest
#虚拟机 默认/
virtual-host: /
复制代码

4.定义交换机,队列以及绑定关系的配置类

package com.wyh.rabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
* @program: SpringBoot_RabbitMQ_Procuder
* @description: RabbitMQ配置类
* @author: 魏一鹤
* @createDate: 2022-03-31 23:49
**/


//用于定义配置类,可替换xml配置文件
@Configuration
public class RabbitMQConfig {


//定义常量
public static final String EXCHANGE_NAME = "boot_topic_exchange" ;
public static final String QUEUE_NAME = "boot_queue" ;


//1 交换机 exchange
//定义Bean
@Bean( "bootExchange" )
public Exchange bootExchange(){
//构建并且返回通配符规则的交换机 durable持久化
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}


//2 队列 queue
//定义Bean
@Bean( "bootQueue" )
public Queue bootQueue() {
//持久化队列
return QueueBuilder.durable(QUEUE_NAME).build();
}


//3 队列和队列的绑定关系 binding
/**
* 1 知道哪个交换机
* 2 知道哪个队列
* 3 设置routingkey 路由键
**/
@Bean
//把交换机和队列进行注入作为参数 @Qualifier精准注入某一个bean
public Binding bindQueueExchange(@Qualifier( "bootQueue" )Queue queue,@Qualifier( "bootExchange" ) Exchange exchange){
//指定交换机和队列的绑定 并且指定路由key #0个或者多个单词 noargs不指定参数
return BindingBuilder.bind(queue).to(exchange).with( "boot.#" ).noargs();
}
}
复制代码

5.注入 RabbitTemplate,调用方法,完成消息发送

package com.wyh.test;


import com.wyh.rabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import javax.annotation.Resource;
import javax.annotation.security.RunAs;


/**
* @program: SpringBoot_RabbitMQ_Procuder
* @description: 测试rabbitmq生产者
* @author: 魏一鹤
* @createDate: 2022-04-02 23:26
**/


@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
//1 注入RabbitTemplate类
@Resource
private RabbitTemplate rabbitTemplate;
//测试
@Test
public void send() {
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.weiyihe" , "boot mq hello!!!" );
}
}
复制代码

6 测试

登录 guest(配置的那个账户).查看对应的虚拟机(配置的那个虚拟机),查看队列以及发送的消息



17 Springboot 整合 RabbitMQ(消费端)

步骤如下

1 创建生产者 SpringBoot 项目

2 引入依赖



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


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


<!--RabbitMQ依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>


</dependencies>
复制代码

3 编写 yml 配置基本信息

 #配置rabbitmq基本信息 ip port  username password...
spring:
rabbitmq:
#ip 默认localhost 远程172.16.98.133
host: localhost
#密码 默认guest 游客
password: weiyihe
#端口
port: 5672
#用户名 默认guest 游客
username: weiyihe
#虚拟机 默认/
virtual-host: /itcast_wyh
复制代码

4 定义监听器,使用 @RabbitListener 注解完成队列监听

package com.wyh.listener;


import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
* @program: SpringBoot_RabbitMQ_Procuder
* @description: RabbitMQ消费者监听器
* @author: 魏一鹤
* @createDate: 2022-04-03 22:39
**/


//表明这是一个配置文件
@Component
public class RabbitMQListener {


//监听器注解 里面是监听的队列的名称
@RabbitListener(queues = "boot_queue" )
public void Listener(Message message) {
//由于消息以及被消费了 想要得到纤细只能重新发送
System.out.println(message); //(Body:'boot mq hello!!!' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.weiyihe, deliveryTag=1, consumerTag=amq.ctag-S-FylSWagEzp7TP8BkTvpA, consumerQueue=boot_queue])


}
}
复制代码

5 测试

SpingBoot 整合 RabbitMQ 总结

1 SpringBoot 提供了快速整合 RabbitMQ 的方式.需要引入 AMQP 依赖


2 基本信息在 YML 中配置,队列交换机以及绑定关系在配置类中使用 Bean 方式配置


3 生产端直接注入 RabbitTemplate 完成消息发送


4 消费端直接引用 @RabbitListener 完成消息接收

用户头像

不觉心动

关注

还未添加个人签名 2019-05-27 加入

还未添加个人简介

评论

发布
暂无评论
Springboot整合RabbitMQ生产端和消费端_6月优质更文活动_不觉心动_InfoQ写作社区