- 依赖
org.springframework.retry spring-retry 1.1.5.RELEASE org.springframework spring-aspects
- 使用注意事项
查询可以进行重试,写操作要慎重 - 案例
3.1 注解启用Retry
@EnableRetry
3.2 重试的方法上增加@Retryable 注解
@RestController
@RequestMapping("/test1")
public class Test1Controller {
@SneakyThrows
@GetMapping
@Retryable(value = Exception.class,maxAttempts = 3)
public CommonResult test1(){
String result = new RestTemplate().getForObject("http://10.5.9.88:8080/test2/exception", String.class);
System.out.println(result);
JSONObject object = JSON.parseObject(result);
Integer code = object.getInteger("code");
if(code == 500){
throw new Exception("调用其他服务做业务,失败了!");
}
return CommonResult.success();
}
}



