As the title suggests I want to change the url based on a given query parameter. So for example if I request:
http://api.company.com/path?service=A
>> route to upstreamA
http://api.company.com/path?service=B
>> route to upstreamB
I've tried multiple different versions based on this, this, this or that. None of these examples worked or provide a complete list environment (code + config files).
Current non working version:
application.yml:
zuul:
ignoredServices: '*'
routes:
serviceA:
path: /statistics/**
url: http://localhost:3000/a
serviceB:
path: /statistics/**
url: http://localhost:3000/b
PreFilter for Zuul
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import java.net.URL;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.*;
public class QueryParamServiceIdPreFilter extends ZuulFilter {
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER + 1;
}
public String filterType() {
return PRE_TYPE;
}
@Override
public boolean shouldFilter() {
return true;
}
public Object run() {
RequestContext ctx = getCurrentContext();
HttpServletRequest request = ctx.getRequest();
// always null
URL routeHost = getCurrentContext().getRouteHost();
if ( "A".equals(request.getParameter("serviceUrl")) ) {
ctx.put("serviceId", "serviceA");
}
if ( "B".equals(request.getParameter("serviceUrl")) ) {
ctx.put("serviceId", "serviceB");
}
return null;
}
}
Fake micro service:
var express = require('express');
var app = express();
app.get('/a', function (req, res) {
res.send('Service A');
});
app.get('/b', function (req, res) {
res.send('Service B');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Result on call
$ curl "localhost:8071/statistics?serviceUrl=B" ; echo; curl "localhost:8071/statistics?serviceUrl=A"
Service B
Service B⏎