spring boot实现retry机制

  spring boot 实现retry机制我主要是为了写回调来使用的较少错误几率  废话不多说 上代码

首先pom.xml

        
        
            org.springframework.retry
            spring-retry
        
        

启动类启动加载

@EnableRetry//开启重试机制

service业务

package com.test.core.service;

import com.test.core.excption.NotifyErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Jiang
 * @date 2019/8/9 15:12
 * 测试重试机制
 */
@Slf4j
@Service
public class ReTryService {

    /**
     * 重试操作
     * delay * multiplier 下一次操作时间
     * maxAttempts 操作次数
     */
    @Retryable(value = NotifyErrorException.class, maxAttempts = 5, backoff = @Backoff(delay = 1000L, multiplier = 5))
    public void testReyTry(){
        System.out.println(new Date().toString());
        throw  new NotifyErrorException("测试抛出异常","1");
    }


    /**
     * 兜底操作
     * @param e
     */
    @Recover
    public void recover(NotifyErrorException e) {
        System.out.println("失败");
    }
}

OK 调用测试

开始插入日志
Fri Aug 09 15:44:13 CST 2019
Fri Aug 09 15:44:14 CST 2019
Fri Aug 09 15:44:19 CST 2019
Fri Aug 09 15:44:44 CST 2019
Fri Aug 09 15:45:14 CST 2019
失败

 

你可能感兴趣的:(随笔)