Springboot 中接口服务重试机制

在平时开发中可能在调用服务时会遇到调用失败的情况,在springboot 中retery 机制可以很好的满足我们的开发场景,下面举个简单的例子模拟第三方调用。

package com.szhome.web.action;

import com.szhome.web.service.ThirdApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author caizl
 * @Description TODO
 * @Date 2024/01/10/8:43
 * @Version 1.0
 */
@Controller
@RequestMapping("/api")
public class ThirdApiAction {
    @Autowired
    private ThirdApiService thirdApiService;
    @RequestMapping("/third")
    @ResponseBody
    public Map<String,String> callThirdApi(@RequestParam String id)throws Exception{
        System.out.println("开始调用第三方接口");
        thirdApiService.callThirdApi(id);
        Map<String,String> map = new HashMap<>();
        map.put("code","ok");
        map.put("msg","执行成功");
        return map;
    }

}

package com.szhome.web.service;

import com.google.gson.Gson;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author caizl
 * @Description TODO
 * @Date 2024/01/10/8:44
 * @Version 1.0
 */
@Service
public class ThirdApiService {
   /**
 * value:抛出指定异常才会重试
 * include:和value一样,默认为空,当exclude也为空时,默认所有异常
 * exclude:指定不处理的异常
 * maxAttempts:最大重试次数,默认3次
 * backoff:重试等待策略,
 * 默认使用@Backoff,@Backoff的value默认为1000L,我们设置为2000; 以毫秒为单位的延迟(默认 1000)
 * multiplier(指定延迟倍数)默认为0,表示固定暂停1秒后进行重试,如果把multiplier设置为1.5,则第一次重试为2秒,第二次为3秒,第三次为4.5秒。
 * @return
 */
    @Retryable(
            value = { RestClientException.class },//异常时进行重试
            maxAttempts = 3,//最大重试次数
            backoff = @Backoff(delay = 1000, multiplier = 2)
            //backoff指定了重试间隔的初始延迟和延迟倍数。
    )
    public String callThirdApi(String id)throws RestClientException{
        System.out.println("第三方服务类。。。。start。。。。");
        Map<String,String> map = new HashMap<>();
        Gson gson = new Gson();
        try{
            map.put("code","1");
            throw  new RestClientException("测试带三方重试接口"+id);
        }catch (Exception e){
            map.put("code","0");
            throw e;
        }/*finally {
            return gson.toJson(map);
        }*/

    }
    @Recover
    public String lastProcess(String id){
        System.out.println("第三方服务失败后最终执行业务,模拟记录日志,id=" + id);
        return "ok";
    }
}

使用起来很简单,只需要在引入相关jar,并且在启动的时候进行开启,这是springboot 的老套路,在我们服务层进行 @Retryable 的配置,在重试机制完成后我们可以配置一个兜底服务@Recover,我们可以接收请求参数,以此我们后续还可以进行补偿服务的延伸扩展使我们的服务更加的灵活健硕。

以下重试机制的参数说明及相关类使用:

@Backoff
重试回退策略(立即重试还是等待一会再重试)
value:重试延迟时间,单位毫秒,默认值1000,即默认延迟1秒。当未设置multiplier时,表示每隔value的时间重试,直到重试次数到达maxAttempts设置的最大允许重试次数。当设置了multiplier参数时,该值作为幂运算的初始值。
delay:等同value参数,两个参数设置一个即可。
maxDelay:两次重试间最大间隔时间。当设置multiplier参数后,下次延迟时间根据是上次延迟时间乘以 multiplier得出的,这会导致两次重试间的延迟时间越来越长,该参数限制两次重试的最大间隔时间,当间隔时间大于该值时,计算出的间隔时间将会被忽略,使用上次的重试间隔时间。
multiplier:作为乘数用于计算下次延迟时间。公式:delay = delay * multiplier
random:是否启用随机退避策略,默认false。设置为true时启用退避策略,重试延迟时间将是delay和maxDelay间的一个随机数。设置该参数的目的是重试的时候避免同时发起重试请求,造成Ddos攻击。
@CircuitBreaker
include: 指定处理的异常类。默认为空
exclude: 指定不需要处理的异常。默认为空
vaue: 指定要重试的异常。默认为空
maxAttempts: 最大重试次数。默认3次
openTimeout: 配置熔断器打开的超时时间,默认5s,当超过openTimeout之后熔断器电路变成半打开状态(只要有一次重试成功,则闭合电路)
resetTimeout: 配置熔断器重新闭合的超时时间,默认20s,超过这个时间断路器关闭

