如何在feignClient中使用隔板?

时间:2019-04-04 13:41:20

标签: hystrix spring-cloud-feign netflix-feign

1,我可以在feignClient中使用隔板模式吗?

2,我对hystrix有一些困惑。


例如,如果我只有三个假客户“ a”,“ b”,“ c”。“ a”分别称为“ b”和“ c”。
我知道我可以轻松使用带有fallback参数和这样的Configuration的断路器:

@FeignClient(name = "b", fallback = bFallback.class)
protected interface HystrixClient {
  //some methods
}

@FeignClient(name = "c", fallback = cFallback.class)
protected interface HystrixClient {
  //some methods
}

以另一种方式,我可以使用@HystrixCommand将远程呼叫包装成这样的一些配置:

@HystrixCommand(fallbackMethod="getFallback") 
    public Object get(@PathVariable("id") long id) {
     //...
    }

此外,我可以在@HystrixCommandapplication.yml中配置一些参数,,我也可以在@HystrixCommand中添加threadPoolKey


Q1 :我了解到Hystrix封装了远程呼叫以实现目的,我可以通过后一种方式理解,但是前一种方式喜欢封装被叫方?

我在文档中发现:

  

Feign会用断行包装所有方法

这是否意味着FeignClient在本质上似乎在接口中的每个方法上都添加了@Hystrixcommand?

第二季度:如果Feign客户端“ b”进行了三个远程调用,我如何让它们在隔板中运行以避免一种消耗所有线程的方法?将feignClien t和@HystrixCommand?他们会发生冲突吗?
因为我在feignClient中找不到类似 threadPoolKey 的参数。自动隔板?

第3季度:如果我的hystrix配置在application.yml中,则feignClient模式和@HytirxCommand模式是否具有相同的配置模式?像这样:

hystrix:
 command:
 default:
    execution:
      isolation: 
        thread:
          timeoutInMilliseconds:1000
    circuitBreaker:
      requestVolumeThreshold:10
...
...

但是接下来的超时时间是什么?

feign:
  client:
    config:
      feignName:
        connectTimeout: 5000
        readTimeout: 5000

1 个答案:

答案 0 :(得分:0)

  

1,我可以在feignClient中使用隔板模式吗?

HystrixFeign类的setterFactory()方法的Java文档说:

/**
 * Allows you to override hystrix properties such as thread pools and command keys.
 */
public Builder setterFactory(SetterFactory setterFactory) {
  this.setterFactory = setterFactory;
  return this;
}

https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html说:

Spring Cloud Netflix默认不提供以下用于伪装的bean,但仍会从应用程序上下文中查找这些类型的bean以创建伪装客户端: •Logger.Level •重试器 •ErrorDecoder •Request.Options •收藏 •SetterFactory

因此,我们应该创建setterFactory并在其中指定线程池。您可以这样创建一个Bean:

@Bean
public SetterFactory feignHystrixSetterFactory() {
    return (target, method) -> {
        String groupKey = target.name();
        String commandKey = Feign.configKey(target.type(), method);
        return HystrixCommand.Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey( target.type().getSimpleName() ));
    };
}
  

但是接下来的超时时间是什么?

Feign客户端超时与功能区超时相似,并且指定了httpconnectin的属性,但是您可以为不同的feignclient定义不同的超时。

feign.client.config.bar.readTimeout //this configuration will apply to bar client
feign.client.config.default.readTimeout // this configuration will apply to all feign 

我是怎么发现的?如果您调试应用程序并将断点放在RetryableFeignLoadBalancer类的以下代码上:

final Request.Options options;
if (configOverride != null) {
   RibbonProperties ribbon = RibbonProperties.from(configOverride);
   options = new Request.Options(ribbon.connectTimeout(this.connectTimeout),
         ribbon.readTimeout(this.readTimeout));
}
else {
   options = new Request.Options(this.connectTimeout, this.readTimeout);
}

您将看到这些值将用作HTTPConection的属性。请看一下feign.Client类。

connection.setConnectTimeout(options.connectTimeoutMillis());
connection.setReadTimeout(options.readTimeoutMillis());