免费网站源码...,金坛网站优化,做自媒体常用的图片网站,让网站快速收录最新1.Retry介绍 Spring Retry 提供了自动重新调用失败的操作的功能。这在错误可能是暂时的#xff08;例如瞬时网络故障#xff09;的情况下很有用。从2.2.0版本开始#xff0c;重试功能已从Spring Batch中撤出#xff0c;成为一个独立的新库#xff1a;Spring Retry 使用场景… 1.Retry介绍 Spring Retry 提供了自动重新调用失败的操作的功能。这在错误可能是暂时的例如瞬时网络故障的情况下很有用。从2.2.0版本开始重试功能已从Spring Batch中撤出成为一个独立的新库Spring Retry 使用场景 在日常开发过程中难免会与第三方接口发生交互例如短信发送、远程服务调用、争抢锁等场景当正常调用发生异常时例如网络抖动这些间歇性的异常在一段时候之后会自行恢复程序为了更加健壮并且更不容易出现故障需要重新触发业务操作以防止间歇性的异常对程序照成的影响。 tips幂等性 非幂等的情况下要小心使用重试。HTTP/1.1中对幂等性的定义是一次和多次请求某一个资源对于资源本身应该具有同样的结果网络超时等问题除外。也就是说其任意多次执行对资源本身所产生的影响均与一次执行的影响相同。 2.代码工程 实验目标模拟三方接口异常触发重试 pom.xml ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringboot-demo/artifactIdgroupIdcom.et/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdSpringRetry/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--retry--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactId/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactId/dependency/dependencies
/project application.yaml server:port: 8088 RemoteApiService.java Retryable注解value值表示当哪些异常的时候触发重试maxAttempts表示最大重试次数默认为3delay表示重试的延迟时间multiplier表示上一次延时时间是这一次的倍数。Recover注解当重试次数达到设置的次数的时候还是失败抛出异常执行的回调函数。 package com.et.retry.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 java.time.LocalDateTime;
import java.time.LocalTime;Service
public class RemoteApiService {private Logger logger LoggerFactory.getLogger(getClass());Retryable(value Exception.class,maxAttempts 3,backoff Backoff(delay 2000,multiplier 1.5))public boolean pay(int num) throws Exception{logger.info(invoke third method);logger.info(do something... {}, LocalDateTime.now());//mock exceptionif(num0) {throw new Exception(errorneed retry);}return true;}Recoverpublic boolean recover(int num) throws Exception {logger.info(recover ... {},{}, num, LocalDateTime.now());return false;}} DemoApplication.java 在主类上加上EnableRetry注解表示启用重试机制。 package com.et.retry;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;SpringBootApplication
EnableRetry
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
} 以上只是一些关键代码所有代码请参见下面代码仓库 代码仓库 https://github.com/Harries/springboot-demo 3.测试 编写测试类 package com.et.retry;import com.et.retry.service.RemoteApiService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class)
SpringBootTest(classes DemoApplication.class)
public class DemoTests {private Logger log LoggerFactory.getLogger(getClass());AutowiredRemoteApiService remoteApiService;Beforepublic void before() {log.info(init some data);}Afterpublic void after(){log.info(clean some data);}Testpublic void execute() throws Exception {log.info(pay result:remoteApiService.pay(0));}} 运行测试方法结果如下 2024-04-03 10:35:54.738 INFO 13096 --- [ main] com.et.retry.DemoTests : init some data
2024-04-03 10:35:54.756 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
2024-04-03 10:35:54.759 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:54.758
2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:56.766
2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method
2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:59.776
2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : recover ... 0,2024-04-03T10:35:59.776
2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.DemoTests : pay result:false
2024-04-03 10:35:59.778 INFO 13096 --- [ main] com.et.retry.DemoTests : clean some data 4.引用 https://www.jianshu.com/p/314059943f1chttp://www.liuhaihua.cn/archives/710389.html