@Service
class BusinessService {

    @Recover
    public int fallback(BoomException ex) {
        return 2;
    }

    @CircuitBreaker(include = Exception.class, openTimeout = 20000L, resetTimeout = 5000L, maxAttempts = 1)
    public int desireNumber() throws Exception {
        System.out.println("calling desireNumber()");
        if (Math.random() > .5) {
            throw new Exception("error");
        }
        return 1;
    }
}

RetryTemplate
什么时候使用RetryTemplate?

不使用spring容器的时候,使用了@Retryable,@CircuitBreaker的方法不能在本类被调用,不然重试机制不会生效。也就是要标记为@Service,然后在其它类使用@Autowired注入或者@Bean去实例才能生效。
需要使用复杂策略机制和异常场景时
使用有状态重试,且需要全局模式时建议使用
需要使用监听器Listener的场景
需要使用Retry统计分析
RetryPolicy 重试策略
NeverRetryPolicy:只允许调用RetryCallback一次,不允许重试;
AlwaysRetryPolicy:允许无限重试,直到成功,此方式逻辑不当会导致死循环;
SimpleRetryPolicy:固定次数重试策略,默认重试最大次数为3次,RetryTemplate默认使用的策略;
TimeoutRetryPolicy:超时时间重试策略,默认超时时间为1秒,在指定的超时时间内允许重试;
CircuitBreakerRetryPolicy:有熔断功能的重试策略,需设置3个参数openTimeout、resetTimeout和delegate,稍后详细介绍该策略;
CompositeRetryPolicy:组合重试策略,有两种组合方式,乐观组合重试策略是指只要有一个策略允许重试即可以,悲观组合重试策略是指只要有一个策略不允许重试即可以,但不管哪种组合方式,组合中的每一个策略都会执行。
BackOffPolicy 退避策略
NoBackOffPolicy:无退避算法策略,即当重试时是立即重试;
FixedBackOffPolicy:固定时间的退避策略,需设置参数sleeper和backOffPeriod,sleeper指定等待策略,默认是Thread.sleep,即线程休眠,backOffPeriod指定休眠时间,默认1秒;
UniformRandomBackOffPolicy:随机时间退避策略,需设置sleeper、minBackOffPeriod和maxBackOffPeriod,该策略在[minBackOffPeriod,maxBackOffPeriod之间取一个随机休眠时间,minBackOffPeriod默认500毫秒,maxBackOffPeriod默认1500毫秒;
ExponentialBackOffPolicy:指数退避策略,需设置参数sleeper、initialInterval、maxInterval和multiplier,initialInterval指定初始休眠时间,默认100毫秒,maxInterval指定最大休眠时间,默认30秒,multiplier指定乘数,即下一次休眠时间为当前休眠时间*multiplier;
ExponentialRandomBackOffPolicy:随机指数退避策略,引入随机乘数,之前说过固定乘数可能会引起很多服务同时重试导致DDos,使用随机休眠时间来避免这种情况。
DEMO RetryTemplate
TimeoutRetryPolicy
TimeoutRetryPolicy策略,TimeoutRetryPolicy超时时间默认是1秒。
TimeoutRetryPolicy超时是指在execute方法内部,从open操作开始到调用TimeoutRetryPolicy的canRetry方法这之间所经过的时间。
这段时间未超过TimeoutRetryPolicy定义的超时时间,那么执行操作,否则抛出异常。
当重试执行完闭,操作还未成为,那么可以通过RecoveryCallback完成一些失败事后处理。
public class RetryTemplate01 {
    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();
        TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
        template.setRetryPolicy(policy);
        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                return "Retry";
            }
        });
        System.out.println(result);
    }
}
SimpleRetryPolicy
代码重试两次后,仍然失败,RecoveryCallback被调用,返回”recovery callback”。如果没有定义RecoveryCallback,那么重试2次后,将会抛出异常。

