装うのタイムアウトが詳細:
ではSpring Cloud
マイクロサービスアーキテクチャ、ほとんどの企業が使用しているOpen Feign
多くの問題を持っていませんデフォルトの設定を使用してコールルームサービス、比較的単純なビジネスを、しかしビジネスがある場合は、より複雑なサービスは、より複雑なビジネス・コンピューティングであることを背景が表示される可能性があるとRead Timeout
、この例外を。
1、hystrixにヒューズタイムアウト
あなたがいる場合Feign
開くデフォルトのタイムアウトが短すぎるヒューズで、簡単にビジネスサービスが完了していないコールやタイムアウトにつながるだけで1秒が、吹き飛ばされたため、切断されたヒューズはリセットタイムアウトでなければなりません。
方法を設定タイムアウトヒューズへ:
#Feign如何开启熔断
feign.hystrix.enabled=true
#是否开始超时熔断,如果为false,则熔断机制只在服务不可用时开启(spring-cloud-starter-openfeign中的HystrixCommandProperties默认为true)
hystrix.command.default.execution.timeout.enabled=true
#设置超时熔断时间(spring-cloud-starter-openfeign中的HystrixCommandProperties默认为1000毫秒)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
注:のための構成が提案されていませんが、中に取得します。hystrix
application.properties
HystrixCommandProperties
// 构造函数
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder, String propertyPrefix) {
// .... 省略很多其他配置
// propertyPrefix:hystrix,key:default
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
}
// 具体获取属性的方法
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
return HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue).build();
}
2、リボンタイムアウトに。
Feign
コールのデフォルトは使用することですRibbon
、我々はまた、について知る必要があるので、負荷分散をRibbon
タイムアウト。
①、装うコールリンク
リボンのタイムアウトを使用するための要求があるか否かに装う見て、どのようにタイムアウトリボンを読むには?
(1)、org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#実行
(2)、com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(S、com.netflix.client.config.IClientConfig)
(3)、org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory#作成します
创建Client,这里会判断对应ClientName的链接Client是否创建过,如果创建过复用之前的Client;
如果不存在则创建一个并且放入cache缓存。
public FeignLoadBalancer create(String clientName) {
FeignLoadBalancer client = this.cache.get(clientName);
if(client != null) {
return client;
}
IClientConfig config = this.factory.getClientConfig(clientName);
ILoadBalancer lb = this.factory.getLoadBalancer(clientName);
ServerIntrospector serverIntrospector = this.factory.getInstance(clientName, ServerIntrospector.class);
// 判断是否有重试
client = loadBalancedRetryFactory != null ? new RetryableFeignLoadBalancer(lb, config, serverIntrospector,
loadBalancedRetryFactory) : new FeignLoadBalancer(lb, config, serverIntrospector);
this.cache.put(clientName, client);
return client;
}
(4)、com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(S、com.netflix.client.config.IClientConfig)
负载均衡器抽象类
(5)、org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer位実行します
Feign的负载均衡器实现类。到这里我们可以看到,连接超时和读超时的配置都在这里:
如果application.properties配置文件中的超时时间不为空,则使用配置的超时时间。
如果为空则使用默认值,而从FeignLoadBalancer的构造函数可以看到,默认值也是取的RibbonProperties的默认超时时间。
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
throws IOException {
Request.Options options;
// 设置超时时间。,如果orride的配置为空,则用默认值
if (configOverride != null) {
RibbonProperties override = RibbonProperties.from(configOverride);
options = new Request.Options(
override.connectTimeout(this.connectTimeout),
override.readTimeout(this.readTimeout));
}
else {
options = new Request.Options(this.connectTimeout, this.readTimeout);
}
// 发起请求
Response response = request.client().execute(request.toRequest(), options);
return new RibbonResponse(request.getUri(), response);
}
// 构造函数
public FeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig, ServerIntrospector serverIntrospector) {
super(lb, clientConfig);
this.setRetryHandler(RetryHandler.DEFAULT);
this.clientConfig = clientConfig;
this.ribbon = RibbonProperties.from(clientConfig);
RibbonProperties ribbon = this.ribbon;
this.connectTimeout = ribbon.getConnectTimeout();
this.readTimeout = ribbon.getReadTimeout();
this.serverIntrospector = serverIntrospector;
}
②、リボンデフォルトのタイムアウト
ではRibbonClientConfiguration
ミドル:
public static final int DEFAULT_CONNECT_TIMEOUT = 1000;
public static final int DEFAULT_READ_TIMEOUT = 1000;
③、リボンタイムアウトをカスタマイズする方法
まず、RibbonProperties
次のようにソースのタイムアウトが読んで:
public Integer getConnectTimeout() {
return (Integer)this.get(CommonClientConfigKey.ConnectTimeout);
}
public Integer getReadTimeout() {
return (Integer)this.get(CommonClientConfigKey.ReadTimeout);
}
次に、あなたができるCommonClientConfigKey
2つのタイムアウトの名前を参照してください。
// ConnectTimeout:
public static final IClientConfigKey<Integer> ConnectTimeout = new CommonClientConfigKey<Integer>("ConnectTimeout") {};
// ReadTimeout:
public static final IClientConfigKey<Integer> ReadTimeout = new CommonClientConfigKey<Integer>("ReadTimeout") {};
すると、中にIClientConfig
デフォルト実装クラス:DefaultClientConfigImpl
それは見つけることができますRibbon
接頭辞として
public static final String DEFAULT_PROPERTY_NAME_SPACE = "ribbon";
だから、最終的にはRibbon
そのように構成されたタイムアウト:
ribbon.ConnectTimeout=5000
ribbon.ReadTimeout=5000
概要
設定方法Hystrix
とRibbon
それタイムアウト?
実際には、ルーチンがあります。そのためFeign
、要求の:事実でHystrix
+ Ribbon
。Hystrix
最も外側の層では、その後Ribbon
、最終的にはそこにあるhttp
要求。Soが Hystrix
ヒューズは、より大きくなければなりませんRibbon
(ConnectTimeout
+ ReadTimeout
)。場合Ribbon
には、リトライメカニズムをオンにするだけでなく、再試行の対応する数を乗じた、ことを保証するために、Ribbon
要求は上でなかった場合、Hystrix
時間が吹き出されません。