Feign, Hystrix使用笔记

  1. 如果 @FeignClient 标注的接口扩展了其他接口,则设置 fallback 后,Spring 会提示被扩展的接口有两个实现 Bean,改为设置 fallbackFactory 可以解决此问题。需要注意的是,FallbackFactory的实现类必须为 Spring 中的组件
import java.util.List;

import com.yonyou.ocm.common.rules.RuleDto;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RuleClientFallbackFactory implements FallbackFactory {
  @Override
  public RuleClient create(Throwable throwable) {
      return new RuleClient() {
          @Override
          public List getEnabledCommonRules() {
              log.info("Fallback for RuleClient.getEnabledCommonRules");
              return null;
          }

          @Override
          public List getEnabledRulesByPackage(String packageName) {
              log.info("Fallback for RuleClient.getEnabledRulesByPackage");
              return null;
          }
      };
  }
}
  1. 超时时间配置:Hystrix 的超时时间应当大于 Ribbon 的超时时间。否则在控制台会出现一条警告信息。
  • Hystrix
    hystrix:
      command:
        default:
            execution:
                isolation:
                    thread:
                        # 应该大于(ribbonReadTimeout + ribbonConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1)
                        timeoutInMilliseconds: 21000
    
  • Ribbon
    ribbon:
      ReadTimeout: 5000
      ConnectTimeout: 5000
    

你可能感兴趣的:(Feign, Hystrix使用笔记)