首页 > 其他分享 >SpringCloud重试retry 20220927

SpringCloud重试retry 20220927

时间:2022-09-27 09:47:46浏览次数:51  
标签:Retryer feign retry int SpringCloud 重试 ribbon

SpringCloud重试retry是一个很赞的功能,能够有效的处理单点故障的问题。主要功能是当请求一个服务的某个实例时,譬如你的User服务启动了2个,它们都在eureka里注册了,那么正常情况下当请求User服务时,ribbon默认会轮询这两个实例。此时如果其中一个实例故障了,发生了宕机或者超时等,如果没有配置启用重试retry策略,那么调用方就会得到错误信息或者超时无响应或者是熔断返回的信息。我们希望的自然是一个故障了,会自动切换到另一个去访问。

最简单的方法就是retry。

需要先在pom.xml里加入

  1. <dependency>
  2. <groupId>org.springframework.retry</groupId>
  3. <artifactId>spring-retry</artifactId>
  4. </dependency>

 

ribbon、zuul、feign都可以配置各自的retry方式。

1 ribbon配置如下

  1. @Bean
  2. @LoadBalanced
  3. RestTemplate restTemplate() {
  4. HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  5. httpRequestFactory.setReadTimeout(5000);
  6. httpRequestFactory.setConnectTimeout(5000);
  7. return new RestTemplate(httpRequestFactory);
  8. }

 

2 zuul配置如下

zuul的重试比较简单,不需要任何代码,直接在yml里配置即可。 注意,配置时,ribbon开头的在yml里是不给提示的,不要以为不提示就是没效果,其实是可以用的。 这个ReadTimeout和ConnectTimeout区别是很大的,ConnectTimeout是指建立连接的时间,如果目标服务宕机或网络故障,那么响应的就是ConnectTimeout,无法连接。而ReadTimeout则是连接建立后,等待目标服务返回响应的时间,譬如目标服务做了一个复杂操作导致耗时较长,那么会触发ReadTimeout。 譬如zuul路由了/user路径到user服务上,如果User1实例宕机了,那么配置了retry的zuul就会在重试MaxAutoRetries次数后,切换到另一个实例User2上。如果User2也故障了,那么返回404. retryableStatusCodes里面有几个错误码,意思就是遇到哪些错误码时触发重试。默认是404,我多配了几个,仅供参考。

3 feign配置如下

feign默认是通过自己包下的Retryer进行重试配置,默认是5次
  1. import static java.util.concurrent.TimeUnit.SECONDS;
  2.  
  3. /**
  4. * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
  5. * Implementations may keep state to determine if retry operations should continue or not.
  6. */
  7. public interface Retryer extends Cloneable {
  8.  
  9. /**
  10. * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
  11. */
  12. void continueOrPropagate(RetryableException e);
  13.  
  14. Retryer clone();
  15.  
  16. public static class Default implements Retryer {
  17.  
  18. private final int maxAttempts;
  19. private final long period;
  20. private final long maxPeriod;
  21. int attempt;
  22. long sleptForMillis;
  23.  
  24. public Default() {
  25. this(100, SECONDS.toMillis(1), 5);
  26. }
  27.  
  28. public Default(long period, long maxPeriod, int maxAttempts) {
  29. this.period = period;
  30. this.maxPeriod = maxPeriod;
  31. this.maxAttempts = maxAttempts;
  32. this.attempt = 1;
  33. }
  34.  
feign取消重试
  1. @Bean
  2. Retryer feignRetryer() {
  3. return Retryer.NEVER_RETRY;
  4. }
feign请求超时设置
  1. @Bean
  2. Request.Options requestOptions(ConfigurableEnvironment env){
  3. int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
  4. int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);
  5.  
  6. return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
  7. }
参考:http://www.cnblogs.com/zhangjianbin/p/7228606.html

标签:Retryer,feign,retry,int,SpringCloud,重试,ribbon
From: https://www.cnblogs.com/smallfa/p/16733422.html

相关文章

  • SpringCloud微服务架构(二)
    nacos配置管理1.在nacos中添加配置信息2.在弹出表单中填写配置信息3.配置获取的步骤如下4.配置的步骤如下:1)引入Nacos的配置管理客户端依赖<!--nacos配置管理依......
  • pdfkit,retry,wordcloud
    pdfkitpipinstallpdfkit#wkhtmltopdf的Python封装包安装wkhtmltopdf并添加至环境变量sudoaptinstallwkhtmltopdfpdfkit用法pdfkit.from_url('http://google......
  • SpringCloud/Alibaba学习+Nacos/Sentinel源码(五万字长文)
    SpringCloud学习笔记未经授权不得转载,创作不易,违者必究一、微服务架构1.1应用架构发展集中式架构网站流量很小,一个应用将所有功能部署优点:系统开发速度快;维护成本......
  • springcloud--负载均衡(ribbon)
    springcloud--负载均衡(ribbon)一、项目背景搭建好的springcloud项目,包含erueka模块(这里是单个,也可以多个)、服务提供者(多个)、消费者。创建多个服务提供者模块,用于集群(单......
  • SpringCloud config分布式配置
    分布式的问题,配置文件的管理简单来说就是,我们每一个微服务自己带着一个application.yml,那上百的的配置文件要修改起来,岂不是要疯了。所以将一些经常要改的文件发布到git......
  • springcloud快速入门
    一、项目思路使用idea工具创建一个maven空项目,删除不需要的src目录等东西,用来做父项目,只剩下pom.xml文件在父项目中依次创建三个模块module,分别为eruekaservice、provid......
  • SpringCloud微服务架构
    认识微服务单体架构将业务的所有功能集中在一个项目中开发,打成一个包部署。优点:1.架构简单2.部署成本低缺点:耦合度高分布式架构根据业务功能对系统进行拆分,......
  • pip install 安装第三方库时提示 WARNING: Retrying (Retry(total=4, connect=None, r
    当尝试在cmd中用pip命令安装第三方库时,出现如下的WARNINGWARNING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))afterconnectio......
  • feign.RetryableException: iZuf627hz8vloz1wtzgxzdZ executing GET http://ip-servic
    问题就是通过feign调用接口的时候,去注册中心获取服务位置的时候,拿了服务器的实例名,这是微服务没法处理服务提供者这样配置eureka.instance.prefer-ip-address=trueeure......
  • SpringCloud使用注解+AOP+MQ来实现日志管理模块
    简介无论在什么系统中,日志管理模块都属于十分重要的部分,接下来会通过注解+AOP+MQ的方式实现一个简易的日志管理系统思路注解: 标记需要记录日志的方法AOP: 通过AOP......