zuul.ignoredServices で否定的なパターンを持つことは可能ですか? name/virtualHostName が「hrerp*」のみのサービスを負荷分散したい。
これらは zuul.routes で明示的に定義できます。他の可能性はありますか?
zuul.ignoredServices で否定的なパターンを持つことは可能ですか? name/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);
}
}