public class RetryTemplate02 {

    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();
        SimpleRetryPolicy policy = new SimpleRetryPolicy();
        policy.setMaxAttempts(2);
        template.setRetryPolicy(policy);
        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                throw new NullPointerException("nullPointerException");
            }
        }, new RecoveryCallback<String>() {
            public String recover(RetryContext context) throws Exception {
                return "recovery callback";
            }
        });
        System.out.println(result);
    }
}
该策略定义了对指定的异常进行若干次重试。默认情况下,对Exception异常及其子类重试3次。 如果创建SimpleRetryPolicy并指定重试异常map,可以选择性重试或不进行重试。下面的代码定义了对TimeOutException进行重试。

public class RetryTemplate03 {

    public static void main(String[] args) throws Exception {
        
        RetryTemplate template = new RetryTemplate();
        Map<Class<? extends Throwable>, Boolean> maps = new HashMap<Class<? extends Throwable>, Boolean>();
        maps.put(TimeoutException.class, true);
        SimpleRetryPolicy policy2 = new SimpleRetryPolicy(2, maps);
        template.setRetryPolicy(policy2);

        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                throw new TimeoutException("TimeoutException");
            }
        }, new RecoveryCallback<String>() {
            public String recover(RetryContext context) throws Exception {
                return "recovery callback";
            }
        });
        System.out.println(result);
    }

}
ExceptionClassifierRetryPolicy
通过PolicyMap定义异常及其重试策略。下面的代码在抛出NullPointerException采用NeverRetryPolicy策略,而TimeoutException采用AlwaysRetryPolicy。

public class RetryTemplate04 {

    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();
        ExceptionClassifierRetryPolicy policy = new ExceptionClassifierRetryPolicy();
        Map<Class<? extends Throwable>, RetryPolicy> policyMap = new HashMap<Class<? extends Throwable>, RetryPolicy>();
        policyMap.put(TimeoutException.class, new AlwaysRetryPolicy());
        policyMap.put(NullPointerException.class, new NeverRetryPolicy());
        policy.setPolicyMap(policyMap);
        template.setRetryPolicy(policy);
        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                if (arg0.getRetryCount() >= 2) {
                    Thread.sleep(1000);
                    throw new NullPointerException();
                }
                throw new TimeoutException("TimeoutException");
            }
        }, new RecoveryCallback<String>() {
            public String recover(RetryContext context) throws Exception {
                return "recovery callback";
            }
        });
        System.out.println(result);
    }

}
CompositeRetryPolicy
用户指定一组策略,随后根据optimistic选项来确认如何重试。
下面的代码中创建CompositeRetryPolicy策略,并创建了RetryPolicy数组,数组有两个具体策略SimpleRetryPolicy与AlwaysRetryPolicy。
当CompositeRetryPolicy设置optimistic为true时,Spring-retry会顺序遍历RetryPolicy[]数组,如果有一个重试策略可重试,例如SimpleRetryPolicy没有达到重试次数,那么就会进行重试。
如果optimistic选项设置为false。那么有一个重试策略无法重试,那么就不进行重试。
例如SimpleRetryPolicy达到重试次数不能再重试,而AlwaysRetryPolicy可以重试,那么最终是无法重试的。
下面代码设置setOptimistic(true),而AlwaysRetryPolicy一直可重试,那么最终可以不断进行重试。
public class RetryTemplate05 {

    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();

        CompositeRetryPolicy policy = new CompositeRetryPolicy();
        RetryPolicy[] polices = { new SimpleRetryPolicy(), new AlwaysRetryPolicy() };

