• SpringCloud重试机制配置


        首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory =  new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setReadTimeout(5000);
        httpRequestFactory.setConnectTimeout(5000);
        return new RestTemplate(httpRequestFactory);
    }

    feign重试机制

    feign默认是通过自己包下的Retryer进行重试配置,默认是5次

    package feign;
    
    import static java.util.concurrent.TimeUnit.SECONDS;
    
    /**
     * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
     * Implementations may keep state to determine if retry operations should continue or not.
     */
    public interface Retryer extends Cloneable {
    
      /**
       * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
       */
      void continueOrPropagate(RetryableException e);
    
      Retryer clone();
    
      public static class Default implements Retryer {
    
        private final int maxAttempts;
        private final long period;
        private final long maxPeriod;
        int attempt;
        long sleptForMillis;
    
        public Default() {
          this(100, SECONDS.toMillis(1), 5);
        }
    
        public Default(long period, long maxPeriod, int maxAttempts) {
          this.period = period;
          this.maxPeriod = maxPeriod;
          this.maxAttempts = maxAttempts;
          this.attempt = 1;
        }

    feign取消重试

    @Bean
        Retryer feignRetryer() {
            return Retryer.NEVER_RETRY;
        }

    feign请求超时设置

    @Bean
    Request.Options requestOptions(ConfigurableEnvironment env){
        int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
        int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);
    
        return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
    }
  • 相关阅读:
    python3中try异常调试 raise 异常抛出
    基于 k8s-搭建 Kubernetes 的 web 管理界面
    PostgreSQL SERIAL创建自增列
    C++之同名覆盖、多态
    golang实现路由中间件middleware
    fastjson源码分析之序列化
    AJPFX实践 java实现快速排序算法
    AJPFX关于IO流的简单总结
    AJPFX关于多态中的动态绑定和静态绑定的总结
    关于java的arrays数组排序示例AJPFX的分享
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/7228606.html
Copyright © 2020-2023  润新知