이 기사에서는 스프링 레트리를 지원하기위한 스프링 레트리 방법을 소개합니다. 그것은 당신과 공유됩니다. 세부 사항은 다음과 같습니다.
첫 번째 단계는 Maven 의존성을 소개하는 것입니다
<부모> <groupid> org.springframework.boot </groupid> <artifactid> Spring-Boot-Starter-Parent </artifactid> <버전> 1.5.3. release </version> </부모> <pecientency> <groupid> org.spramframwork.boot </groupid> <artifactid> Spring-Boot-Starter-web </artifactid> </fectionency> <!-https://mvnrepository.com/artifact/org.springframework.retry/spring-retry-> <pelection> <groupid> org.springframework.retry </groupid> <artifactid> Springretrety </artifactid> <버전> 1.1.2. release </version> </dependency> <ceptency> <groupId> org.aspectj </groupId> <artifactID> SACESWEAVER </artifactID> <버전> 1.8.6 </version> </dectortency>
2 단계 : @recover 및 @recover 주석을 추가하십시오
패키지 hello; import org.springframework.remoting.remoteaccessexception; import org.springframework.retry.annotation.backoff; import org.springframework.retry.annotation.recover; import org.spramework.annotation.recover; import org.spramegration org.springframework.stereotyp.service; @servicepublic class Remoteservice {@retryable (value = {remoteaccessexception.class}, maxattempts = 3, backoff = @backoff (delain = 5000L, multiplier = 1)) public void call () {system.out.println ( "); 새 remoteaccessexception ( "rpc call exception");}@recoverpublic void recover (recomeCessException e) {system.out.println (e.getMessage ());}}}. @retryable 주석 <br /> 주석이 달린 메소드에 예외가 있으면 다시 시도됩니다.
값 : 예외가 발생했음을 지정하고 다시 시도하십시오
포함 : 값과 마찬가지로 기본값은 비어 있습니다. 제외가 비어 있으면 모든 예외가 다시 시도됩니다.
제외 : 예외가 다시 시도되지 않도록 지정하고 기본값이 비어 있습니다. 포함이 비어있는 경우 모든 예외가 다시 시도됩니다.
MaxAttemps : 재시험 시간 수, 기본값 3
백 오프 : 기본값이없는 보상 메커니즘을 다시 시도하십시오
@backoff 주석
지연 : 지연을 지정하고 다시 시도하십시오
멀티 플라이어 : 첫 번째 재 시도가 5 초인 후 지연 = 5000L, 승수 = 2와 같은 지연의 배수를 지정합니다. 두 번째는 10 초이며 세 번째는 20 초입니다.
@다시 덮다
재 시도가 지정된 횟수에 도달하면 주석이 달린 방법이 다시 호출 되고이 방법에서 로그 처리를 수행 할 수 있습니다. 콜백은 예외가 발생하고 매개 변수 유형이 동일 한 경우에만 발생합니다.
3 단계 : 컨테이너를 시작하고 SpringBoot에서 테스트하십시오
재 시도 기능을 활성화하려면 @enabreretry 주석을 추가하십시오
패키지 hello; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.context.applicationContext; import org.spramframework.context.annotation.annotationConfigApplicationContext; import org.springframework.retry.annotation.enabretrety;@springbootApplication@enableretrypublic class application {public static void main (string [] args)은 예외 {ApplicationContext AnnotationContext = new AnnotationConfigAppicationContxt ( "Hello"); remoteservice remoteservice = AnnotationContext.getBean ( "remoteservice", remoteservice.class); Remoteservice.call (); }} 실행 결과 :
16 : 50 : 51.012 [Main] Debug org.springframework.retry.support.retrytemplate -Retry : count = 0
뭔가…
16 : 50 : 51.025 [Main] Debug org.springframework.retry.backoff.exponentialbackoffpolicy- 5000을 위해 수면
16 : 50 : 56.026 [Main] Debug org.springframework.retry.support.retrytemplate- 재면로 점검 : count = 1
16 : 50 : 56.026 [Main] Debug org.springframework.retry.support.retrytemplate -Retry : count = 1
뭔가…
16 : 50 : 56.026 [Main] Debug org.springframework.retry.backoff.exponentialbackoffpolicy- 5000을 위해 수면
16 : 51 : 01.026 [Main] Debug org.springframework.retry.support.retrytemplate- 재고 검사 : count = 2
16 : 51 : 01.027 [Main] Debug org.springframework.retry.support.retrytemplate -Retry : count = 2
뭔가…
16 : 51 : 01.027 [Main] Debug org.springframework.retry.support.retrytemplate- 재고 검사 : count = 3
16 : 51 : 01.027 [Main] Debug org.springframework.retry.support.retrytemplate -Retry 실패 마지막 시도 : count = 3
RPC 호출 예외
참조 : https://github.com/spring-projects/spring-retry
다시 채우다
비 혹독한 요청 (예 : 신규 또는 업데이트 된 작업)의 경우 데이터 일관성에 큰 영향을 미치기 때문에 재 시도를 사용하지 마십시오.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.