In many cases, due to restart the services, network downtime or poor, there will be abnormal routing Zuul, then, the exception information directly presented to the user unfriendly, we need to prompt a number of easy to understand why users would inform failure occurs, then you can use the rollback process, SpringCloud used Hystrix achieve fault tolerance and micro-services fallback, in fact Zuul default has been integrated Hystrix
This article explains how to implement a fallback strategy Zuul, Hystrix components do not know if we can go to my article: Hystrix SpringCloud component of
First, to achieve FallbackProvider Interface
Zuul service this paper is to build the previous articles, the service here is not to build Zuul, before the Zuul do not understand, then you can view the article: Zuul SpringCloud component of
/**
* @author Gjing
**/
@Component
public class GlobalFallback implements FallbackProvider {
/**
* 这里配置是为哪个服务提供回退,*号代表所有服务
*/
@Override
public String getRoute() {
return "*";
}
/**
* 回退返回
*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
@NonNull
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST.value();
}
@Override
@NonNull
public String getStatusText() throws IOException {
return HttpStatus.BAD_REQUEST.getReasonPhrase();
}
@Override
public void close() {
}
@Override
@NonNull
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("服务器异常请稍后再试".getBytes(StandardCharsets.UTF_8));
}
@Override
@NonNull
public HttpHeaders getHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return httpHeaders;
}
};
}
}
Correlation method described
Method name | Explanation |
---|---|
getRoute | Which provides a fallback for the service, on behalf of all services asterisk |
fallbackResponse | Fallback response |
getStatusCode | Return status code of retirement |
getRawStatusCode | Numeric status code |
getStatusText | Status Text |
close | This not control |
getBody | Response Body |
getHeaders | Response header returned |
Second, the configuration timeout
# 负载均衡超时时间设置
ribbon:
ReadTimeout: 读超时时间(单位毫秒)
socketTimeOut: 连接超时时间(单位毫秒)
note! ! ! : If zuul configured fallback if the fuse, the fuse must configure the timeout, or if you configure ribbon timeout is greater than the time-out fuse (Hystirx timeout default 1 second), then will go first blow, the equivalent of the ribbon with your timeout is not into effect, ribbon and hystrix is in effect at the same time, the value of which entered into force which small, the other can not see the effect .
hystrix:
command:
# default为默认所有,可以配置指定服务名
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 超时时间(单位毫秒)
Third, start Eureka, Zuul, and a Web service and web service interface setting thread sleep, analog time-out, when the timeout request actually reaches the set will be rolled back
This concludes the article, a reasonable timeout configuration and rollback, help make the program better experience Oh, concrete or depending on actual traffic on the scene Oh, this Demo source code for the address: SpringCloud-Demo