spring-cloud / spring-cloud-netflix

Integration with Netflix OSS components
http://cloud.spring.io/spring-cloud-netflix/
Apache License 2.0
4.87k stars 2.44k forks source link

RequestContext.set("requestURI", "") is not routing to correct micro-services #3266

Closed aswinde03032008 closed 6 years ago

aswinde03032008 commented 6 years ago

Here is my filter class which extends ZuulFilter `@Override public String filterType() { return "pre"; }

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

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

@Override
public Object run() {

    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String requestUri = request.getRequestURI().split(";")[0];
    log.info("@gateway -> Request start  ->{}", requestUri);

    if(clientPortalURIs.contains(requestUri)) {
        if(request.getHeader("http-user") != null && "cpTestUser".equals(request.getHeader("http-user"))){
            log.info("it is client-portal request so entitlment is not required.");
        }
        String url = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString().replace("navx", "navxcp")).build().toUriString();
        log.info("modified request-uri --------------->{}", url);
        ctx.put("requestURI", url);
    } else { 
                log.info("just forwarded here");
          }
  return null; 

bootstarp.yml: `zuul: ignoredServices: navxconfig add-proxy-headers: true sensitive-headers: Cookie,Set-Cookie routes: web: path: /navx/** service-id: navxgui stripPrefix: false

clientportal:
  path: /navxcp/**
  service-id: navxcp
  stripPrefix: false`

log.info("modified request-uri --------------->{}", url); -> it is printing correct but request is routed/forwarded to "navxgui" micro-service even if requestURI is "/navxcp". I was expecting request should be forwarded to "navxcp" micro-service

Can you please let me what goes wrong here?

ryanjbaxter commented 6 years ago

Please learn how to format code on GitHub.

If you want to change where Zuul routes a request the filter type should be route.

aswinde03032008 commented 6 years ago

Thank you for quick response! I was thinking if we update the requestURI in RequestContext of the pre filter then request will be routed based on request uri: Like:
zuul.routes.app1.path: /app1/** zuul.routes.app1.service-id: app-1

zuul.routes.app2.path: /app2/** zuul.routes.app2.service-id: app-2

Here all request with request-uri : /app1/ -> will be routed to app-1 microservice Here all request with request-uri : /app2/ -> will be routed to app-2 microservice

So I can make the changes from pre to route it will be working

aswinde03032008 commented 6 years ago

I was reading - https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html#zuul-developer-guide-sample-route-filter

How to Write a Pre Filter

As per document "To modify the path to which routing filters forward, set the REQUEST_URI_KEY."

ryanjbaxter commented 6 years ago

Maybe that should be moved to the next section? @spencergibb, what do you think?

aswinde03032008 commented 6 years ago

String requestUri = ctx.get(FilterConstants.REQUEST_URI_KEY).toString().replace("navx", "navxcp"); ctx.put(FilterConstants.REQUEST_URI_KEY, requestUri);

I did in run() of pre filter but request is not routed correctly: zuul.routes.cp.path: /navxcp/** zuul.routes.cp.service-id: navxcp

I noticed it is routed to below one -

zuul.routes.web.path: /navx/** zuul.routes.web.service-id: navxweb

aswinde03032008 commented 6 years ago

@Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1; }

I am making sure my pre-filter will be executed after PreDecorationFilter but still it is not routing to correct micro-service

ryanjbaxter commented 6 years ago

And what happens when you switch the type to route?

aswinde03032008 commented 6 years ago

No luck. it is same. /navxcp/** -> is routed to navxweb microservice actually I am hitting http://host:port/gateway/navx/getData Here getData is available on navxcp microservice So I am trying to change the requestURI in pre-filter so that it should be routed to navxcp

Now you may asking why I am hitting http://host:port/gateway/navx/getData rather I cloud hit http://host:port/gateway/navxcp/getData then it will be working fine.

But http://host:port/gateway/navx -> is Load Balancing URL for 'gateway' app. Gateway app is deployed in multiple servers

aswinde03032008 commented 6 years ago

String url = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString().replace("navx", "navxcp")).build().toUriString(); ctx.setRouteHost(new URL(url));

It is routing to navxcp but it is appending the /navx/getData Log:

2018-11-07 17:26:34 [http-nio-6705-exec-1] INFO c.j.c.f.n.g.filter.IDAAuthFillter - @gateway -> Request start ->/ng/navx/coreAccountAlertSummary 2018-11-07 17:26:34 [http-nio-6705-exec-4] INFO c.j.c.f.n.g.filter.IDAAuthFillter - @gateway -> Request start ->/ng/navxcp/coreAccountAlertSummary/navx/coreAccountAlertSummary

aswinde03032008 commented 6 years ago

if(!requestUri.contains("navxcp")) { String url = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString().replace("navx", "navxcp")).build().toUriString(); ctx.setRouteHost(new URL(url)); ctx.set(FilterConstants.REQUEST_URI_KEY, ""); log.info("Request is re-directed to -> {}", url); }

Log: 2018-11-07 18:50:29 [http-nio-6705-exec-1] INFO c.j.c.f.n.g.filter.PreFilter - @gateway -> Request start ->/ng/navx/coreAccountAlertSummary 2018-11-07 18:50:29 [http-nio-6705-exec-1] INFO c.j.c.f.n.g.filter.IDAAuthFillter - Request is re-directed to -> http://localhost.svr.us.jpmchase.net:6705/ng/appcp/coreAccountAlertSummary 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.j.c.f.n.g.filter.IDAAuthFillter - @gateway -> Request start ->/ng/navxcp/coreAccountAlertSummary 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO o.s.c.a.AnnotationConfigApplicationContext - Refreshing SpringClientFactory-navxcp: startup date [Wed Nov 07 18:50:29 EST 2018]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3ecd23d9 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.config.ChainedDynamicProperty - Flipping property: navxcp.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.u.c.ShutdownEnabledTimer - Shutdown hook installed for: NFLoadBalancer-PingTimer-navxcp 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.loadbalancer.BaseLoadBalancer - Client: navxcp instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=navxcp,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.l.DynamicServerListLoadBalancer - Using serverListUpdater PollingServerListUpdater 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.config.ChainedDynamicProperty - Flipping property: navxcp.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 2018-11-07 18:50:29 [http-nio-6705-exec-2] INFO c.n.l.DynamicServerListLoadBalancer - DynamicServerListLoadBalancer for client navxcp initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=navxcp,current list of Servers=[localhost.svr.us.jpmchase.net:6704],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;] },Server stats: [[Server:localhost.svr.us.jpmchase.net:6704; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Wed Dec 31 19:00:00 EST 1969; First connection made: Wed Dec 31 19:00:00 EST 1969; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0] ]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@25ff58c3 2018-11-07 18:50:30 [http-nio-6704-exec-1] INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-11-07 18:50:30 [http-nio-6704-exec-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started 2018-11-07 18:50:30 [http-nio-6704-exec-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms 2018-11-07 18:50:30 [http-nio-6705-exec-2] INFO c.j.c.f.n.g.filter.PostAuthFilter - Request End @gateway ->/ng/navxcp/coreAccountAlertSummary 2018-11-07 18:50:30 [http-nio-6705-exec-1] INFO c.j.c.f.n.g.filter.PostAuthFilter - Request End @gateway ->/ng/navx/coreAccountAlertSummary

ryanjbaxter commented 6 years ago

Random code is not helpful, please provide a sample so we have something concrete