1、feign设置单个接口超时:通过指定的contextId

@FeignClient(name = test, contextId = "testContextId",
        fallbackFactory = TestFeignFallBackImpl.class)

#feign开启熔断 更换http客户端    
feign:
  hystrix:
    enabled: true
  # 接口级超时配置    
  client:
    config:
      testContextId: # contextId
        connectTimeout: 1000
        readTimeout: 15000

2、ribbon不能指定单个接口超时时间,可以用feign配置单个接口超时,feign和ribbon配置超时同时存在时,feign的配置生效。

要实现ribbon的单个接口超时,需要重写ribbon源码。
https://www.csdn.net/tags/MtjaQgxsMjA0ODAtYmxvZwO0O0OO0O0O.html

3、hystrixHystrix默认支持:全局(default )或 方法级(类名#方法名(参数类型) )配置。

hystrix:
  command:
    default: # 全局
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000
    # 接口级超时配置
    TestFeign#list(String):  # 类名#方法名(参数类型) 
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000

类名#方法名(参数类型) 的方式配置不够方便,可重写源码,自定义CommandKey,使支持类名配置

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import feign.Feign;
import feign.Target;
import feign.hystrix.SetterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 设置自定义CommandKey配置接口熔断超时(Hystrix默认支持的CommandKey:default 或 类名#方法名(参数类型) )
 */
@Component
@Slf4j
public class MyHystrixCommandKey implements SetterFactory {

    @Override
    public HystrixCommand.Setter create(Target<?> target, Method method) {
        String groupKey = target.name();
        String simpleName = target.type().getSimpleName();
        String commandKey = Feign.configKey(target.type(), method);

        String specialCommandKey1 = "TestFeign";
        if (specialCommandKey1.equals(simpleName)) {
            return HystrixCommand.Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(specialCommandKey1));
        }

        return HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
    }
}

@FeignClient(name = test, contextId = "testContextId",
        fallbackFactory = TestFeignFallBackImpl.class)
public interface TestFeign {
}


#feign开启熔断 更换http客户端    
feign:
  hystrix:
    enabled: true
  # 接口级超时配置    
  client:
    config:
      testContextId: # contextId
        connectTimeout: 1000
        readTimeout: 15000
        
hystrix:
  command:
    default: # 全局
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000
    # 接口级超时配置
    TestFeign:  # 类名
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
#ribbon 超时及重试        
ribbon:
  ConnectTimeout: 1000
  MaxAutoRetriesNextServer: 1
  ReadTimeout: 4000
  maxAutoRetries: 1 
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