Spring Retry && Guava Retry
Spring Retry
一、引入Maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
二、EnableRetry注解
package com.zxy.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
@SpringBootApplication
@EnableRetry
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、在需要重试的方法添加@Retryable注解
package com.zxy.demo.service.impl;
import cn.hutool.core.date.DateUtil;
import com.zxy.demo.service.RetryService;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
/**
* @description: study
* 模块名称:
* 说明:
* 作者(@author): zxy
* 创建日期:
*/
@Service
public class RetryServiceImpl implements RetryService {
@Override
@Retryable(value = {RetryException.class}, maxAttempts = 5, backoff = @Backoff(delay = 5000L, multiplier = 2))
public void test(String str) {
System.out.println("retry------test:" + str);
System.out.println(DateUtil.now());
throw new RetryException("发生异常");
}
@Recover
private void recover(RetryException e) {
System.out.println(e.getMessage());
}
}
参数解释
@Retryable
- value:指定发生的异常进行重试;
- include:和value一样,默认空,当exclude也为空时,所有异常都重试;
- exclude:指定异常不重试,默认空,当include也为空时,所有异常都重试;
- maxAttemps:重试次数,默认为3;
- backoff:重试补偿机制,默认没有
@Backoff
- delay:指定延迟后重试;
- multiplier:指定延迟的倍数,比如delay = 5000L,multiplier=2时,第一次重试为5秒后,第二次10秒,第三次为20秒。
@Recover
- 当重试到达指定次数时,被注解的方法将被回调,可以再该方法中进行日志处理等操作。需要注意的时发生的异常和入参类型一致时才会回调。
Guava Retry
一、引入Maven依赖
<!-- https://mvnrepository.com/artifact/com.github.rholder/guava-retrying -->
<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>
二、代码展示
package com.zxy.demo.retry;
import cn.hutool.core.date.DateUtil;
import com.github.rholder.retry.*;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* @description: study
* 模块名称:
* 说明: Retry Google Guava
* 作者(@author): zxy
* 创建日期: 2022年2月23日10:01:04
*/
@Slf4j
public class RetryGuava {
public static void main(String[] args) throws Exception {
//定义重试机制
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
// retryIf 重试条件返回值为false
// .retryIfResult(Predicates.equalTo(false))
//设置异常重试源 发生具体异常
.retryIfExceptionOfType(Exception.class)
//设置等待间隔时间 没过几秒后进行重试
// .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
// 每次重试递增时间量
.withWaitStrategy(WaitStrategies.incrementingWait(1, TimeUnit.SECONDS,2, TimeUnit.SECONDS))
//设置最大重试次数
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.build();
try {
retryer.call(() -> test());
} catch (RetryException | ExecutionException e) {
e.printStackTrace();
}
}
public static Boolean test() throws Exception {
String now = DateUtil.now();
System.out.println(now);
System.out.println("异常");
throw new Exception("发送异常,需要重试");
// return false;
}
}
参数解释
retryIfResult
- 方法触发重试的结果,比如方法返回null或方法返回false等结果后触发重试操作。
retryIfExceptionOfType
- 方法发生异常后重试,可以指定具体异常。
withWaitStrategy
- 指定重试策略,比如WaitStrategies.fixedWait(long sleepTime, TimeUnit timeUnit),比如设置了5秒,则每次重试的间隔都是五秒。
- WaitStrategies.incrementingWait(long initialSleepTime, TimeUnit initialSleepTimeUnit, long increment, TimeUnit incrementTimeUnit),时间量递增重试,比如参数为 1, TimeUnit.SECONDS,2, TimeUnit.SECONDS,则第二次间隔一秒,第三次间隔三秒,第四次间隔五秒,递增式增加时间量。
withStopStrategy
- 设置最大重试次数。
try {
retryer.call(() -> 发送某种错误后要重试的方法);
} catch (ExecutionException e) {
// 发生其他异常时触发;
} catch (RetryException e) {
// 重试完成后仍未拿到正确响应时触发,可以记录日志等操作;
}
标题:Spring Retry && Guava Retry
作者:zzzzchen
地址:https://www.dczzs.com/articles/2022/02/26/1645844978034.html