1、RabbitMQ 简介
RabbitMQ是使用Erlang语言开发的开源消息队列系统,基于AMQP协议来实现。AMQP的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、 安全。AMQP协议更多用在企业系统内,对数据一致性、稳定性和可靠性要求很高的场景,对性能和吞吐量的要求还在其次
2、RabbitMQ 使用场景
2.1 解耦(最终一致性)
场景说明:用户下单后,订单系统需要通知库存系统。传统的做法是,订单系统调用库存系统的接口。
.
传统模式的缺点:
- 假如库存系统无法访问,则订单减库存将失败,从而导致订单失败
- 订单系统与库存系统耦合
引入消息队列
订单系统:用户下单后,订单系统完成持久化处理,将消息写入消息队列,返回用户订单下单成功
库存系统:订阅下单的消息,采用拉/推的方式,获取下单信息,库存系统根据下单信息,进行库存操作
假如:在下单时库存系统不能正常使用。也不影响正常下单,因为下单后,订单系统写入消息队列就不再关心其他的后续操作了。实现订单系统与库存系统的应用解耦
为了保证库存肯定有,可以将队列大小设置成库存数量,或者采用其他方式解决。
短信、邮件通知、缓存刷新等操作使用消息队列进行通知。
2.2 异步提升效率
2.3 流量削峰
引入MQ:100万用户在高峰期的时候,每秒请求有5000个请求左右,将这5000请求写入MQ里面,系统A每秒最多只能处理2000请求,因为MySQL每秒只能处理2000个请求。系统A从MQ中慢慢拉取请求,每秒就拉取2000个请求,不要超过自己每秒能处理的请求数量即可。MQ,每秒5000个请求进来,结果只有2000个请求出去,所以在秒杀期间(将近一小时)可能会有几十万或者几百万的请求积压在MQ中。
3、RabbitMQ整合springboot
3.1 在windows下的安装配置
rabbitMQ安装教程
启动方式:rabbitmq-server.bat
默认的登录账号:guest,guest
- RabbitMQ遇到java.net.ConnectException: Connection refused: connect
解决方法:
C:\Users\Administrator\AppData\Roaming\RabbitMQ\rabbitmq.config
{tcp_listeners, [5673]}
若没有rabbitmq.config,在安装目录rabbitmq_server-3.7.7\etc找
rabbitmq.config.example 修改后移到上面位置即可
3.2 springboot整合RabbitMQ
1、 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、 属性配置
#rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5673
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
spring.rabbitmq.listener.simple.prefetch= 1
spring.rabbitmq.listener.simple.auto-startup=true
spring.rabbitmq.listener.simple.default-requeue-rejected= true
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=1000ms
spring.rabbitmq.template.retry.max-attempts=3
spring.rabbitmq.template.retry.max-interval =10000ms
spring.rabbitmq.template.retry.multiplier=1.0
3.3 RabbitMQ的四种模式
1、 Derect模式
Derect模式适合一对一的存入读取的队列
- MQConfig类
@Configuration
public class MQConfig {
public static final String QUEUE="queue";
@Bean
public Queue queue(){
return new Queue(QUEUE,true);
}
}
- MQSender类
@Service
public class MQSender {
@Autowired
AmqpTemplate amqpTemplate;
public void send(Object message){
//将Object转为String
String msg= RedisService.beanToString(message);
amqpTemplate.convertAndSend(MQConfig.QUEUE,msg);
}
- MQReicever 类
@Service
public class MQReicever {
@RabbitListener(queues = MQConfig.QUEUE)
public void receive(String message){
System.out.println(message);
}
}
- 测试
@Controller
public class TestController {
@Autowired
MQSender mqSender;
@RequestMapping("/derect")
@ResponseBody
public String derect(){
mqSender.send("hello world");
return "ok";
}
}
2、 Topic模式
根据路由的字符串匹配
- MQConfig类
@Configuration
public class MQConfig {
public static final String TOPIC_QUEUE1="topic.queue1";
public static final String TOPIC_QUEUE2="topic.queue2";
public static final String TOPIC_EXCHANGE="topicExchange";
//两个队列
@Bean
public Queue topicQueue1(){
return new Queue(TOPIC_QUEUE1,true);
}
@Bean
public Queue topicQueue2(){
return new Queue(TOPIC_QUEUE2,true);
}
//一个交换机
@Bean
public TopicExchange topicExchange(){
return new TopicExchange(TOPIC_EXCHANGE);
}
//路由绑定
@Bean
public Binding topicBinding1(){
return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.key1");
}
@Bean
public Binding topicBinding2(){
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
}
}
- MQSender类
@Service
public class MQSender {
@Autowired
AmqpTemplate amqpTemplate;
public void sendTopic(Object message){
String msg= RedisService.beanToString(message);
//路由到topic1,topic2
amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",msg);
//路由到topic2
amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key2",msg);
}
}
- MQReicever 类
@Service
public class MQReicever {
@RabbitListener(queues = MQConfig.TOPIC_QUEUE1)
public void receiveTopic1(String message){
System.out.println(message);
}
@RabbitListener(queues = MQConfig.TOPIC_QUEUE2)
public void receiveTopic2(String message){
System.out.println(message);
}
根据路由的设置:
TOPIC_QUEUE1 只接收两次
TOPIC_QUEUE2 只接收一次
3、 Topic模式
- MQConfig类
@Configuration
public class MQConfig {
public static final String FANOUT_QUEUE1="fanout.queue1";
public static final String FANOUT_QUEUE2="fanout.queue2";
public static final String FANOUT_EXCHANGE="fanoutExchange";
@Bean
public Queue fanoutQueue1(){
return new Queue(FANOUT_QUEUE1,true);
}
@Bean
public Queue fanoutQueue2(){
return new Queue(FANOUT_QUEUE2,true);
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange(FANOUT_EXCHANGE);
}
@Bean
public Binding fanoutBinding1(){
return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
}
@Bean
public Binding fanoutBinding2(){
return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
}
}
- MQSender类
@Service
public class MQSender {
@Autowired
AmqpTemplate amqpTemplate;
public void sendFanout(Object message){
String msg= RedisService.beanToString(message);
amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,msg);
amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,msg);
}
}
- MQReicever 类
@Service
public class MQReicever {
@RabbitListener(queues = MQConfig.FANOUT_QUEUE1)
public void receiveTopic1(String message){
System.out.println(message);
}
@RabbitListener(queues = MQConfig.FANOUT_QUEUE2)
public void receiveTopic2(String message){
System.out.println(message);
}
}
4、Headers模式
- MQConfig类
@Configuration
public class MQConfig {
public static final String HEADERS_QUEUE="headersqueue";
public static final String HEADERS_EXCHANGE="headersExchange";
/**
* header模式:header匹配
*/
@Bean
public Queue headersQueue1(){
return new Queue(HEADERS_QUEUE,true);
}
@Bean
public HeadersExchange headersExchange(){
return new HeadersExchange(HEADERS_EXCHANGE);
}
@Bean
public Binding headersBinding(){
Map<String,Object> map=new HashMap<String, Object>();
map.put("header1","value1");
map.put("header2","value2");
return BindingBuilder.bind(headersQueue1()).to(headersExchange()).whereAll(map).match();
}
}
- MQSender类
@Service
public class MQSender {
@Autowired
AmqpTemplate amqpTemplate;
public void sendHeaders(Object message){
String msg= RedisService.beanToString(message);
MessageProperties properties=new MessageProperties();
properties.setHeader("header1","value1");
properties.setHeader("header2","value2");
Message obj=new Message(msg.getBytes(),properties);
amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,"",obj);
}
}
- MQReicever 类
@Service
public class MQReicever {
@RabbitListener(queues = MQConfig.HEADERS_QUEUE)
public void receiveHeaders(byte[] message){
System.out.println(new String(message));
}
}
4、如何选择消息队列
Kafka | RocketMQ | RabbitMQ | |
单机吞吐量 | 十万级 | 十万级 | 万级 |
开发语言 | Java & Scala | Java | Erlang |
消息延迟 | 毫秒级 | 毫秒级 | 微秒级 |
消息丢失 | 参数优化配置后可做到0丢失 | 参数优化配置后可做到0丢失 | 有较低的概率丢失 |
消费模式 | Pull | Pull+Push | Pull+Push |
topic数量对吞吐量的影响 | topic达到几十,几百个时,吞吐量会大幅度下降 | topic达到几百,几千个时,吞吐量会有较小幅度的下降 | \ |
可用性 | 非常高(分布式) | 非常高(主从) | 高(主从) |
总结 | 吞吐量高,微秒级延时,分布式高可用,最好是支持较少topic数量,会有消息重复现象 | 可支撑大规模topic数量,方便二次开发和扩展 | 不支持集群动态扩容,扩展和二次开发难 |
- 如果消息队列不是将要构建系统的重点,对消息队列功能和性能没有很高的要求,只需要一个快速上手易于维护的消息队列,建议使用 RabbitMQ。
- 如果系统使用消息队列主要场景是处理在线业务,比如在交易系统中用消息队列传递订单,需要低延迟和高稳定性,建议使用 RocketMQ。
- 如果需要处理海量的消息,像收集日志、监控信息或是埋点这类数据,或是你的应用场景大量使用了大数据、流计算相关的开源产品,那 Kafka 是最适合的消息队列。
5、RabbitMQ实现邮件发送
5.1 作为消息队列注意点
- 消息发送确认机制
- 消费确认机制
- 消息的重新投递
- 消费幂等性, 等等
5.2 实现思路
1、简略介绍qq邮箱授权码的获取
2、编写发送邮件工具类
3、编写RabbitMQ配置文件
4、生产者发起调用
5、消费者发送邮件
6、定时任务定时拉取投递失败的消息, 重新投递
5.3 案例demo
5.3.1 获取邮箱授权码
5.3.2 pom依赖
<!--mq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
5.3.3 Rabbitmq与邮箱的配置
# rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
# 开启confirms回调 P -> Exchange
spring.rabbitmq.publisher-confirms=true
# 开启returnedMessage回调 Exchange -> Queue
spring.rabbitmq.publisher-returns=true
# 设置手动确认(ack) Queue -> C
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.prefetch=100
# mail
spring.mail.host=smtp.qq.com
spring.mail.username=18621142249@qq.com
spring.mail.password=***
spring.mail.from=18621142249@qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
5.3.4 消息表结构
CREATE TABLE `msg_log` (
`msg_id` varchar(255) NOT NULL DEFAULT '' COMMENT '消息唯一标识',
`msg` text COMMENT '消息体, json格式化',
`exchange` varchar(255) NOT NULL DEFAULT '' COMMENT '交换机',
`routing_key` varchar(255) NOT NULL DEFAULT '' COMMENT '路由键',
`status` int(11) NOT NULL DEFAULT '0' COMMENT '状态: 0投递中 1投递成功 2投递失败 3已消费',
`try_count` int(11) NOT NULL DEFAULT '0' COMMENT '重试次数',
`next_try_time` datetime DEFAULT NULL COMMENT '下一次重试时间',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`msg_id`),
UNIQUE KEY `unq_msg_id` (`msg_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息投递日志';
5.3.5 封装MailUtil
@Component
@Slf4j
public class MailUtil {
@Value("${spring.mail.from}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 发送简单邮件
*
* @param mail
*/
public boolean send(Mail mail) {
String to = mail.getTo();// 目标邮箱
String title = mail.getTitle();// 邮件标题
String content = mail.getContent();// 邮件正文
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(title);
message.setText(content);
try {
mailSender.send(message);
log.info("邮件发送成功");
return true;
} catch (MailException e) {
log.error("邮件发送失败, to: {}, title: {}", to, title, e);
return false;
}
}
}
5.3.6 RabbitConfig
@Configuration
@Slf4j
public class RabbitConfig {
@Autowired
private CachingConnectionFactory connectionFactory;
@Autowired
private MsgLogService msgLogService;
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(converter());
// 消息是否成功发送到Exchange
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
log.info("消息成功发送到Exchange");
String msgId = correlationData.getId();
msgLogService.updateStatus(msgId, Constant.MsgLogStatus.DELIVER_SUCCESS);
} else {
log.info("消息发送到Exchange失败, {}, cause: {}", correlationData, cause);
}
});
// 触发setReturnCallback回调必须设置mandatory=true, 否则Exchange没有找到Queue就会丢弃掉消息, 而不会触发回调
rabbitTemplate.setMandatory(true);
// 消息是否从Exchange路由到Queue, 注意: 这是一个失败回调, 只有消息从Exchange路由到Queue失败才会回调这个方法
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
log.info("消息从Exchange路由到Queue失败: exchange: {}, route: {}, replyCode: {}, replyText: {}, message: {}", exchange, routingKey, replyCode, replyText, message);
});
return rabbitTemplate;
}
@Bean
public Jackson2JsonMessageConverter converter() {
return new Jackson2JsonMessageConverter();
}
// 发送邮件
public static final String MAIL_QUEUE_NAME = "mail.queue";
public static final String MAIL_EXCHANGE_NAME = "mail.exchange";
public static final String MAIL_ROUTING_KEY_NAME = "mail.routing.key";
@Bean
public Queue mailQueue() {
return new Queue(MAIL_QUEUE_NAME, true);
}
@Bean
public DirectExchange mailExchange() {
return new DirectExchange(MAIL_EXCHANGE_NAME, true, false);
}
@Bean
public Binding mailBinding() {
return BindingBuilder.bind(mailQueue()).to(mailExchange()).with(MAIL_ROUTING_KEY_NAME);
}
}
5.3.7 生产消息并放入队列
@Service
public class TestServiceImpl implements TestService {
@Autowired
private MsgLogMapper msgLogMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public ServerResponse send(Mail mail) {
String msgId = RandomUtil.UUID32();
mail.setMsgId(msgId);
MsgLog msgLog = new MsgLog(msgId, mail, RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME);
msgLogMapper.insert(msgLog);// 消息入库
CorrelationData correlationData = new CorrelationData(msgId);
rabbitTemplate.convertAndSend(RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME, MessageHelper.objToMsg(mail), correlationData);// 发送消息
return ServerResponse.success(ResponseCode.MAIL_SEND_SUCCESS.getMsg());
}
}
5.3.8 消费消息并发送邮件
@Component
@Slf4j
public class MailConsumer {
@Autowired
private MsgLogService msgLogService;
@Autowired
private MailUtil mailUtil;
@RabbitListener(queues = RabbitConfig.MAIL_QUEUE_NAME)
public void consume(Message message, Channel channel) throws IOException {
Mail mail = MessageHelper.msgToObj(message, Mail.class);
log.info("收到消息: {}", mail.toString());
String msgId = mail.getMsgId();
MsgLog msgLog = msgLogService.selectByMsgId(msgId);
if (null == msgLog || msgLog.getStatus().equals(Constant.MsgLogStatus.CONSUMED_SUCCESS)) {// 消费幂等性
log.info("重复消费, msgId: {}", msgId);
return;
}
MessageProperties properties = message.getMessageProperties();
long tag = properties.getDeliveryTag();
boolean success = mailUtil.send(mail);
if (success) {
msgLogService.updateStatus(msgId, Constant.MsgLogStatus.CONSUMED_SUCCESS);
channel.basicAck(tag, false);// 消费确认
} else {
channel.basicNack(tag, false, true);
}
}
}
5.3.9 投递失败消息处理策略
定时任务重新投递发送失败的消息
@Component
@Slf4j
public class ResendMsg {
@Autowired
private MsgLogService msgLogService;
@Autowired
private RabbitTemplate rabbitTemplate;
// 最大投递次数
private static final int MAX_TRY_COUNT = 3;
/**
* 每30s拉取投递失败的消息, 重新投递
*/
@Scheduled(cron = "0/30 * * * * ?")
public void resend() {
log.info("开始执行定时任务(重新投递消息)");
List<MsgLog> msgLogs = msgLogService.selectTimeoutMsg();
msgLogs.forEach(msgLog -> {
String msgId = msgLog.getMsgId();
if (msgLog.getTryCount() >= MAX_TRY_COUNT) {
msgLogService.updateStatus(msgId, Constant.MsgLogStatus.DELIVER_FAIL);
log.info("超过最大重试次数, 消息投递失败, msgId: {}", msgId);
} else {
msgLogService.updateTryCount(msgId, msgLog.getNextTryTime());// 投递次数+1
CorrelationData correlationData = new CorrelationData(msgId);
rabbitTemplate.convertAndSend(msgLog.getExchange(), msgLog.getRoutingKey(), MessageHelper.objToMsg(msgLog.getMsg()), correlationData);// 重新投递
log.info("第 " + (msgLog.getTryCount() + 1) + " 次重新投递消息");
}
});
log.info("定时任务执行结束(重新投递消息)");
}
}