Spring boot Retry 사용

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 수식 으로 이 방법 을 복구 방법 으로 표시 합 니 다. 즉, 모든 재 시도 횟수 가 다 떨 어 졌 는데 도 이상 을 던 졌 을 때 이 방법 을 호출 하여 기록 오류 로 처리 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기