feign、ribbon、hystrix设置单个接口的超时时间

本文介绍了如何在Spring Cloud中针对Feign、Ribbon和Hystrix进行接口超时的配置。Feign可以通过`contextId`设置单个接口超时,而Ribbon不直接支持,需要自定义实现。Hystrix则支持全局和方法级超时配置。同时,展示了如何重写Hystrix源码以支持类名配置的接口超时。此外,还提到了Ribbon的超时和重试配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值