0

I am having few spring boot microservices, which are deployed to JBoss over a cloud environment. These boot services are Eureka clients which register itself in to the Eureka server. Following is an example:

eureka:
  client:
    healthcheck:
        enabled: true
    serviceUrl:
       defaultZone: ${DISCOVERY_URL:http://localhost:8761}/eureka/


  instance:
    ip-address: 127.0.0.1
    appname: user-regn-service-app
    home-page-url-path: /user-regn-service-app

It registers the app with Eureka with the name user-regn-service-app Eureka Homepage

The wildfly server is running at 8080 and the user-regn-service-app is deployed at the context path /user-regn-service-app. So the rest api is as below

localhost:8080/user-regn-service-app/regnUser

When I am using zuul as api gateway, the config is as below

zuul:
  prefix: /api
  routes:
    test:
      path: /test/**
      service-id: USER-REGN-SERVICE-APP
      strip-prefix: true


ribbon:
  eureka:
    enabled: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    registerWithEureka: false

But whenever I am making call to zuul api gateway it is unable to recognize the context path and redirects to localhost:8080 instead of localhost:8080/user-regn-service-app.

http://localhost:8765/api/ -> 404 not found

http://localhost:8765/api/user-regn-service-app/ -> Wildfly default homepage

http://localhost:8765/api/user-regn-service-app/user-regn-service-app/regnUser -> Redirects to user registration.

Expected behavior: http://localhost:8765/api/test/regnUser should redirect to the user registration.

I have pretty much tried all combinations that I got from blogs between Zuul and Eureka to get the following done but no luck. Kindly advise if I am missing something.

I have tried using custom zuul custom filter as below but it doesn't forward to the Required Context path. Code is as below

@Component
public class ZuulApiFilter extends ZuulFilter{

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        System.out.println("original"+ ctx.get("requestURI"));
        HttpServletRequest request = ctx.getRequest();
        String requestURI = request.getRequestURI();
        String contextAwareURI=requestURI.concat("user-regn-service-app/");
        ctx.set("requestURI", contextAwareURI);
        return null;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public String filterType() {
        return "pre";
    }

}

The requestURI doesn't changes after setting the new URI as well ctx.set("requestURI", contextAwareURI); request.getRequestURI(); shows the old Request URI only.

Tanmay Ghosh
  • 1
  • 1
  • 3

1 Answers1

0

Could you remove ZuulApiFilter and try setting:

strip-prefix: false

and try sending the request to:

http://<zuul ip>:<zuul port>/api/test/....

Edited:

@Tanmay Ghosh

Here is a sample code I'm using for Zuul-related blog post (still in draft) that I'll publish in the next couple of days:

Zuul's application.yml:

...
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

# ribbon.eureka.enabled: false
zuul:
  ignoredServices: "*"
  routes:
    zuulDemo1:
      path: /zuul1/**
# serviceId as registed with Eureka. Enabled and used when ribbon.eureka.enabled is true.
      serviceId: demo-zuul-api1
# zuul.routes.<the route>.url used when ribbon.eureka.enabled is false, serviceId is disabled.
#      url: http://localhost:8600/
# stripPrefix set to true if context path is set to /
      stripPrefix: true
...

And actually my Zuul server repo is public and available at: https://bitbucket.org/asimio/zuulserver and a recent blog post at http://tech.asimio.net/2017/10/10/Routing-requests-and-dynamically-refreshing-routes-using-Spring-Cloud-Zuul-Server.html

Another thing, Does the Zuul service also uses an app context other than / ? If so, Could you try sending the request via Zuul at: http://<zuul host>:<zuul port>/<zuul app context>/api/test/.... ?

ootero
  • 3,235
  • 2
  • 16
  • 22
  • If i make it false i can access the below http://localhost:8765/api/user-regn-service-app/ -> Wildfly homepage http://localhost:8765/api/user-regn-service-app/user-regn-service-app/userRegn ->Service Not sure if any config is missing – Tanmay Ghosh Oct 05 '17 at 16:58
  • This is confusing, are you using JBoss and Weblogic? If the API is listening at `localhost:8080` and also using app context `user-regn-service-app` it means requests should be sent to `http://localhost:8080/user-regn-service-app/`. stripPrefix is set to true if context path of the API (not Zuul service) is set to / – ootero Oct 05 '17 at 17:06
  • Sorry for the confusion,I am using Jboss Wildfly for this service and it is deployed at the context root /user-regn-service-app at 8080. http://localhost:8080/user-regn-service-app/ works fine, but zuul is unable to redirect to the given service. Am I missing something in the config? – Tanmay Ghosh Oct 05 '17 at 17:16
  • Setting `strip-prefix: true` causes `Zuul` to proxy the request to the API service without the app context, meaning `Zuul` is most-likely sending the request to `http://localhost:8080/` – ootero Oct 05 '17 at 18:26
  • I have updated my answer with some sample code I'm using for a Zuul-related blog post I'll be publishing either today or tomorrow – ootero Oct 05 '17 at 18:29
  • Thank for your response.. i will try it to see if it works well for me. My Zuul server is working at root context only at port 8765 which is using service id from Eureka server running at 8761. – Tanmay Ghosh Oct 05 '17 at 19:40