04消息队列系列-RabbitMQ 利用MQ实现事物补偿
04消息队列系列-RabbitMQ 利用MQ实现事物补偿
一、介绍
本篇使用SpringBoot整合RabbitMQ,为后续业务处理开发做铺垫。
二、整合实战
2.1 创建一个gradle项目,引入amqp依赖
implementation 'org.springframework.boot:spring-boot-starter-amqp'
2.2 在application.properties文件里添加RabbitMQ的配置信息
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
2.3 编写RabbitUtil工具类
package com.lucky.spring.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
/**
* Created by zhangdd on 2020/10/7
*/
public class RabbitUtil {
private static final Logger logger = LoggerFactory.getLogger(RabbitUtil.class);
@Autowired
private RabbitAdmin rabbitAdmin;
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 创建 Exchange
*
* @param exchangeType
* @param exchangeName
*/
public void addExchange(String exchangeType, String exchangeName) {
Exchange exchange = createExchange(exchangeType, exchangeName);
rabbitAdmin.declareExchange(exchange);
}
/**
* 删除一个Exchange
*
* @param exchangeName
* @return
*/
public boolean deleteExchange(String exchangeName) {
return rabbitAdmin.deleteExchange(exchangeName);
}
/**
* 创建一个指定的Queue
*
* @param queueName
*/
public void addQueue(String queueName) {
Queue queue = createQueue(queueName);
rabbitAdmin.declareQueue(queue);
}
/**
* 删除一个 queue
*
* @param queueName
* @return
*/
public boolean deleteQueue(String queueName) {
return rabbitAdmin.deleteQueue(queueName);
}
/**
* 按照筛选条件,删除队列
*
* @param queueName
* @param unused
* @param empty
*/
public void deleteQueue(String queueName, boolean unused, boolean empty) {
rabbitAdmin.deleteQueue(queueName, unused, empty);
}
/**
* 清空某个队列中的消息,注意,清空的消息并没有被消费
*
* @param queueName
*/
public void purgeQueue(String queueName) {
rabbitAdmin.purgeQueue(queueName, false);
}
/**
* 判断指定的队列是否存在
*
* @param queueName
* @return
*/
public boolean existQueue(String queueName) {
return rabbitAdmin.getQueueProperties(queueName) == null ? false : true;
}
/**
* 绑定一个队列到一个匹配型交换器使用一个routingKey
*
* @param exchangeType
* @param exchangeName
* @param queueName
* @param routingKey
* @param isWhereAll
* @param headers EADERS模式类型设置,其他模式类型传空
*/
public void addBinding(String exchangeType, String exchangeName, String queueName,
String routingKey, boolean isWhereAll, Map<String, Object> headers) {
Binding binding = bindingBuilder(exchangeType, exchangeName, queueName, routingKey, isWhereAll, headers);
rabbitAdmin.declareBinding(binding);
}
/**
* 声明绑定
*
* @param binding
*/
public void addBinding(Binding binding) {
rabbitAdmin.declareBinding(binding);
}
/**
* 解除交换器和队列的绑定
*
* @param exchangeType
* @param exchangeName
* @param queueName
* @param routingKey
* @param isWhereAll
* @param headers
*/
public void removeBinding(String exchangeType, String exchangeName, String queueName,
String routingKey, boolean isWhereAll, Map<String, Object> headers) {
Binding binding = bindingBuilder(exchangeType, exchangeName, queueName, routingKey, isWhereAll, headers);
removeBinding(binding);
}
/**
* 解除交换器与队列的绑定
*
* @param binding
*/
public void removeBinding(Binding binding) {
rabbitAdmin.removeBinding(binding);
}
/**
* create a exchange,queue and bind queue at the same time
*
* @param exchangeType
* @param exchangeName
* @param queueName
* @param routingKey
* @param isWhereAll
* @param headers
*/
public void addExchangeBindingQueue(String exchangeType, String exchangeName, String queueName,
String routingKey, boolean isWhereAll, Map<String, Object> headers) {
//声明交换器
addExchange(exchangeType, exchangeName);
//declare queue
addQueue(queueName);
//declare relationship of binding
addBinding(exchangeType, exchangeName, queueName, routingKey, isWhereAll, headers);
}
/**
* send message
*
* @param exchange
* @param routingKey
* @param object
*/
public void convertAndSend(String exchange, String routingKey, final Object object) {
rabbitTemplate.convertAndSend(exchange, routingKey, object);
}
/**
* switch message object
*
* @param messageType
* @param msg
* @return
*/
public Message getMessage(String messageType, Object msg) {
MessageProperties properties = new MessageProperties();
properties.setContentType(messageType);
Message message = new Message(msg.toString().getBytes(), properties);
return message;
}
/**
* declare exchange
*
* @param exchangeType
* @param exchangeName
* @return
*/
private Exchange createExchange(String exchangeType, String exchangeName) {
if (ExchangeTypes.DIRECT.equals(exchangeType)) {
return new DirectExchange(exchangeName);
}
if (ExchangeTypes.TOPIC.equals(exchangeType)) {
return new TopicExchange(exchangeName);
}
if (ExchangeTypes.HEADERS.equals(exchangeType)) {
return new HeadersExchange(exchangeName);
}
if (ExchangeTypes.FANOUT.equals(exchangeType)) {
return new FanoutExchange(exchangeName);
}
return null;
}
/**
* declare relation of binding
*
* @param exchangeType
* @param exchangeName
* @param queueName
* @param routingKey
* @param isWhereAll
* @param headers
* @return
*/
private Binding bindingBuilder(String exchangeType, String exchangeName, String queueName,
String routingKey, boolean isWhereAll, Map<String, Object> headers) {
if (ExchangeTypes.DIRECT.equals(exchangeType)) {
return BindingBuilder.bind(new Queue(queueName))
.to(new DirectExchange(exchangeName))
.with(routingKey);
}
if (ExchangeTypes.TOPIC.equals(exchangeType)) {
return BindingBuilder.bind(new Queue(queueName))
.to(new TopicExchange(exchangeName))
.with(routingKey);
}
if (ExchangeTypes.HEADERS.equals(exchangeType)) {
if (isWhereAll) {
return BindingBuilder.bind(new Queue(queueName))
.to(new HeadersExchange(exchangeName))
.whereAll(headers)
.match();
} else {
return BindingBuilder.bind(new Queue(queueName))
.to(new HeadersExchange(exchangeName))
.whereAny(headers)
.match();
}
}
if (ExchangeTypes.FANOUT.equals(exchangeType)) {
return BindingBuilder.bind(new Queue(queueName))
.to(new FanoutExchange(exchangeName));
}
return null;
}
/**
* declare queue
*
* @param queueName
* @return
*/
private Queue createQueue(String queueName) {
return new Queue(queueName);
}
}
2.4 编写RabbitMQ配置类
package com.lucky.spring.config;
import com.lucky.spring.util.RabbitUtil;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by zhangdd on 2020/10/7
*/
@Configuration
public class RabbitConfig {
/**
* init factory of connection
*
* @param host
* @param port
* @param username
* @param password
* @return
*/
@Bean
public ConnectionFactory connectionFactory(@Value("${spring.rabbitmq.host}") String host,
@Value("${spring.rabbitmq.port}") int port,
@Value("${spring.rabbitmq.username}") String username,
@Value("${spring.rabbitmq.password}") String password) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
/**
* 重新实例化 RabbitAdmin操作类
*
* @param connectionFactory
* @return
*/
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
/**
* 重新实例化 RabbitTemplate操作类
*
* @param connectionFactory
* @return
*/
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//数据转换为json存入消息队列
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
@Bean
public RabbitUtil rabbitUtil() {
return new RabbitUtil();
}
}
2.5 编写队列监听类(静态)
我们已经知道这个概念队列监听这个概念,只需要在方法上加上@RabbitListener(queues = "")
即可收到对应队列的消息。但此时的队列是已经知道了的所以称之为监听静态队列。
package com.lucky.spring.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
/**
* Created by zhangdd on 2020/10/7
*/
@Configuration
public class DirectConsumeListener {
private Logger log = LoggerFactory.getLogger(DirectConsumeListener.class);
/**
* 监听指定队列 mq.direct.1
*
* @param message
*/
@RabbitListener(queues = "mq.direct.1")
public void consume(Message message) {
log.info("DirectConsumeListener,收到消息: {}", message.toString());
}
}
2.6 编写队列监听类(动态)
在服务运行过程中,动态的新增队列,然后在监听新增的队列的行为,就是监听动态队列。
package com.lucky.spring.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by zhangdd on 2020/10/7
*/
@Configuration
public class DynamicConsumeListener {
private Logger log = LoggerFactory.getLogger(DynamicConsumeListener.class);
/**
* 使用 SimpleMessageListenerContainer 实现动态监听
*
* @param connectionFactory
* @return
*/
@Bean
public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setMessageListener(message -> {
log.info("ConsumerMessageListen,收到消息: {}", message.toString());
});
return container;
}
}
如果想向SimpleMessageListenerContainer
添加监听队列或者移除队列,只需要通过如下方式即可操作。
package com.lucky.spring.controller;
import com.lucky.spring.entity.ConsumerInfo;
import com.lucky.spring.util.RabbitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhangdd on 2020/10/7
*/
@RestController
public class ConsumerController {
@Autowired
private SimpleMessageListenerContainer container;
@Autowired
private RabbitUtil rabbitUtil;
private Logger log = LoggerFactory.getLogger(ConsumerController.class);
/**
* 添加队列到监听器
*
* @param consumerInfo
*/
@PostMapping("/consume/addQueue")
public void addQueue(@RequestBody ConsumerInfo consumerInfo) {
boolean existQueue = rabbitUtil.existQueue(consumerInfo.getQueueName());
if (!existQueue) {
throw new RuntimeException("当前队列不存在");
}
//添加mq监听的队列
container.addQueueNames(consumerInfo.getQueueName());
//打印监听容器中正在监听到队列
log.info("container-queue:{}", container.getQueueNames());
}
/**
* 移除正在监听的队列
*
* @param consumerInfo
*/
@PostMapping("/consume/removeQueue")
public void removeQueue(@RequestBody ConsumerInfo consumerInfo) {
//移除mq监听的队列
container.removeQueueNames(consumerInfo.getQueueName());
//打印监听容器中正在监听到队列
log.info("container-queue:{}", container.getQueueNames());
}
/**
* 查询监听容器中正在监听到的队列
*/
@PostMapping("/consume/queryListenerQueue")
public void queryListenerQueue() {
log.info("container-queue:{}", container.getQueueNames());
}
}
2.7 发送消息到交换器
通过如下方式发送消息到交换器。
- 先编写一个请求参数实体类
package com.lucky.spring.entity;
import java.io.Serializable;
/**
* Created by zhangdd on 2020/10/7
*/
public class ProduceInfo implements Serializable {
private static final long serialVersionUID = -5816966739399349770L;
/**
* 交换器名称
*/
private String exchangeName;
/**
* 路由键key
*/
private String routingKey;
/**
* 消息内容
*/
public String msg;
public String getExchangeName() {
return exchangeName;
}
public void setExchangeName(String exchangeName) {
this.exchangeName = exchangeName;
}
public String getRoutingKey() {
return routingKey;
}
public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
- 编写API接口
package com.lucky.spring.controller;
import com.lucky.spring.entity.ConsumerInfo;
import com.lucky.spring.util.RabbitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhangdd on 2020/10/7
*/
@RestController
public class ConsumerController {
@Autowired
private SimpleMessageListenerContainer container;
@Autowired
private RabbitUtil rabbitUtil;
private Logger log = LoggerFactory.getLogger(ConsumerController.class);
/**
* 添加队列到监听器
*
* @param consumerInfo
*/
@PostMapping("/consume/addQueue")
public void addQueue(@RequestBody ConsumerInfo consumerInfo) {
boolean existQueue = rabbitUtil.existQueue(consumerInfo.getQueueName());
if (!existQueue) {
throw new RuntimeException("当前队列不存在");
}
//添加mq监听的队列
container.addQueueNames(consumerInfo.getQueueName());
//打印监听容器中正在监听到队列
log.info("container-queue:{}", container.getQueueNames());
}
/**
* 移除正在监听的队列
*
* @param consumerInfo
*/
@PostMapping("/consume/removeQueue")
public void removeQueue(@RequestBody ConsumerInfo consumerInfo) {
//移除mq监听的队列
container.removeQueueNames(consumerInfo.getQueueName());
//打印监听容器中正在监听到队列
log.info("container-queue:{}", container.getQueueNames());
}
/**
* 查询监听容器中正在监听到的队列
*/
@PostMapping("/consume/queryListenerQueue")
public void queryListenerQueue() {
log.info("container-queue:{}", container.getQueueNames());
}
}
2.8 交换器、队列维护操作
如果想通过接口对RabbitMQ中的交换器、队列以及绑定关系进行维护,通过如下方式接口操作,即可实现。
- 先编写一个请求参数实体类
package com.lucky.spring.entity;
import java.io.Serializable;
/**
* Created by zhangdd on 2020/10/7
*/
public class QueueConfig implements Serializable {
private static final long serialVersionUID = -6576396650731444495L;
/**
* 交换器类型
*/
private String exchangeType;
/**
* 交换器名称
*/
private String exchangeName;
/**
* 队列名称
*/
private String queueName;
/**
* 路由键key
*/
private String routingKey;
public String getExchangeType() {
return exchangeType;
}
public void setExchangeType(String exchangeType) {
this.exchangeType = exchangeType;
}
public String getExchangeName() {
return exchangeName;
}
public void setExchangeName(String exchangeName) {
this.exchangeName = exchangeName;
}
public String getQueueName() {
return queueName;
}
public void setQueueName(String queueName) {
this.queueName = queueName;
}
public String getRoutingKey() {
return routingKey;
}
public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}
}
- 编写接口API
package com.lucky.spring.controller;
import com.lucky.spring.entity.QueueConfig;
import com.lucky.spring.util.RabbitUtil;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* Created by zhangdd on 2020/10/7
*/
@RestController("/config")
public class RabbitController {
@Autowired
private RabbitUtil rabbitUtil;
/**
* 创建交换器
*
* @param config
*/
@PostMapping("addExchange")
public void addExchange(@RequestBody QueueConfig config) {
rabbitUtil.addExchange(config.getExchangeType(), config.getExchangeName());
}
/**
* 删除交换器
*
* @param config
*/
@PostMapping("deleteExchange")
public void deleteExchange(@RequestBody QueueConfig config) {
rabbitUtil.deleteExchange(config.getExchangeName());
}
/**
* 添加队列
*
* @param config
*/
@PostMapping("addQueue")
public void addQueue(@RequestBody QueueConfig config) {
rabbitUtil.addQueue(config.getQueueName());
}
/**
* 删除队列
*
* @param config
*/
@PostMapping("deleteQueue")
public void deleteQueue(@RequestBody QueueConfig config) {
rabbitUtil.deleteQueue(config.getQueueName());
}
/**
* 清空队列数据
*
* @param config
*/
@PostMapping("purgeQueue")
public void purgeQueue(@RequestBody QueueConfig config) {
rabbitUtil.purgeQueue(config.getQueueName());
}
/**
* 添加绑定
*
* @param config
*/
@PostMapping("addBinding")
public void addBinding(@RequestBody QueueConfig config) {
rabbitUtil.addBinding(config.getExchangeType(), config.getExchangeName(), config.getQueueName(), config.getRoutingKey(), false, null);
}
/**
* 解除绑定
*
* @param config
*/
@PostMapping("removeBinding")
public void removeBinding(@RequestBody QueueConfig config) {
rabbitUtil.removeBinding(config.getExchangeType(), config.getExchangeName(), config.getQueueName(), config.getRoutingKey(), false, null);
}
/**
* 创建头部类型的交换器
* 判断条件是所有的键值对都匹配成功才发送到队列
*
* @param config
*/
@PostMapping("andExchangeBindingQueueOfHeaderAll")
public void andExchangeBindingQueueOfHeaderAll(@RequestBody QueueConfig config) {
HashMap<String, Object> header = new HashMap<>();
header.put("queue", "queue");
header.put("bindType", "whereAll");
rabbitUtil.addExchangeBindingQueue(ExchangeTypes.HEADERS, config.getExchangeName(), config.getQueueName(), null, true, header);
}
/**
* 创建头部类型的交换器
* 判断条件是只要有一个键值对匹配成功就发送到队列
*
* @param config
*/
@PostMapping("andExchangeBindingQueueOfHeaderAny")
public void andExchangeBindingQueueOfHeaderAny(@RequestBody QueueConfig config) {
HashMap<String, Object> header = new HashMap<>();
header.put("queue", "queue");
header.put("bindType", "whereAny");
rabbitUtil.addExchangeBindingQueue(ExchangeTypes.HEADERS, config.getExchangeName(), config.getQueueName(), null, false, header);
}
}
三、利用MQ实现事物补偿
上面的操作只是告诉我们怎么使用 rabbitMQ!
当你仔细回想整个过程的时候,其实还是回到最初那个问题,什么时候使用 MQ ?
以常见的订单系统为例,用户点击【下单】按钮之后的业务逻辑可能包括:支付订单、扣减库存、生成相应单据、发红包、发短信通知等等。
在业务发展初期这些逻辑可能放在一起同步执行,随着业务的发展订单量增长,需要提升系统服务的性能,这时可以将一些不需要立即生效的操作拆分出来异步执行,比如发放红包、发短信通知等。这种场景下就可以用 MQ ,在下单的主流程(比如扣减库存、生成相应单据)完成之后发送一条消息到 MQ 让主流程快速完结,而由另外的单独线程拉取 MQ 的消息(或者由 MQ 推送消息),当发现 MQ 中有发红包或发短信之类的消息时,执行相应的业务逻辑。
这种是利用 MQ 实现业务解耦,其它的场景包括最终一致性、广播、错峰流控等等。
利用 MQ 实现业务解耦的过程其实也很简单。
- 当主流程结束之后,将消息推送到发红包、发短信交换器中即可
package com.lucky.spring.service;
import com.lucky.spring.entity.Order;
import com.lucky.spring.util.RabbitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by zhangdd on 2020/10/7
*/
@Service
public class OrderService {
@Autowired
private RabbitUtil rabbitUtil;
@Transactional
public void createOrder(Order order) {
//1、创建订单
//2、调用库存接口,减库存
//3、向客户发放红包
rabbitUtil.convertAndSend("exchange.send.bonus", null, order);
//4、发短信通知
rabbitUtil.convertAndSend("exchange.sms.message", null, order);
}
}
- 监听发红包操作
/**
* 监听发红包
* @param message
* @param channel
* @throws IOException
*/
@RabbitListener(queues = "exchange.send.bonus")
public void consume(Message message, RabbitProperties.Cache.Channel channel) throws IOException {
String msgJson = new String(message.getBody(),"UTF-8");
log.info("收到消息: {}", message.toString());
//调用发红包接口
}
- 监听发短信操作
/**
* 监听发短信
* @param message
* @param channel
* @throws IOException
*/
@RabbitListener(queues = "exchange.sms.message")
public void consume(Message message, AMQImpl.Channel channel) throws IOException {
String msgJson = new String(message.getBody(),"UTF-8");
log.info("收到消息: {}", message.toString());
//调用发短信接口
}
既然 MQ 这么好用,那是不是完全可以将以前的业务也按照整个模型进行拆分呢?
答案显然不是!
当引入 MQ 之后业务的确是解耦了,但是当 MQ 一旦挂了,所有的服务基本都挂了,是不是很可怕!所以这时候就需要RabbitMQ的集群搭建和部署,保证消息几乎100%的投递和消费。
到此结束