Skip to content

feat:support zuul circuit breaker fallback response. #1522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
- [fix:fix zuul delay circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1519)
- [fix:fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties.](https://github.com/Tencent/spring-cloud-tencent/pull/1520)
- [docs:update circuit breaker examples.](https://github.com/Tencent/spring-cloud-tencent/pull/1521)
- [feat:support zuul circuit breaker fallback response.](https://github.com/Tencent/spring-cloud-tencent/pull/1522)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -38,6 +39,7 @@
import com.tencent.cloud.polaris.router.RouterRuleLabelResolver;
import com.tencent.cloud.polaris.router.spi.ServletRouterLabelResolver;
import com.tencent.cloud.rpc.enhancement.zuul.EnhancedZuulPluginRunner;
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,6 +52,7 @@
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -196,7 +199,34 @@ private void init() {

@Override
public Object run() {
enhancedZuulPluginRunner.run();
return super.run();
RequestContext context = RequestContext.getCurrentContext();
// Run pre enhanced plugins.
try {
enhancedZuulPluginRunner.run();
}
catch (CallAbortedException e) {
if (e.getFallbackInfo() == null) {
throw e;
}
// circuit breaker fallback, not need to run post/exception enhanced plugins.
// set sendZuulResponse to false
context.setSendZuulResponse(false);
// set response status code
HttpStatus httpStatus = HttpStatus.resolve(e.getFallbackInfo().getCode());
context.setResponseStatusCode(httpStatus != null ? httpStatus.value() : HttpStatus.INTERNAL_SERVER_ERROR.value());
// set response body
String body = Optional.of(e.getFallbackInfo().getBody()).orElse("");
context.setResponseBody(body);
// set response content type
context.getResponse().setContentType("text/plain;charset=UTF-8");
// set response headers
if (com.tencent.polaris.api.utils.CollectionUtils.isNotEmpty(e.getFallbackInfo().getHeaders())) {
e.getFallbackInfo().getHeaders().forEach(context.getResponse()::addHeader);
}
}
if (context.sendZuulResponse()) {
return super.run();
}
return null;
}
}