Spring-Retry

011 @Retryable的使用

别说谁变了你拦得住时间么 提交于 2020-05-05 12:18:26
一:概述   在调用第三方接口或者使用mq时,会出现网络抖动,连接超时等网络异常,所以需要重试。   为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执行成功。   例如,由于网络故障或数据库更新中的DeadLockLoserException导致Web服务或RMI服务的远程调用可能会在短暂等待后自行解决。 为了自动执行这些操作的重试,Spring Batch具有RetryOperations策略。不过该重试功能从Spring Batch 2.2.0版本中独立出来,变成了Spring Retry模块。 二:示例 1.pom < dependency > < groupId > org.springframework.retry </ groupId > < artifactId > spring-retry </ artifactId > </ dependency > < dependency > < groupId > org.aspectj </ groupId > < artifactId > aspectjweaver </ artifactId > </ dependency > 2.配置类   只有启用@EnabelRetry才行,写在配置类或者启动类上都是可以的 package com.jun.web.config; import

springcloud ribbon负载均衡

匆匆过客 提交于 2020-04-29 15:40:43
springcloud ribbon负载均衡 1.我们先启动上次建好的eureka、product服务 product服务要设置多个端口,将端口修改为9001,9011 启动后我们访问 http://localhost:9000/ 当我们用order服务去调用product服务时会发现,一会调用9001,一会调用9011,这就是ribbon的默认负载策略是轮询的方式,每个节点都访问一次。 修改ribbon负载均衡策略 ribbon有以下几种方式策略: 1. com.netflix.loadbalancer.RoundRobinRule :以轮询的方式进行负载均衡。 2.com.netflix.loadbalancer.RandomRule :随机策略 3.com.netflix.loadbalancer.RetryRule :重试策略。 4.com.netflix.loadbalancer.WeightedResponseTimeRule :权重策略。会计算每个服务的权重,越高的被调用的可能性越大。 5.com.netflix.loadbalancer.BestAvailableRule :最佳策略。遍历所有的服务实例,过滤掉故障实例,并返回请求数最小的实例返回。 6.com.netflix.loadbalancer.AvailabilityFilteringRule

Which HTTP errors should never trigger an automatic retry?

扶醉桌前 提交于 2020-04-07 18:41:10
问题 I'm trying to make a few microservices more resilient and retrying certain types of HTTP requests would help with that. Retrying timeouts will give clients a terribly slow experience, so I don't intend to retry in this case. Retrying 400s doesn't help because a bad request will remain a bad request a few milliseconds later. I imagine there are other reasons to not retry a few other types of errors, but which errors and why? 回答1: There are some errors that should not be retried because they

Which HTTP errors should never trigger an automatic retry?

萝らか妹 提交于 2020-04-07 18:40:47
问题 I'm trying to make a few microservices more resilient and retrying certain types of HTTP requests would help with that. Retrying timeouts will give clients a terribly slow experience, so I don't intend to retry in this case. Retrying 400s doesn't help because a bad request will remain a bad request a few milliseconds later. I imagine there are other reasons to not retry a few other types of errors, but which errors and why? 回答1: There are some errors that should not be retried because they

How to retry with spring kafka version 2..2

拥有回忆 提交于 2020-03-05 00:26:59
问题 Just trying to find out a simple example with spring-kafka 2.2 that works with a KafkaListener, to retry last failed message. If a message fails, the message should be redirected to another Topic where the retries attempts will be made. We will have 4 topics. topic , retryTopic , sucessTopic and errorTopic If topic fails, should be redirected to retryTopic where the 3 attempts to retry will be made. If those attempts fails, must redirect to errorTopic . In case of sucess on both topic and

Retry not working with Spring Batch with Java Config

荒凉一梦 提交于 2020-03-03 12:27:12
问题 I have Spring batch job with following config: @Bean public Job myJob(Step step1, Step step2, Step step3) { return jobs.get("myJob").start(step1).next(step2).next(step3).build(); } @Bean public Step step1(ItemReader<String> myReader, ItemProcessor<String, String> myProcessor, ItemWriter<String> myWriter) { return steps.get("step1").<String, String>chunk(1) .reader(myReader) .faultTolerant().retryLimit(3).retry(MyException.class) .processor(myProcessor) .writer(myWriter) .build(); } @Bean

spring retry 小试

南楼画角 提交于 2020-02-29 01:25:41
retry就是重试的意思,在我们的系统中,分布式或者是外部服务,存在其他服务异常,或者网络问题,需要我们重试的情况,但是重试可能还是错误的,总不能让系统一直报错,直到崩掉吧。所以熔断器的功能就是必须的啦,在重试的次数还是失败的时候,就终止,比如dubbo这样微服务的框架,一般都会配置重试次数。这些spring retry 都做了处理,还可以配置重试的异常类型和时间间隔等。 可以看看这个quickstart(https://github.com/spring-projects/spring-retry),然后创建一个spring boot 的项目。 然后在 pom 中加入相应的依赖 然后我们写一个启动类,区别就是加上 EnableRetry 的注解,这样重试会生效 然后我们看看我们实现的类中的方法,这里我们会捕获TypeOneException.class, TypeTwoException.class异常,然后maxAttempts是4 日志输出: 如果我们不用重试,那么会是以下的日志输出: 有什么讨论的内容,可以加我微信公众号: 来源: oschina 链接: https://my.oschina.net/u/2277632/blog/1858191

spring-retry重试框架

跟風遠走 提交于 2020-02-25 19:11:13
最近有个超时问题经常发生,运营商也无法解决,只能引入重试机制来实现了。本来想自己写个for觉得太low,找到有这么个框架也很简单。 1、添加pom引用 <!--重试框架引入jar包--> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> 2、在springboot的启动类上添加启用重试机制的注解。 @EnableRetry 3、在需要重试的方法上添加重试注解。参数就不多做解释了,也比较简单,如果碰到难的在做笔记。 value:要拦截的异常类型 maxAttempts:最大重试次数 delay:延迟多少毫秒后重新执行 multiplier:指定延迟倍数,默认为0,表示固定暂停2秒后进行重试。 @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1)) private void

Can Spring's Retryable or RetryTemplate use Retry-After header for dynamic backoff?

半城伤御伤魂 提交于 2020-02-05 05:13:24
问题 Can I make Spring's @Retryable or @RetryTemplate use the number received in a Retry-After header in an HTTP 503 "Service Unavailable" response as delay for the next retry iteration? For example: @Retryable(maxAttempts = 42, backoff = @Backoff(delay = 1000), value = NotYetReady.class) public boolean isExternalComponentReadyToUse() throws NotYetReady { ResponseEntity<String> response = callRestEndpointToCheckReadiness(); if (!response.getStatus().is2xxSuccessful()) { int retryAfterInSeconds =

@Recover methods are not triggered with @Retryable

时光怂恿深爱的人放手 提交于 2020-01-15 07:36:07
问题 I followed this question after getting ExhaustedRetryException on @Retryable function. The @Retryable function is retrying. Here's the Delegate with the @Retryable function: @Component public class OrderRequestDelegate { private static final Logger LOGGER = LoggerFactory.getLogger(OrderRequestDelegate.class); private final OrderRequestDao orderRequestDao; private final SqsQueueDao sqsQueueDao; @Autowired public OrderRequestDelegate(OrderRequestDao orderRequestDao, SqsQueueDao sqsQueueDao) {