        policy.setPolicies(polices);
        policy.setOptimistic(true);
        template.setRetryPolicy(policy);

        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                if (arg0.getRetryCount() >= 2) {
                    Thread.sleep(1000);
                    throw new NullPointerException();

                }
                throw new TimeoutException("TimeoutException");
            }
        }, new RecoveryCallback<String>() {
            public String recover(RetryContext context) throws Exception {
                return "recovery callback";
            }
        });
        System.out.println(result);
    }

}
ExponentialRandomBackOffPolicy
通过监听器,可以在重试操作的某些位置嵌入调用者定义的一些操作,以便在某些场景触发。
代码注册了两个Listener,Listener中的三个实现方法,onError,open,close会在执行重试操作时被调用,
在RetryTemplate中doOpenInterceptors,doCloseInterceptors,doOnErrorInterceptors会调用监听器对应的open,close,onError方法。
doOpenInterceptors方法在第一次重试之前会被调用,如果该方法返回true,则会继续向下直接,如果返回false,则抛出异常,停止重试。
doCloseInterceptors 会在重试操作执行完毕后调用。
doOnErrorInterceptors 在抛出异常后执行,
当注册多个Listener时,open方法按会按Listener的注册顺序调用,而onError和close则按Listener注册的顺序逆序调用。
public class RetryTemplate06 {

    public static void main(String[] args) throws Exception {
        
        RetryTemplate template = new RetryTemplate();

        ExponentialRandomBackOffPolicy exponentialBackOffPolicy = new ExponentialRandomBackOffPolicy();
        exponentialBackOffPolicy.setInitialInterval(1500);
        exponentialBackOffPolicy.setMultiplier(2);
        exponentialBackOffPolicy.setMaxInterval(6000);

        CompositeRetryPolicy policy = new CompositeRetryPolicy();
        RetryPolicy[] polices = { new SimpleRetryPolicy(), new AlwaysRetryPolicy() };

        policy.setPolicies(polices);
        policy.setOptimistic(true);

        template.setRetryPolicy(policy);
        template.setBackOffPolicy(exponentialBackOffPolicy);

        template.registerListener(new RetryListener() {
            public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
                System.out.println("open");
                return true;
            }
            public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
                    Throwable throwable) {
                System.out.println("onError");
            }
            public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
                    Throwable throwable) {
                System.out.println("close");
            }
        });

        template.registerListener(new RetryListener() {
            public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
                System.out.println("open2");
                return true;
            }
            public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
                    Throwable throwable) {
                System.out.println("onError2");
            }
            public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback,
                    Throwable throwable) {
                System.out.println("close2");
            }
        });
        String result = template.execute(new RetryCallback<String, Exception>() {
            public String doWithRetry(RetryContext arg0) throws Exception {
                arg0.getAttribute("");
                if (arg0.getRetryCount() >= 2) {
                    throw new NullPointerException();
                }
                throw new TimeoutException("TimeoutException");
            }
        });
        System.out.println(result);
    }

}
有状态RetryTemplate
当把状态放入缓存时,通过该key查询获取,全局模式 DataAccessException进行回滚

public class RetryTemplate07 {
    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();
        Object key = "mykey";
        boolean isForceRefresh = true;
        BinaryExceptionClassifier rollbackClassifier = new BinaryExceptionClassifier(
                Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
        RetryState state = new DefaultRetryState(key, isForceRefresh, rollbackClassifier);
        String result = template.execute(new RetryCallback<String, RuntimeException>() {
            @Override
            public String doWithRetry(RetryContext context) throws RuntimeException {
                System.out.println("retry count:" + context.getRetryCount());
                throw new TypeMismatchDataAccessException();
            }
        }, new RecoveryCallback<String>() {
            @Override
            public String recover(RetryContext context) throws Exception {
                return "default";
            }
        }, state);
        System.out.println(result);
    }
}
熔断器场景。在有状态重试时,且是全局模式,不在当前循环中处理重试,而是全局重试模式(不是线程上下文),如熔断器策略时测试代码如下所示:

public class RetryTemplate08 {

