Java源码示例:com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager
示例1
@Override
public void afterSingletonsInstantiated() {
Map<String, ZuulBlockFallbackProvider> providerMap = beanFactory
.getBeansOfType(ZuulBlockFallbackProvider.class);
if (!CollectionUtils.isEmpty(providerMap)) {
providerMap.forEach((k, v) -> {
logger.info("[Sentinel Zuul] Register provider name:{}, instance: {}", k,
v);
ZuulBlockFallbackManager.registerProvider(v);
});
}
else {
logger.info("[Sentinel Zuul] Register default fallback provider. ");
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
}
}
示例2
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
String origin = parseOrigin(ctx.getRequest());
String routeId = (String)ctx.get(ZuulConstant.PROXY_ID_KEY);
Deque<AsyncEntry> asyncEntries = new ArrayDeque<>();
String fallBackRoute = routeId;
try {
if (StringUtil.isNotBlank(routeId)) {
ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId, origin);
doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, ctx, asyncEntries);
}
Set<String> matchingApis = pickMatchingApiDefinitions(ctx);
if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) {
ContextUtil.enter(ZuulConstant.ZUUL_DEFAULT_CONTEXT, origin);
}
for (String apiName : matchingApis) {
fallBackRoute = apiName;
doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, ctx, asyncEntries);
}
} catch (BlockException ex) {
ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(
fallBackRoute);
BlockResponse blockResponse = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, ex);
// Prevent routing from running
ctx.setRouteHost(null);
ctx.set(ZuulConstant.SERVICE_ID_KEY, null);
// Set fallback response.
ctx.setResponseBody(blockResponse.toString());
ctx.setResponseStatusCode(blockResponse.getCode());
// Set Response ContentType
ctx.getResponse().setContentType("application/json; charset=utf-8");
} finally {
// We don't exit the entry here. We need to exit the entries in post filter to record Rt correctly.
// So here the entries will be carried in the request context.
if (!asyncEntries.isEmpty()) {
ctx.put(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY, asyncEntries);
}
}
return null;
}
示例3
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
String origin = parseOrigin(ctx.getRequest());
String routeId = (String)ctx.get(ZuulConstant.PROXY_ID_KEY);
Deque<EntryHolder> holders = new ArrayDeque<>();
String fallBackRoute = routeId;
try {
if (StringUtil.isNotBlank(routeId)) {
ContextUtil.enter(GATEWAY_CONTEXT_ROUTE_PREFIX + routeId, origin);
doSentinelEntry(routeId, RESOURCE_MODE_ROUTE_ID, ctx, holders);
}
Set<String> matchingApis = pickMatchingApiDefinitions(ctx);
if (!matchingApis.isEmpty() && ContextUtil.getContext() == null) {
ContextUtil.enter(ZuulConstant.ZUUL_DEFAULT_CONTEXT, origin);
}
for (String apiName : matchingApis) {
fallBackRoute = apiName;
doSentinelEntry(apiName, RESOURCE_MODE_CUSTOM_API_NAME, ctx, holders);
}
} catch (BlockException ex) {
ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(
fallBackRoute);
BlockResponse blockResponse = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, ex);
// Prevent routing from running
ctx.setRouteHost(null);
ctx.set(ZuulConstant.SERVICE_ID_KEY, null);
// Set fallback response.
ctx.setResponseBody(blockResponse.toString());
ctx.setResponseStatusCode(blockResponse.getCode());
// Set Response ContentType
ctx.getResponse().setContentType("application/json; charset=utf-8");
} finally {
// We don't exit the entry here. We need to exit the entries in post filter to record Rt correctly.
// So here the entries will be carried in the request context.
if (!holders.isEmpty()) {
ctx.put(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY, holders);
}
}
return null;
}