zuul.ignoredServices 是否有可能出现负面模式?我想对只有名称/virtualHostName 为“hrerp*”的服务进行负载平衡。
我可以在 zuul.routes 中明确定义这些。还有其他可能吗?
zuul.ignoredServices 是否有可能出现负面模式?我想对只有名称/virtualHostName 为“hrerp*”的服务进行负载平衡。
我可以在 zuul.routes 中明确定义这些。还有其他可能吗?
作为替代:
ProxyRouteLocator
( )CustomProxyRouteLocator
locateRoutes()
locateRoutes
将被ZuulProperties.ignoredServices
视为要包括在内的。CustomProxyRouteLocator
bean 与@Primary
PreDecorationFilter
beanCustomProxyRouteLocator
@Primary
尝试这样的事情,它可以让您完全控制阻止和记录那些被阻止的请求:
在您的属性中设置:
zuul:
blockedServices: 'admin, forbidden'
然后像这样创建一个过滤器类:
@Component
@Slf4j
public class BlockingFilter extends ZuulFilter {
@Value("#{'${zuul.blockedServices}'.replace(' ', '').split(',')}")
private List<String> blockedServices;
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
if (isBlockedLocation()) {
blockCurrentRequest();
}
return null;
}
private boolean isBlockedLocation() {
String requestUrl = RequestContext.getCurrentContext().getRequest().getRequestURL().toString();
Set<String> violations = blockedServices.stream()
.filter(s-> requestUrl.matches(".*" +s +".*"))
.collect(Collectors.toSet());
if (violations.size() > 0) {
log.warn("Blocked illegal request {} which violated rules {}",
requestUrl,
violations.stream().collect(Collectors.joining(" : ")));
return true;
}
return false;
}
/**
* If they attempt to get to an internal service report back not found
*/
private void blockCurrentRequest() {
RequestContext ctx = RequestContext.getCurrentContext();
//Set your custom error code
ctx.setResponseStatusCode(NOT_FOUND.value());
//Spring tends to use json for not found. This just sets a message.
ctx.setResponseBody("Page not found");
//Blocks this request from continued processing.
ctx.setSendZuulResponse(false);
}
}
不,尚不支持否定模式。欢迎拉取请求。