0
votes

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.

1

1 Answers

0
votes

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/.... ?