我正在尝试使用zuul在旧的Solr服务器上构建一个api网关。
我希望有多条路线,例如:
api-zuul/api1
api-zuul/api2
api-zuul/api3
重定向到相同的端点(Solr api),但根据调用传递不同的参数。
api-zuul/api1 => mysolrserver/api/select?&collection=api1&otherParams
api-zuul/api2 => mysolrserver/api/select?&collection=api2&otherParams
api-zuul/api3 => mysolrserver/api/select?&collection=api3&otherParams
目前我设法通过ZuulFilter将参数传递给一条路线:
public class ProjectParamFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(ProjectBodyFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Map<String, List<String>> params = new HashMap<>();
List<String> paramAll = new ArrayList<>();
List<String> paramTrue = new ArrayList<>();
List<String> paramJson = new ArrayList<>();
List<String> paramCollection = new ArrayList<>();
paramAll.add("*:*");
paramTrue.add("true");
paramJson.add("json");
paramCollection.add("Project");
params.put("q", paramAll);
params.put("indent", paramTrue);
params.put("wt", paramJson);
params.put("collection", paramCollection);
ctx.setRequestQueryParams(params);
log.info("passed params :" + ctx.getRequestQueryParams().toString());
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
return null;
}
}
由于zuul旨在处理多项服务,因此我无法理解如何根据传入请求为同一端点调用不同的过滤器。
答案 0 :(得分:0)
如果有人遇到同样的问题,我使用shouldFilter()函数解决了它。
在application.properties中添加路由:
zuul.routes.test.url = https://stackoverflow.com/
在过滤器中:
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
if (ctx.getRequest().getRequestURI().equals("/test")) {
return true;
} else {
return false;
}
}