成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Springboot自定義重試注解@Retryable

開發 架構
新增重試任務成功之后,我們可通過調度平臺(比如:xxlJob),定時查詢重試任務表,然后調用RetryTaskDefinitionEnum中定義的重試的Service(retryServiceName),這里可以定義一個模板方法,根據RetryServiceName,從spring中獲取到對應的Bean,執行具體的業務方法,然后更新任務狀態和重試次數即可。

一、概述

微服務之間相互調用,難免會出現形形色色的異常,出現異常時有些情況可能需要先落重試任務表,然后通過任務調度等進行定時重試;通過自定義重試注解@Retryable,減少對核心業務代碼入侵,增強代碼可讀性、可維護性。下面通過實戰,開發自定義重試注解@Retryable。諸位可根據業務需要,稍作改造直接使用;如果有疑問、或者好的想法,歡迎留言,經驗共享。

二、實戰

重試任務表定義(retry_task):

CREATE TABLE `retry_task` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵值',
  `business_type_code` varchar(32) COLLATE NOT NULL DEFAULT '' COMMENT '業務類型編碼',
  `business_type_desc` varchar(100) COLLATE NOT NULL DEFAULT '' COMMENT '業務類型描述',
  `retry_service_name` varchar(100) COLLATE NOT NULL DEFAULT '' COMMENT '重試的service名稱',
  `business_param` text COLLATE NOT NULL DEFAULT '' COMMENT '業務參數',
  `wait_retry_times` int(11) NOT NULL DEFAULT 3 COMMENT '待重試次數',
  `already_retry_times` int(11) NOT NULL DEFAULT 0 COMMENT '已重試次數',
  `retry_result_code` varchar(36) COLLATE NOT NULL DEFAULT '' COMMENT '重試結果碼',
  `retry_result_msg` varchar(255) COLLATE NOT NULL DEFAULT '' COMMENT '重試結果描述',
  `create_user` varchar(36) COLLATE NOT NULL DEFAULT '' COMMENT '創建人',
  `create_time` datetime NOT NULL COMMENT '創建時間',
  `update_user` varchar(36) COLLATE NOT NULL DEFAULT '' COMMENT '更新人',
  `update_time` datetime NOT NULL COMMENT '更新時間',
  PRIMARY KEY (`id`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_business_type_code` (`business_type_code`)
) COMMENT='重試任務表';

重試任務表實體類(RetryTaskEntity):

@Data
public class RetryTaskEntity implements Serializable {

    private static final long serialVersionUID = -1950778520234119369L;

    /**
     * 主鍵值
     */
    private BigInteger id;

    /**
     * 業務類型編碼
     */
    private String businessTypeCode;

    /**
     * 業務類型描述
     */
    private String businessTypeDesc;

    /**
     * 重試的service名稱
     */
    private String retryServiceName;

    /**
     * 業務參數
     */
    private String businessParam;

    /**
     * 待重試的次數
     */
    private Integer waitRetryTimes;

    /**
     * 已重試的次數
     */
    private Integer alreadyRetryTimes;

    /**
     * 重試結果碼
     */
    private String retryResultCode;

    /**
     * 重試結果描述
     */
    private String retryResultMsg;

    /**
     * 創建人
     */
    private String createUser;

    /**
     * 創建時間
     */
    private Date createTime;

    /**
     * 更新人
     */
    private String updateUser;

    /**
     * 更新時間
     */
    private Date updateTime;
}

重試任務表mapper和對應的xml文件:

public interface RetryTaskMapper {
    int addRetryTask(RetryTaskEntity retryTaskEntity);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.boot.demo.mapper.RetryTaskMapper">

    <insert id="addRetryTask" parameterType="com.boot.demo.pojo.RetryTaskEntity">
        INSERT INTO retry_task(business_type_code,
                               business_type_desc,
                               retry_service_name,
                               business_param,
                               wait_retry_times,
                               already_retry_times,
                               retry_result_code,
                               retry_result_msg,
                               create_user,
                               create_time,
                               update_user,
                               update_time)
        VALUES (#{businessTypeCode},
                #{businessTypeDesc},
                #{retryServiceName},
                #{businessParam},
                #{waitRetryTimes},
                #{alreadyRetryTimes},
                #{retryResultCode},
                #{retryResultMsg},
                #{createUser},
                #{createTime},
                #{updateUser},
                #{updateTime})
    </insert>
</mapper>

重試任務表service和對應的serviceImpl:

public interface RetryTaskService {
    void addRetryTask(RetryTaskEntity retryTaskEntity);
}
@Service
public class RetryTaskServiceImpl implements RetryTaskService {

    @Autowired
    private RetryTaskMapper retryTaskMapper;

    @Override
    public void addRetryTask(RetryTaskEntity retryTaskEntity) {
        retryTaskMapper.addRetryTask(retryTaskEntity);
    }
}

業務類型枚舉類(RetryTaskDefinitionEnum):

/**
 * 重試任務枚舉
 */
public enum RetryTaskDefinitionEnum {

    ADD_STOCK("101", "采購入庫成功后新增庫存異常重試", "purchaseService", 3);

    /**
     * 業務類型編碼
     */
    private final String businessTypeCode;

    /**
     * 業務類型描述
     */
    private final String businessTypeDesc;

    /**
     * 重試的service名稱
     */
    private final String retryServiceName;

    /**
     * 重試次數
     */
    private final Integer retryTimes;

    RetryTaskDefinitionEnum(String businessTypeCode, String businessTypeDesc, String retryServiceName, Integer retryTimes) {
        this.businessTypeCode = businessTypeCode;
        this.businessTypeDesc = businessTypeDesc;
        this.retryServiceName = retryServiceName;
        this.retryTimes = retryTimes;
    }

    public static RetryTaskDefinitionEnum getTaskDefinitionByBusinessTypeCode(String businessTypeCode) {
        if (StringUtils.isBlank(businessTypeCode)) {
            return null;
        }
        for (RetryTaskDefinitionEnum taskDefinition : values()) {
            if (taskDefinition.getBusinessTypeCode().equals(businessTypeCode)) {
                return taskDefinition;
            }
        }
        return null;
    }

    public String getBusinessTypeCode() {
        return businessTypeCode;
    }

    public String getBusinessTypeDesc() {
        return businessTypeDesc;
    }

    public String getRetryServiceName() {
        return retryServiceName;
    }

    public Integer getRetryTimes() {
        return retryTimes;
    }
}

自定義注解(@MyRetryable):

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@Documented
public @interface MyRetryable {
    RetryTaskDefinitionEnum businessType();
}

自定義注解切面(MyRetryableAspect):

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import com.boot.demo.result.Result;
import com.boot.demo.result.ResultCode;
import com.boot.demo.pojo.RetryTaskEntity;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import com.boot.demo.annotation.MyRetryable;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.ProceedingJoinPoint;
import com.boot.demo.service.RetryTaskService;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.boot.demo.annotation.RetryTaskDefinitionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;

@Slf4j
@Aspect
@Component
public class MyRetryableAspect {

    @Autowired
    private RetryTaskService retryTaskService;

    @Pointcut("@annotation(com.boot.demo.annotation.MyRetryable)")
    public void pointCut() {
    }

    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Result result = null;
        try {
            // 執行目標方法
            result = (Result) joinPoint.proceed();
            // 目標方法返回:成功結果碼(200),則無需重試
            if (ResultCode.SUCCESS.getCode() == result.getCode()) {
                return result;
            }
            // 目標方法返回:非成功結果碼(非200)則需重試(此次可根據需要判斷什么樣的返回碼需要重試)
            dealAddRetryTask(joinPoint);,
            return result;
        } catch (Throwable e) {
            log.error("myRetryableAspectLog error param: {} result: {} e: ", joinPoint.getArgs(), result, e);
            // 此處捕獲異常之后,也可以根據需要重試,這里就僅輸出異常日志
            return result;
        }
    }

    private void dealAddRetryTask(ProceedingJoinPoint joinPoint) {
        // 獲取重試注解信息
        MyRetryable myRetryableAnnotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(MyRetryable.class);
        if (null == myRetryableAnnotation) {
            return;
        }
        // 根據業務類型編碼,獲取枚舉中定義的業務類型描述、重試的service、重試次數等信息
        String businessTypeCode = myRetryableAnnotation.businessType().getBusinessTypeCode();
        RetryTaskDefinitionEnum retryTaskDefinition = RetryTaskDefinitionEnum.getTaskDefinitionByBusinessTypeCode(businessTypeCode);
        if (null == retryTaskDefinition) {
            return;
        }
        RetryTaskEntity retryTaskEntity = new RetryTaskEntity();
        retryTaskEntity.setBusinessTypeCode(businessTypeCode);
        retryTaskEntity.setBusinessTypeDesc(retryTaskDefinition.getBusinessTypeDesc());
        retryTaskEntity.setRetryServiceName(retryTaskDefinition.getRetryServiceName());
        retryTaskEntity.setBusinessParam(JSON.toJSONString(joinPoint.getArgs()[0]));
        retryTaskEntity.setWaitRetryTimes(retryTaskDefinition.getRetryTimes());
        retryTaskEntity.setAlreadyRetryTimes(0);
        retryTaskEntity.setRetryResultCode("");
        retryTaskEntity.setRetryResultMsg("");
        retryTaskEntity.setCreateUser("SYS");
        retryTaskEntity.setCreateTime(new Date());
        retryTaskEntity.setUpdateUser("SYS");
        retryTaskEntity.setUpdateTime(new Date());
        retryTaskService.addRetryTask(retryTaskEntity);
    }
}

基礎類(Result、ResultCode、ResultGenerator)。

Result類:

public class Result {

    private int code;
    private String message;
    private Object data;

    public Result setCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Result{");
        sb.append("code=").append(code);
        sb.append(", message='").append(message).append('\'');
        sb.append(", data=").append(data);
        sb.append('}');
        return sb.toString();
    }
}

ResultCode類:

public enum ResultCode {

    SUCCESS(200),
    FAIL(400),
    UNAUTHORIZED(401),
    FORBIDDEN(403),
    NOT_FOUND(404),
    INTERNAL_SERVER_ERROR(500);

    private final int code;

    ResultCode(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}

ResultGenerator類:

public class ResultGenerator {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    private ResultGenerator() {

    }

    public static Result genSuccessResult() {
        return new Result().setCode(ResultCode.SUCCESS).setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result genSuccessResult(Object data) {
        return new Result().setCode(ResultCode.SUCCESS).setMessage(DEFAULT_SUCCESS_MESSAGE).setData(data);
    }

    public static Result genFailResult(String message) {
        return new Result().setCode(ResultCode.FAIL).setMessage(message);
    }

    public static Result genFailResult(ResultCode code, String message) {
        return new Result().setCode(code).setMessage(message);
    }

    public static Result genFailResult(String message, Object data) {
        return new Result().setCode(ResultCode.FAIL).setMessage(message).setData(data);
    }
}

測試controller(PurchaseController):

@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("/test")
    public String test(String param) {
        purchaseService.addStock(param);
        return "success";
    }
}

測試PurchaseService、和PurchaseServiceImpl

public interface PurchaseService {
    Result addStock(String param);
}
@Service("purchaseService")
public class PurchaseServiceImpl implements PurchaseService {

    @Override
    // 在需要重試的業務方法上新增重試注解即可
    @MyRetryable(businessType = RetryTaskDefinitionEnum.ADD_STOCK)
    public Result addStock(String param) {
//     return ResultGenerator.genSuccessResult();
        return ResultGenerator.genFailResult("系統異常...");
    }
}

三、總結

新增重試任務成功之后,我們可通過調度平臺(比如:xxlJob),定時查詢重試任務表,然后調用RetryTaskDefinitionEnum中定義的重試的service(retryServiceName),這里可以定義一個模板方法,根據retryServiceName,從spring中獲取到對應的bean,執行具體的業務方法,然后更新任務狀態和重試次數即可。

責任編輯:姜華 來源: 今日頭條
相關推薦

2021-02-20 11:40:35

SpringBoot占位符開發技術

2024-07-02 11:42:53

SpringRedis自定義

2024-10-09 10:46:41

springboot緩存redis

2023-10-09 07:37:01

2024-10-14 17:18:27

2023-10-24 13:48:50

自定義注解舉值驗證

2024-12-27 15:37:23

2021-12-30 12:30:01

Java注解編譯器

2021-05-06 07:47:48

TestNGListener用例失敗重試

2022-02-17 07:10:39

Nest自定義注解

2022-11-01 11:15:56

接口策略模式

2020-11-25 11:20:44

Spring注解Java

2017-08-03 17:00:54

Springmvc任務執行器

2023-07-03 08:29:11

BannerSpringBoot

2023-03-30 16:16:00

Java自定義注解開發

2015-02-12 15:33:43

微信SDK

2020-09-04 13:30:43

Java自定義代碼

2015-02-12 15:38:26

微信SDK

2021-09-26 05:02:00

緩存Ehcache用法

2021-03-16 10:39:29

SpringBoot參數解析器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线日韩不卡 | 精品美女 | 久久99网 | 97精品久久 | 亚洲国产精品久久久久久 | 91在线看片 | 黄色精品| 亚洲免费观看 | 九九精品久久久 | 激情久久久久 | 综合久久综合久久 | 欧美一级片在线观看 | 欧美专区在线视频 | 国产精品无码久久久久 | 国产精品国产自产拍高清 | 中文字幕一级毛片视频 | 久久噜噜噜精品国产亚洲综合 | 日韩精品在线一区 | 国产在线中文字幕 | 天堂网色 | 婷婷午夜天 | 影音先锋男 | 逼逼网| 99精品欧美一区二区三区综合在线 | 免费精品一区 | 91精品国产综合久久久动漫日韩 | 精品久| 国产精品一区二区视频 | 国产亚洲一区二区三区 | 国家一级黄色片 | 99热最新网址 | 亚洲精品福利视频 | 一区二区三区高清 | 亚洲精品短视频 | 久久99精品国产麻豆婷婷 | 中文字幕在线观看第一页 | 日本人麻豆 | 午夜免费视频 | 99re热这里只有精品视频 | 亚洲福利 | 亚洲欧美日韩精品久久亚洲区 |