五、Rabbitmq消息可靠性投递实践

思路:将投递失败的消息存入数据库,利用定时任务去定时重试发送消息,重试三次,如果三次都无法投递的话,这时候需要人工干预,去处理消息为什么没有能够投递成功

1. 基础配置

SpringBoot版本:2.0.3

  1. sql
create table if not exists rabbit.msg_broker
(
	id int auto_increment primary key,
	retry tinyint default '0' not null comment '尝试投递次数',
	status tinyint default '1' not null comment '此时的消息的状态',
	reason varchar(1000) default '' not null comment '失败原因',
	create_time timestamp default CURRENT_TIMESTAMP not null,
	update_time timestamp default CURRENT_TIMESTAMP not null,
	corr_id varchar(64) default '' not null comment '消息唯一标识',
	message varchar(10000) default '' not null comment '消息体',
	rounting_key varchar(20) default '' not null comment '路由键',
	constraint msg_broker_corr_id_uindex unique (corr_id)
)comment 'rabbit消息信息';

实体类

package com.itcloud.entity;

import java.util.Date;

public class MsgBroker {
   
	//主键
	private Integer id;
	
	//消息唯一标识
	private String corrId;
	
	//消息尝试发送次数
	private Integer retry;
	
	//消息状态
	private Integer status;
	
	//消息发送失败原因
	private String reason;
	
	//创建时间
	private Date createTime;
	
	//更新时间
	private Date updateTime;
	
	//消息体
	private String message;
	
	//消息路由键
	private String rountingKey;
	
}

  1. RabbitApplication.java

这里添加注解:@EnableScheduling开启springBoot定时任务功能

/**
 * rabbbit 消息可靠性投递最佳实践
 * @author ITCloud
 */
@SpringBootApplication
@EnableScheduling
public class RabbitApplication {
   
	public static void main(String[] args) {
   
		SpringApplication.run(RabbitApplication.class, args);
	}
}
  1. application.yml
server:
  port: 8077
spring:
  rabbitmq:
    addresses: 192.168.186.130
    port: 5672
    username: itcloud
    password: itcloud
    virtual-host: hmall
    listener:
      direct:
        acknowledge-mode: manual # 设置手动签收
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/rabbit?useSSL=false
    username: root
    password: HelloJava0903!
  1. RabbitConfig

常量配置类

package com.itcloud.config;
public interface RabbitConfig {
   
	//其中一个交换机名称
	public static final String EXCHANGE_DIRECT = "hmex.direct";
	
	public static final int SENDING_STATUS = 0; //消息正在重试发送
	public static final int SUCCESS_STATUS = 1; // 消息发送成功
	public static final int FAIL_STATUS = 2; // 消息发送失败
	
	public static final int NOT_FOUNT = -1; //数据不存在
	
	public static final int FIRST_RETRY = 1; //第一次重试
	public static 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值