Spring boot Retry 사용
많은 장면 에서 재 시도 가 필요 합 니 다. 예 를 들 어 외부 요청 을 처리 할 때 상대방 의 네트워크 연결 이 끊 기거 나 다른 제어 할 수 없 는 원인 으로 데 이 터 를 요청 할 수 없고 다시 요청 을 보 내야 하 는 상황 에서 Spring boot Retry 를 사용 하면 재 시도 방법 과 재 시도 횟수 를 정의 할 수 있 습 니 다.
가방 준비
Springboot Retry 는 springboot 프레임 워 크 에 의존 하여 Maven 또는 gradle 에 springboot retry 가방 을 추가 합 니 다.
gradle:
compile('org.springframework.retry:spring-retry')
compile('org.springframework.boot:spring-boot-starter')
( Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around,
compile('org.aspectj:aspectjweaver')
2. 데모
1. 시작 클래스 애플 리 케 이 션 에 주 해 를 추가 합 니 다. @ Enableretry 에서 다시 시도 할 수 있 습 니 다.
2. springboot Retry 는 - > 이상 기반
코드:
우선 이상 정의:
public class RetryException extends RuntimeException {
private String code;
private String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
재 시도 방법:
@Service
public class RetryDemoService {
private Logger log = LoggerFactory.getLogger(RetryDemoService.class);
int i = 1;
@Retryable(value = {RuntimeException.class},maxAttempts = 4,backoff = @Backoff(delay = 5000,multiplier = 1))
public String retry(){
log.info(" ...");
i++;
if(i == 3){
return i+"";
}
RetryException retryException=new RetryException();
retryException.setCode("9999");
retryException.setMsg(" ");
throw retryException;
}
@Recover
public String recover(RetryException e){
log.info("recover::");
log.info(e.getMessage());
return "6";
}
}
재 시도 방법 머리 에 다음 과 같은 주 해 를 덧붙이다
@Retryable(value = {RuntimeException.class},maxAttempts = 4,backoff = @Backoff(delay = 5000,multiplier = 1))
value: 포 착 된 이상 을 표시 합 니 다. 즉, 어떤 이상 을 포착 한 후 다시 시도 합 니 다.
maxAttempts: 재 시도 횟수, 기본 값 은 3, 이번 방법 은 5s 입 니 다.
backoff: 재 시도 지연 ms. mliplier 는 지연 배수 1 배, 즉 5000 * 1 을 말한다.
이번 실현 에 서 는 특별히 사용자 정의 이상 을 던 져 주 해 를 제때에 포착 하여 다시 시도 하고 효 과 를 볼 수 있 도록 합 니 다.
@ Recover 수식 으로 이 방법 을 복구 방법 으로 표시 합 니 다. 즉, 모든 재 시도 횟수 가 다 떨 어 졌 는데 도 이상 을 던 졌 을 때 이 방법 을 호출 하여 기록 오류 로 처리 할 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.