1.場景
有時業務上有需求,大部分接口響應時間都很短,就有那麼幾個接口比如上傳下載、長輪詢時間很長,如果統一把超時時間都設置長,就起不到超時熔斷的效果了。
2.分析
從Gateway 到 下游服務,超時時間設置 共有四個地方,分別是 gateway的hystrix、gateway的ribbon(或者feign)、下游服務ribbon、下游服務的hystrix。
通常來說網關的超時時間是最長的,假設鏈路是 網關-A服務-B服務,網關超時時間應該是調用鏈總和。
hystrix時間最好也大於ribbon,畢竟ribbon是接口的大概執行時間,出意外超時後,再進入熔斷。
ribbon有兩個超時時間:
ribbon:
ReadTimeout: 20000 #ribbon讀取超時時間,接口處理時間,不包括建立連接時間
ConnectTimeout: 3000 #ribbon請求連接時間
hystrix有一個超時時間,超過此時間進入熔斷。
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 2000 #超過此時間後進入熔斷,這個時間應該大於後端及其ribbon的時間,否則後端接口未執行完就進入熔斷
gateway在路由時可以指定 hystrixCommondKey,並且對hystrixCommondKey設置超時時間,下面key是fallbackcmd:
cloud:
gateway:
routes:
- id: frontPC
uri: lb://wisdomclass-front-pc #lb代表從註冊中心獲取服務,將path的請求路由到uri
predicates:
- Path=/pc/** #不想用服務名做path前綴,怕暴露
filters:
- StripPrefix=1 #除去第一個/前綴,比如請求/wisdomclass-demo/demo,會去除前綴/wisdomclass-demo,請求到路由服務的 /demo接口
- RemoveRequestHeader=Origin # 去除請求頭的origin字段,此字段導致post請求 無法進入網關post熔斷
- name: Hystrix #熔斷
args:
name: fallbackcmd
fallbackUri: forward:/fallback #網關的統一熔斷接口
hystrix:
command:
fallbackcmd:
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 2000
有時業務上有需求,大部分接口響應時間都很短,就有那麼幾個接口比如上傳下載、長輪詢時間很長,如果統一把超時時間都設置長,就起不到超時熔斷的效果了。
經查詢,網上有針對某個服務設置ribbon 的超時時間方法,但不滿足需求。
最後找到,可以針對單獨接口 設置hystrix的超時時間,ribbon沒有找到有效方法,但可以暫時滿足需求,將ribbon時間統一設置較長,然後hystrix針對設置。
3.解決辦法
1.gateway
自定義熔斷工廠,裏面設置了熔斷name爲 SpecialHystrix
package cn.sosuncloud.wisdomclass.hystrix;
import com.netflix.hystrix.*;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import rx.Observable;
import rx.RxReactiveStreams;
import rx.Subscription;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
@Component
public class SpecialHystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<SpecialHystrixGatewayFilterFactory.Config> {
private static final String NAME = "SpecialHystrix";
private final ObjectProvider<DispatcherHandler> dispatcherHandler;
public SpecialHystrixGatewayFilterFactory(ObjectProvider<DispatcherHandler> dispatcherHandler) {
super(Config.class);
this.dispatcherHandler = dispatcherHandler;
}
@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList(NAME_KEY);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().pathWithinApplication().value();
Map<String, Integer> timeoutMap = config.getTimeout();
Integer timeout = null;
if (timeoutMap != null) {
//對rest接口通配符url進行轉換 暫只配置url 末尾爲數字的的接口---
path = config.wildCard(path);
timeout = timeoutMap.get(path);
}
MyRouteHystrixCommand command;
if (timeout == null) {
//沒有定義時間的接口將使用配置的default時間
command = new MyRouteHystrixCommand(config.getFallbackUri(), exchange, chain, path);
} else {
//有配置時間的接口將使用配置的時間
command = new MyRouteHystrixCommand(config.getFallbackUri(), exchange, chain, timeout, path);
}
return Mono.create(s -> {
Subscription sub = command.toObservable().subscribe(s::success, s::error, s::success);
s.onCancel(sub::unsubscribe);
}).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> {
if (throwable instanceof HystrixRuntimeException) {
HystrixRuntimeException e = (HystrixRuntimeException) throwable;
HystrixRuntimeException.FailureType failureType = e.getFailureType();
switch (failureType) {
case TIMEOUT:
return Mono.error(new TimeoutException());
case COMMAND_EXCEPTION: {
Throwable cause = e.getCause();
if (cause instanceof ResponseStatusException || AnnotatedElementUtils
.findMergedAnnotation(cause.getClass(), ResponseStatus.class) != null) {
return Mono.error(cause);
}
}
default:
break;
}
}
return Mono.error(throwable);
}).then();
};
}
@Override
public String name() {
return NAME;
}
private class MyRouteHystrixCommand extends HystrixObservableCommand<Void> {
private final URI fallbackUri;
private final ServerWebExchange exchange;
private final GatewayFilterChain chain;
public MyRouteHystrixCommand(URI fallbackUri, ServerWebExchange exchange, GatewayFilterChain chain,
String key) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(key))
.andCommandKey(HystrixCommandKey.Factory.asKey(key)));
this.fallbackUri = fallbackUri;
this.exchange = exchange;
this.chain = chain;
}
public MyRouteHystrixCommand(URI fallbackUri, ServerWebExchange exchange, GatewayFilterChain chain,
int timeout,
String key) {
//***出現通配符的情況**//
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(key))
.andCommandKey(HystrixCommandKey.Factory.asKey(key))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(timeout)));
this.fallbackUri = fallbackUri;
this.exchange = exchange;
this.chain = chain;
}
@Override
protected Observable<Void> construct() {
return RxReactiveStreams.toObservable(this.chain.filter(exchange));
}
@Override
protected Observable<Void> resumeWithFallback() {
if (null == fallbackUri) {
return super.resumeWithFallback();
}
URI uri = exchange.getRequest().getURI();
boolean encoded = containsEncodedParts(uri);
URI requestUrl = UriComponentsBuilder.fromUri(uri)
.host(null)
.port(null)
.uri(this.fallbackUri)
.build(encoded)
.toUri();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
ServerHttpRequest request = this.exchange.getRequest().mutate().uri(requestUrl).build();
ServerWebExchange mutated = exchange.mutate().request(request).build();
DispatcherHandler dispatcherHandler = SpecialHystrixGatewayFilterFactory.this.dispatcherHandler.getIfAvailable();
return RxReactiveStreams.toObservable(dispatcherHandler.handle(mutated));
}
}
public static class Config {
private String id;
private URI fallbackUri;
/**
* url -> timeout ms
*/
private Map<String, Integer> timeout;
public String getId() {
return id;
}
public Config setId(String id) {
this.id = id;
return this;
}
public URI getFallbackUri() {
return fallbackUri;
}
public Config setFallbackUri(URI fallbackUri) {
if (fallbackUri != null && !"forward".equals(fallbackUri.getScheme())) {
throw new IllegalArgumentException("Hystrix Filter currently only supports 'forward' URIs, found " + fallbackUri);
}
this.fallbackUri = fallbackUri;
return this;
}
public Map<String, Integer> getTimeout() {
return timeout;
}
public Config setTimeout(Map<String, Integer> timeout) {
//YAML解析的時候MAP的KEY不支持'/',這裏只能用'-'替代
Map<String, Integer> tempTimeout = new HashMap<>(timeout.size());
for (String key : timeout.keySet()) {
Integer value = timeout.get(key);
key = key.replace("-", "/");
if (!key.startsWith("/")) {
key = "/" + key;
}
/** 末尾有動態傳參 **/
if (key.endsWith("/")) {
key = key + "**";
}
tempTimeout.put(key, value);
}
this.timeout = tempTimeout;
return this;
}
public String wildCard(String path){
String replace = path;
String[] split = path.split("/");
if (split.length>0) {
String wildcard = split[split.length - 1];
boolean numeric = isNumeric(wildcard);
if (numeric) {
replace = path.replace(wildcard, "**");
}
}
return replace;
}
private boolean isNumeric(String str) {
String bigStr;
try {
bigStr = new BigDecimal(str).toString();
} catch (Exception e) {
return false;//異常 說明包含非數字。
}
return true;
}
}
}
路由時更換熔斷類,指定路由熔斷name 爲 SpecialHystrix,並且指定 特殊接口的超時時間20s:
cloud:
gateway:
routes:
- id: frontPC
uri: lb://wisdomclass-front-pc #lb代表從註冊中心獲取服務,將path的請求路由到uri
predicates:
- Path=/pc/** #不想用服務名做path前綴,怕暴露
filters:
- StripPrefix=1 #除去第一個/前綴,比如請求/wisdomclass-demo/demo,會去除前綴/wisdomclass-demo,請求到路由服務的 /demo接口
- RemoveRequestHeader=Origin # 去除請求頭的origin字段,此字段導致post請求 無法進入網關post熔斷
- name: SpecialHystrix #自定義熔斷
args:
id: SpecialHystrix
fallbackUri: forward:/fallback
timeout:
#指定接口超時處理
file-upload-convert: 20000
file-upload-: 20000
file-download-: 20000
course-file-upload-local-: 20000
ribbon設置時間長一些20s
ribbon:
ReadTimeout: 20000 #ribbon讀取超時時間,接口處理時間,不包括建立連接時間
ConnectTimeout: 3000 #ribbon請求連接時間
OkToRetryOnAllOperations: false #網關默認開啓重試,此屬性設置爲false 只對GET請求重試,保證冪等性
MaxAutoRetries: 1 #Max number of retries on the same server (excluding the first try)
MaxAutoRetriesNextServer: 1 #Max number of next servers to retry (excluding the first server)
ServerListRefreshInterval: 3000 # refresh the server list from the source
hystrix 設置默認超時時間2s,這樣除特殊接口外,超時時間都很短
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 2000 #超過此時間後進入熔斷,這個時間應該大於後端及其ribbon的時間,否則後端接口未執行完就進入熔斷
2.下游服務
hystrix,設置默認超時時間 HystrixCommandKey爲default,設置特殊接口超時時間,HystrixCommandKey爲類名#方法名(參數類型,參數類型)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000 #超過此時間後進入熔斷
CourseFileRemote#uploadByLocal(MultipartFile,Long):
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 20000
FilesRemote#fileUpload(MultipartFile,String):
execution:
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 20000
ribbon的時間也長一些
ribbon:
ReadTimeout: 20000 #ribbon讀取超時時間,接口處理時間,不包括建立連接時間
ConnectTimeout: 3000 #ribbon請求連接時間
OkToRetryOnAllOperations: false #網關默認開啓重試,此屬性設置爲false 只對GET請求重試,保證冪等性
MaxAutoRetries: 1 #Max number of retries on the same server (excluding the first try)
MaxAutoRetriesNextServer: 1 #Max number of next servers to retry (excluding the first server)
ServerListRefreshInterval: 3000 # refresh the server list from the source