    public static void main(String[] args) throws Exception {
        RetryTemplate template = new RetryTemplate();
        CircuitBreakerRetryPolicy retryPolicy = new CircuitBreakerRetryPolicy(new SimpleRetryPolicy(3));
        retryPolicy.setOpenTimeout(5000);
        retryPolicy.setResetTimeout(20000);
        template.setRetryPolicy(retryPolicy);
        for (int i = 0; i < 10; i++) {
            try {
                Object key = "circuit";
                boolean isForceRefresh = false;
                RetryState state = new DefaultRetryState(key, isForceRefresh);
                String result = template.execute(new RetryCallback<String, RuntimeException>() {
                    @Override
                    public String doWithRetry(RetryContext context) throws RuntimeException {
                        System.out.println("retry count:" + context.getRetryCount());
                        throw new RuntimeException("timeout");
                    }
                }, new RecoveryCallback<String>() {
                    @Override
                    public String recover(RetryContext context) throws Exception {
                        return "default";
                    }
                }, state);
                System.out.println(result);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

}
XML Configuration
xml配置可以在不修改原来代码的情况下通过,添加spring retry的功能。

@SpringBootApplication
@EnableRetry
@EnableAspectJAutoProxy
@ImportResource("classpath:/retryadvice.xml")
public class XmlApplication {
    public static void main(String[] args) {
        SpringApplication.run(XmlApplication.class, args);
    }
}
public class XmlRetryService {
    public void xmlRetryService(String arg01) throws Exception {
        System.out.println("xmlRetryService do something...");
        throw new RemoteAccessException("RemoteAccessException....");
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    <aop:config>
        <aop:pointcut id="transactional" expression="execution(*XmlRetryService.xmlRetryService(..))" />
        <aop:advisor pointcut-ref="transactional" advice-ref="taskRetryAdvice" order="-1" />
    </aop:config>
    <bean id="taskRetryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
        <property name="RetryOperations" ref="taskRetryTemplate" />
    </bean>
    <bean id="taskRetryTemplate" class="org.springframework.retry.support.RetryTemplate">
        <property name="retryPolicy" ref="taskRetryPolicy" />
        <property name="backOffPolicy" ref="exponentialBackOffPolicy" />
    </bean>
    <bean id="taskRetryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
        <constructor-arg index="0" value="5" />
        <constructor-arg index="1">
            <map>
                <entry key="org.springframework.remoting.RemoteAccessException" value="true" />
            </map>
        </constructor-arg>
    </bean>
    <bean id="exponentialBackOffPolicy"
        class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
        <property name="initialInterval" value="300" />
        <property name="maxInterval" value="30000" />
        <property name="multiplier" value="2.0" />
    </bean>
</beans>
 

相关推荐

  1. Springboot 接口服务机制

    2024-01-12 20:12:02       26 阅读
  2. 第7周 接口机制设计与消息队列

    2024-01-12 20:12:02       9 阅读
  3. spring boot spring-retry机制

    2024-01-12 20:12:02       34 阅读
  4. Golang函数机制实现

    2024-01-12 20:12:02       15 阅读
  5. RestTemplate的异常机制

    2024-01-12 20:12:02       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 20:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 20:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 20:12:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 20:12:02       20 阅读

热门阅读

  1. 博客随手记

    2024-01-12 20:12:02       36 阅读
  2. List、Set、Map有什么区别?

    2024-01-12 20:12:02       27 阅读
  3. 【CS4495】Computer Vision

    2024-01-12 20:12:02       24 阅读
  4. ORACLE之rman备份恢复及故障处理

    2024-01-12 20:12:02       35 阅读
  5. 总结多线程的各种锁

    2024-01-12 20:12:02       37 阅读
  6. TCP 传输的三次握手、四次挥手策略

    2024-01-12 20:12:02       35 阅读
  7. 力扣labuladong——一刷day90

    2024-01-12 20:12:02       35 阅读
  8. HAproxy群集

    2024-01-12 20:12:02       28 阅读
  9. Vue生命周期解析

    2024-01-12 20:12:02       35 阅读