spring-attic / gs-routing-and-filtering

Routing and Filtering :: Learn how to route and filter requests to a microservice using Netflix Zuul
https://spring.io/guides/gs/routing-and-filtering
67 stars 85 forks source link

How to set zuul route dynamically? #14

Closed topsoft-support closed 4 years ago

topsoft-support commented 4 years ago

I have seen this guide, create demo for test ,and it works fine. https://spring.io/guides/gs/routing-and-filtering/ the application.properties:zuul.routes.skd.url=http://localhost:8081/skd

Now but my route for reverse proxy in my application will be set dynamically. I removed the zuul.routes.skd.url=http://localhost:8081/skd and do it like this:

if (path.startsWith("/skd/")) {
            ZuulProperties zuulProperties = applicationContext.getBean(ZuulProperties.class);
            System.out.println(zuulProperties);
            Map<String, ZuulProperties.ZuulRoute> routes = zuulProperties.getRoutes();
            ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
            zuulRoute.setId("skd");
            zuulRoute.setPath("/skd/**");
            zuulRoute.setUrl("http://localhost:8081/skd");
            zuulRoute.setStripPrefix(true)
            routes.put("skd", zuulRoute);
            zuulProperties.setRoutes(routes);
            filterChain.doFilter(servletRequest, servletResponse);
        }

but it doesn't work! anyone konw how to fix it?