在zuul.ignoredServices中有可能有负面的模式吗?我想要负载平衡那些只有名称/virtualHostName为"hrerp*“的服务。
我可以在zuul.routes中显式地定义这些内容。还有其他的可能性吗?
发布于 2015-11-20 18:46:30
不,目前还不支持负面模式。拉请求欢迎。
发布于 2015-11-26 11:37:27
作为替代:
ProxyRouteLocator 的扩展locateRoutes() (CustomProxyRouteLocator)locateRoutes将考虑将ZuulProperties.ignoredServices作为要包含的内容。
CustomProxyRouteLocator bean与@PrimaryPreDecorationFilter CustomProxyRouteLocator和@Primary启动发布于 2018-10-02 12:02:02
尝试这样的方法,它可以让您完全控制阻塞请求的阻塞和日志记录:
在属性中设置如下内容:
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);
}
}https://stackoverflow.com/questions/33797121
复制相似问题