首页 > 编程知识 正文

eureka重试机制,reCAPTCHA保护机制

时间:2023-05-05 00:35:55 阅读:174791 作者:1358

文章列表1、重试1.1重试作用2、重试三种方法2.1 Java retry 2.2 spring retry 2.3 guava-retrying 2.3.1重试源2.3.2自定义重试侦听器RetryListener2.3.3

1、重试1.1重试作用

重试有场景限制,不是任何场景都适合重试。 例如,参数检查不正确,写入操作等(是否应该写入等)不适合重试。

远程调用超时、网络突然中断可以重试。 微服务治理框架通常有自己的重试和超时配置。 例如,dubbo可以设置retries=1,timeout=500,如果调用失败,则只重试一次,如果超过500ms没有返回调用,则调用失败。

如果外部RPC调用或数据签入等操作在一次操作中失败,则可以多次重试,从而增加调用成功的可能性。

2、重试三种方式2.1 Java retry 2.2 spring retry 2.3 guava-retryingretryerbuilder是factory创建者,可以自定义并配置重试源,可以支持多个重试源,重试次数

2.3.1 retryerbuilder重试源支持Exception异常对象和自定义断言对象,可以在retryIfException和retryIfResult中设置,并同时支持多个

抛出retryIfException、runtime异常和checked异常将重试,但抛出error不会重试。

retryIfRuntimeException仅在抛出runtime异常时重试,不重试checked异常或error。

retryIfExceptionOfType允许您仅在出现特定异常(如NullPointerException或IllegalStateException )时重试。 这包括自定义错误。 或者用Predicate实现。

仅当抛出. retryifexceptionoftype (error.class ) error时, 重试. retryifexceptionoftype (illegalstateexception.class ).retryifexceptionoftype (nullpointerexception.class )/NSS

predicates.instance of (illegalstateexception.class ) ) retryIfResult可以指定Callable方法在返回值时重试,如下所示

返回. retryifresult (predicates.equal to (false ) ) false, retryifresult )重试predicates.contains pattern (_error $ ) _ error结束后重试2.3.2自定义重试侦听器RetryListener重试发生后

每次重试时,guava-retrying都会自动回调注册的拦截。 也可以注册多个retry监听器。 按注册顺序依次调用。

. withretrylistener (newretrylistener ) @ overridepublictvoidonretry (attempt ) ) {logger.error )第【】】次调用}} 2.3.3重试停止策略stopstrategystopafterdelaystrategy :设置最大允许执行时间; 例如,最长设定10s,无论任务的执行次数如何,在重试时都超过了最长时间的情况下,任务结束,返回重试异常RetryException;

NeverStop Strategy (用于不停止重试,需要一直知道会有预期的结果的情况;

StopAfterAttempt Strategy (设定最大重试次数,超过最大重试次数时停止重试,返回重试异常;

2.3.4等待时间策略(控制时间间隔)固定等待时间策略)。 随机等待时间策略)允许指定最小和最大时间。 等待时间在每个区间都是随机的值。 不等待

Strategy:递增等待时长策略(提供一个初始值和步长,等待时间随重试次数增加而增加)。ExponentialWait Strategy:指数等待时长策略。FibonacciWait Strategy :Fibonacci等待时长策略。ExceptionWait Strategy:异常时长等待策略。CompositeWait Strategy:复合时长等待策略。 3、guava-retrying demo 3.1 Maven依赖

引用Guava-Retrying的包

<dependency> <groupId>com.github.rholder</groupId> <artifactId>guava-retrying</artifactId> <version>2.0.0</version></dependency> 3.2 项目结构

RetryDemo.java package com.xyy;import com.github.rholder.retry.Retryer;import com.github.rholder.retry.RetryerBuilder;import com.github.rholder.retry.StopStrategies;import com.google.common.base.Predicates;import java.util.concurrent.TimeUnit;import static com.github.rholder.retry.WaitStrategies.incrementingWait;/** * @author wangxuexing * @descrption * @date */public class RetryDemo { public static void main(String[] args) { Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder(). //如果异常会重试 retryIfException(). //如果结果为false会重试 retryIfResult(Predicates.equalTo(false)). //重调策略 withWaitStrategy(incrementingWait(30, TimeUnit.SECONDS, 30, TimeUnit.SECONDS)). //尝试次数 withStopStrategy(StopStrategies.stopAfterAttempt(3)). //注册监听 withRetryListener(new MyRetryListener()).build(); try { retryer.call(new TaskCallable()); } catch (Exception e) { e.printStackTrace(); } }} TaskCallable.java,其中TaskCallable是任务的具体实现类,它实现了Callable接口 package com.xyy;import java.util.concurrent.Callable;public class TaskCallable implements Callable<Boolean> { @Override public Boolean call() throws Exception { return false; }} MyRetryListener.java,MyRetryListener监听实现了RetryListener接口,每次重试都会回调注册的监听 package com.xyy;import com.github.rholder.retry.Attempt;import com.github.rholder.retry.RetryListener;/** * @author wangxuexing * @descrption * @date */public class MyRetryListener implements RetryListener { @Override public <V> void onRetry(Attempt<V> attempt) { System.out.print("[retry]time=" + attempt.getAttemptNumber()); // 距离上一次重试的延迟 System.out.print(",delay=" + attempt.getDelaySinceFirstAttempt()); // 重试结果: 是异常终止, 还是正常返回 System.out.print(",hasException=" + attempt.hasException()); System.out.print(",hasResult=" + attempt.hasResult()); // 是什么原因导致异常 if (attempt.hasException()) { System.out.print(",causeBy=" + attempt.getExceptionCause().toString()); } else {// 正常返回时的结果 System.out.print(",result=" + attempt.getResult()); } System.out.println(); }} 执行结果

参考:https://www.programminghunter.com/article/4327483899/
参考:https://segmentfault.com/a/1190000022962709

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。