Spring-云 Zuul Ignore/Include 服务
Spring-Cloud Zuul Ignore/Include Services
是否可以在 zuul.ignoredServices 中使用负模式?
我想对只有 name/virtualHostName 作为 "hrerp*".
的服务进行负载平衡
我可以在 zuul.routes 中明确定义这些。还有其他可能性吗?
不,尚不支持否定模式。欢迎提出请求。
作为替补:
- 扩展
ProxyRouteLocator
(CustomProxyRouteLocator
) 与
已覆盖 locateRoutes()
locateRoutes
会考虑 ZuulProperties.ignoredServices
被包括在内。
- 用
@Primary
声明了 CustomProxyRouteLocator
个 bean
- 用
CustomProxyRouteLocator
启动了 PreDecorationFilter
个 bean,
和 @Primary
尝试这样的事情,它可以让您完全控制那些被阻止的请求的阻止和记录:
在您的属性中设置:
zuul:
blockedServices: 'admin, forbidden'
然后像这样创建过滤器 class:
@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);
}
}
是否可以在 zuul.ignoredServices 中使用负模式? 我想对只有 name/virtualHostName 作为 "hrerp*".
的服务进行负载平衡我可以在 zuul.routes 中明确定义这些。还有其他可能性吗?
不,尚不支持否定模式。欢迎提出请求。
作为替补:
- 扩展
ProxyRouteLocator
(CustomProxyRouteLocator
) 与 已覆盖locateRoutes()
locateRoutes
会考虑ZuulProperties.ignoredServices
被包括在内。
- 用
@Primary
声明了 - 用
CustomProxyRouteLocator
启动了PreDecorationFilter
个 bean, 和@Primary
CustomProxyRouteLocator
个 bean
尝试这样的事情,它可以让您完全控制那些被阻止的请求的阻止和记录:
在您的属性中设置:
zuul:
blockedServices: 'admin, forbidden'
然后像这样创建过滤器 class:
@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);
}
}