Closed aswinde03032008 closed 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
.
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
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."
Maybe that should be moved to the next section? @spencergibb, what do you think?
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
@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
And what happens when you switch the type to route?
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
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
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
Random code is not helpful, please provide a sample so we have something concrete
Here is my filter class which extends ZuulFilter `@Override public String filterType() { return "pre"; }
bootstarp.yml: `zuul: ignoredServices: navxconfig add-proxy-headers: true sensitive-headers: Cookie,Set-Cookie routes: web: path: /navx/** service-id: navxgui 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?