Question
In my application I am using Eureka as a registry service and Zuul for Gateway.
In our application (DEMO - UI app) I am trying to access USERS app (another API application build by our team which is also registered in Eureka) . However it says 404 when i use contextPath to access the API but the same is working fine without contextPath. Please find the sample application.properties with few info.
server.port=9090
server.contextPath=/user
spring.application.name=DEMO
eureka.instance.preferIpAddress=true
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=600000
zuul.routes.hello.path=/user/hello**
zuul.routes.hello.url=USERS
zuul.routes.hello.stripPrefix=true
Not sure whether i am missing anything here.
Furthermore I could see both DEMO (UI app) and USERS (API app) are registered successfully in Eureka server.
Please help us to resolve this issue asap.
Comment From: elango-boopathy
Hi ,
I resolved this issue with following changes in application.properties. We have to remove "/user" from the Zuul path entry as its a contextPath already appended in URL.
server.port=9090
server.contextPath=/user
#zuul.servletPath=/user
spring.application.name=DEMO
eureka.instance.preferIpAddress=false
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone: http://localhost:8080/eureka/
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=600000
# This is for GET request
zuul.routes.hello.path=/hello**
zuul.routes.hello.url=USERS
zuul.routes.hello.stripPrefix=false
# This is for POST request
zuul.routes.helloservice.path=/postdata**
zuul.routes.helloservice.serviceId=USERS
zuul.routes.helloservice.servicePath=user/postdata**
zuul.routes.helloservice.stripPrefix=false
However we still should keep contextPath in javascript or from client side which makes the REST api call. For Example:-
_$scope.getRequest = function() {
console.log("I've been pressed!");
$http.get("/user/hello").then(
function mySuccess(response) {
$scope.myRes = response.data;
$scope.statuscode = response.status;
}, function myError(response) {
$scope.myRes = response.statusText;
});
};
});_
Comment From: yangzhiwei256
default DiscoveryClientRouteLocator don't acquire service regist context-path, we need to midified it, ZuulRoute define a stripPrefix value, so zuul doesnot replace match path, it works
problem code:
@Override
protected LinkedHashMap<String, ZuulRoute> locateRoutes() {
LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<>();
routesMap.putAll(super.locateRoutes());
if (this.discovery != null) {
Map<String, ZuulRoute> staticServices = new LinkedHashMap<>();
for (ZuulRoute route : routesMap.values()) {
String serviceId = route.getServiceId();
if (serviceId == null) {
serviceId = route.getId();
}
if (serviceId != null) {
staticServices.put(serviceId, route);
}
}
// Add routes for discovery services by default
List<String> services = this.discovery.getServices();
String[] ignored = this.properties.getIgnoredServices()
.toArray(new String[0]);
for (String serviceId : services) {
// Ignore specifically ignored services and those that were manually
// configured
String key = "/" + mapRouteToService(serviceId) + "/**";
if (staticServices.containsKey(serviceId)
&& staticServices.get(serviceId).getUrl() == null) {
// Explicitly configured with no URL, cannot be ignored
// all static routes are already in routesMap
// Update location using serviceId if location is null
ZuulRoute staticRoute = staticServices.get(serviceId);
if (!StringUtils.hasText(staticRoute.getLocation())) {
staticRoute.setLocation(serviceId);
}
}
if (!PatternMatchUtils.simpleMatch(ignored, serviceId)
&& !routesMap.containsKey(key)) {
// Not ignored
**routesMap.put(key, new ZuulRoute(key, serviceId))**;
}
}
}
if (routesMap.get(DEFAULT_ROUTE) != null) {
ZuulRoute defaultRoute = routesMap.get(DEFAULT_ROUTE);
// Move the defaultServiceId to the end
routesMap.remove(DEFAULT_ROUTE);
routesMap.put(DEFAULT_ROUTE, defaultRoute);
}
LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>();
for (Entry<String, ZuulRoute> entry : routesMap.entrySet()) {
String path = entry.getKey();
// Prepend with slash if not already present.
if (!path.startsWith("/")) {
path = "/" + path;
}
if (StringUtils.hasText(this.properties.getPrefix())) {
path = this.properties.getPrefix() + path;
if (!path.startsWith("/")) {
path = "/" + path;
}
}
values.put(path, entry.getValue());
}
return values;
}