Spring Retry -- 重试机制

1. 简介

  Spring Retry是从Spring Batch独立出来的一个功能,主要实现了重试和熔断。
  在一般业务中,需要重试的场景有很多,比如网络中断,连接超时时,可能需要重试机制进行重试或者熔断(终止重试)。Spring Retry提供了丰富的重试功能,只需简单配置即可实现。

2. 示例代码

  • 创建工程
  • 修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.c3stones</groupId>
	<artifactId>spring-retry-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-retry-demo</name>
	<description>Spring Retry Demo</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.retry</groupId>
			<artifactId>spring-retry</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
  • 创建自定义异常类
/**
 * 自定义业务异常
 * 
 * @author CL
 *
 */
public class BusinessException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public BusinessException() {
		super();
	}

	public BusinessException(String message) {
		super(message);
	}

}
  • 创建Service
/**
 * 发送Service
 * 
 * @author CL
 *
 */
public interface SendService {

	/**
	 * 发送短信
	 * 
	 * @return
	 */
	String sms();

}
  • 创建Service实现类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import com.c3stones.exceptions.BusinessException;
import com.c3stones.service.SendService;

/**
 * 发送Service 实现
 * 
 * @author CL
 *
 */
@Service
public class SendServiceImpl implements SendService {

	private static final Logger logger = LoggerFactory.getLogger(SendServiceImpl.class);

	/**
	 * 发送短信
	 * 
	 * <p>
	 * @Retryable : 需要重试
	 * 		value : 当抛出指定异常时,开始重试
	 * 		maxAttempts : 最大重试次数,默认为3
	 * 
	 * @Backoff	: 重试中的退避策略
	 * 		delay : 延迟时间,默认为0
	 * 		maxDelay : 最大延迟时间,默认为0
	 *  	multiplier : 此次延时时间和上一次延迟时间的倍数,默认为0
	 * </p>
	 */
	@Override
	@Retryable(value = BusinessException.class, maxAttempts = 3, backoff = @Backoff(delay = 1000, maxDelay = 10000, multiplier = 2))
	public String sms() {
		logger.info("开始发送短信!");
		throw new BusinessException("发送短信异常");
	}

	/**
	 * 兜底方法,即多次重试后仍失败则执行此方法
	 * 
	 * @param e
	 * @return
	 */
	@Recover
	public String recover(BusinessException e) {
		logger.info("重试发送失败");
		return e.getMessage();
	}

}
  • 创建Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.c3stones.service.SendService;

/**
 * 发送Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "/send")
public class SendController {

	@Autowired
	private SendService sendService;

	/**
	 * 发送短信
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String send() {
		return sendService.sms();
	}

}
  • 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;

/**
 * 启动类
 * 
 * <p>
 * @EnableRetry : 开启重试机制
 * </p>
 * 
 * @author CL
 *
 */
@SpringBootApplication
@EnableRetry
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
  • 启动项目

3. 测试

  • 访问发送接口
curl http://127.0.0.1:8080/send

返回:
    发送短信异常
  • 控制台输出日志
2021-02-21 16:58:40.808  INFO 9920 --- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl  : 开始发送短信!
2021-02-21 16:58:41.810  INFO 9920 --- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl  : 开始发送短信!
2021-02-21 16:58:43.810  INFO 9920 --- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl  : 开始发送短信!
2021-02-21 16:58:43.810  INFO 9920 --- [nio-8080-exec-1] c.c3stones.service.impl.SendServiceImpl  : 重试发送失败
2021-02-21 16:58:43.811  INFO 9920 --- [nio-8080-exec-1] com.c3stones.controller.SendController   : 总耗时:3 s

4. 项目地址

  spring-retry-demo

原文地址:https://www.cnblogs.com/cao-lei/p/14431416.html