Douglas-RabbitMQ+SpringBoot-V1.0.0版本
1.生产者整合
1.1创建生产者SpringBoot工程
图解:以上创建的是一个大的单纯的springboot_rabbitmq项目,在其内将创建两个服务模块(生产者服务/消费者服务)
1.2引入依赖坐标
注意:给mq_producer生产者服务pom引入springboot整合rabbitmq的坐标
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.3编写启动类
1.4编写yml配置
server:
port: 8081 #当前服务的端口
spring:
rabbitmq: #消息中间件
virtual-host: /douglas #mq的虚拟主机
host: 127.0.0.1 #mq的ip地址
username: douglas
password: douglas
template: #生产者发送消息后等待10s未收到回复,确认后重新发送消息,
retry:
enabled: true #开启ask消息回复监听
initial-interval: 10000ms # 10s未收到回复
max-interval: 80000ms # 最多80s秒未收到回复执行
multiplier: 2 # 最多发送两次
publisher-confirms: true # 开启生产者确认
1.5创建MQ的配置类
package com.douglas.mqproducer.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
/**
* @Description
* @Author 以梦为馬 <Douglas1314@126.com>
* @Version V1.0.0
* @Date 2022-04-10 16:00
**/
public class RabbitMqConfig {
/**
* 定义交换机名称
*/
public static final String EXCHANGE_NAME = "boot_topic_exchange";
/**
* 定义队列名称
*/
public static final String QUEUE_NAME = "boot_queue";
/**
* 定义路由
*/
public static final String ROUTING_KEY = "boot.#";
/**
* 创建一个主题模式的交换机
* 将交换机Bean放入spring容器
* topicExchange : 交换机模式 为topic主题模式,并且设置交换机名称
* durable : 是否持久化 选择是
* @author douglas
* @date 2022/4/10 16:08
* @param
* @return com.rabbitmq.client.AMQP.Exchange
**/
@Bean("bootExchange")
public Exchange bootExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
/**
* 创建一个队列,并将其放入spring容器
* durable : 选择持久化队列,设置队列名称
* @author douglas
* @date 2022/4/10 16:14
* @param
* @return org.springframework.amqp.core.Queue
**/
@Bean("bootQueue")
public Queue bootQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
/**
* 绑定队列与交换机
* @author douglas
* @date 2022/4/10 16:22
* @param
* @return org.springframework.amqp.core.Binding
**/
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue")Queue queue,
@Qualifier("bootExchange")Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
}
}
1.6编写消息发送代码
1.6.1启动服务前需要检查
1.6.1.1rabbitmq服务账号密码
1.6.1.2检查rabbitmq的虚拟主机
1.创建虚拟主机
2.绑定虚拟主机
1.6.1.3检查交换机
1.创建douglas虚拟主机的交换机
1.6.1.4检查队列
1.创建douglas虚拟主机的队列
注意:至此rabbitmq服务的配置检查完毕!
1.6.2启动服务发送消息
1.6.2.1发送端接口源码
package com.douglas.mqproducer.controller;
import com.douglas.mqproducer.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description
* @Author 以梦为馬 <Douglas1314@126.com>
* @Version V1.0.0
* @Date 2022-04-10 16:28
**/
@RestController
@RequestMapping("/mq")
public class MqController {
/**
* 注入rabbitmq
*/
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 消息发送入口,接口测试
* @author douglas
* @date 2022/4/10 16:32
* @param
* @return java.lang.String
**/
@PostMapping("/sendMqMessage")
@ResponseBody
public String sendMqMessage(String message){
if(StringUtils.isEmpty(message)){
return "消息内容不能为空!!!";
}
try{
//交换机名称
//需要发送到的队列 路由
//消息内容
rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,
"boot.douglas",
message);
}catch (Exception e){
e.printStackTrace();
return "消息发送失败";
}
return "消息发送成功,内容为:"+message;
}
}
1.6.2.2启动服务
1.启动服务,使用postman进行消息发送接口测试,验证成功。
注意:至此springboot整合rabbitmq消息生产端完成消息发送!!!
2.消费者整合
2.1创建消费者SpringBoot工程
2.2引入依赖坐标
2.3编写yml配置
2.4定义监听类
package com.douglas.mqconsumer.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date;
import static org.springframework.amqp.core.ExchangeTypes.TOPIC;
/**
* @Description
* @Author 以梦为馬 <Douglas1314@126.com>
* @Version V1.0.0
* @Date 2022-04-11 23:25
**/
@Component
public class RabbitMqListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "boot_queue",durable = "true"), //配置队列
exchange = @Exchange(value = "boot_topic_exchange",type = TOPIC), //配置交换机,交换机类型
key = {"boot.#"} //配置路由
))
public void ListenerQueue(Message message){
String body = new String(message.getBody());
System.out.println("收到消息:"+body);
}
}
2.5启动消费者测试
注意:至此springboot整合rabbitmq服务搭建完成。
免费源码:源码地址