Get the App
SLTechnology News&Howtos  ›  Servers  › 

Zuul custom service level and api level routing whitelist in spring-cloud

Shulou Source: shulou.com Published: 2022-06-02 04:59:11 10月04日 Update

It mainly implements the routing of service-level or api-level gateways in the whitelist.

I. Service and api level routing

Gateway routing at 1.service level

Public class ServiceIdWhiteTableRouteLocator extends DiscoveryClientRouteLocator {. / / mainly overrides this method. After calling the locateRoutes of super, the method is compared with the whitelist to extract the intersection @ Override protected LinkedHashMap locateRoutes () {LinkedHashMap routeMaps = super.locateRoutes (); LinkedHashMap whiteRouteMaps = new LinkedHashMap () RouteMaps.forEach ((k, v)-> {if (PatternMatchUtils.simpleMatch (whites, v.getServiceId ()) {whiteRouteMaps.put (k, v);}}); for (ZuulProperties.ZuulRoute route: this.properties.getRoutes (). Values ()) {whiteRouteMaps.put (route.getPath (), route);} return whiteRouteMaps }...}

Gateway routing at 2.api level

Public class PathWhiteTableHandleMapping extends ZuulHandlerMapping {. / / mainly rewrites this method, adding the logic @ Override protected Object lookupHandler (String urlPath, HttpServletRequest request) throws Exception {if (this.errorController! = null & & urlPath.equals (this.errorController.getErrorPath () {return null;} if (isIgnoredPath (urlPath, this.routeLocator.getIgnoredPaths ()) return null) to the original Whitelist. / * check whether it is in the whitelist, and do not route if it is not in the whitelist * / if (! isInPathWhiteTables (urlPath, this.pathWhites)) return null; RequestContext ctx = RequestContext.getCurrentContext (); if (ctx.containsKey ("forward.to")) {return null } if (this.dirty) {synchronized (this) {if (this.dirty) {registerHandlers (); this.dirty = false;} return super.lookupHandler (urlPath, request) } private boolean isInPathWhiteTables (String urlPath, Collection pathWhites) {for (String whitePath: pathWhites) {if (this.pathMatcher.match (whitePath, urlPath)) {return true;}} return false;} public void setPathWhiteTables (Collection whites) {this.pathWhites = whites;}...} di config configuration

1. First uninstall the auto config that comes with zuul.

@ SpringBootApplication (exclude = ZuulProxyAutoConfiguration.class)

two。 Three classes of copy are required.

Org.springframework.cloud.netflix.zuul.RibbonCommandFactoryConfiguration

Org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration

Org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration

Then modify the bean registration of zuulHandlerMapping in ZuulServerAutoConfiguration:

@ Bean (value = "discoveryRouteLocator") public DiscoveryClientRouteLocator discoveryClientRouteLocator (ServerProperties server, DiscoveryClient discovery) {/ / service whitelist injection point return new ServiceIdWhiteTableRouteLocator (server.getServlet (). GetServletPrefix (), discovery, this.zuulProperties, whiteRouteProperties.getWhiteServices ());} @ Bean (value = "zuulHandlerMapping") public ZuulHandlerMapping zuulHandlerMapping (RouteLocator routes) {PathWhiteTableHandleMapping mapping = new PathWhiteTableHandleMapping (routes, zuulController ()); mapping.setErrorController (this.errorController) / / path whitelist entry point mapping.setPathWhiteTables (whiteRouteProperties.getWhitePaths ()); return mapping;}

WhiteRouteProperties is a property class that loads configuration properties, which can be defined by yourself. ZuulProxyAutoConfiguration needs to change its parent class to the ZuulServerAutoConfigurationn described above.

three。 Profile configuration

It is mainly added to the application.yaml file:

Zuul: # Control service level whitelist (list) white-services:-'hello-server' # Control api level whitelist (list) white-paths: -' / hello/world' routes:-url: hello-server path: / hello/** # all routes are not routed by default ignored-services:'*'

The above configuration can route / hello/** the pattern request to the hello-server. Because the default settings are not routed, it is added through zuul.routes (see source code). Then, because the whitelist feature is set, you need to add hello-server to the white-services, while white-paths mainly controls which specific api in a service in the whitelist can be routed. As can be seen above, only / hello/world can be routed.

In this way, multi-dimensional whitelist routing is realized.

If there are any deficiencies, please do not hesitate to comment.

Tags: List route level configuration gateway control properties files methods processing don't hesitate to comment three intersection function foundation as above is in source code path logic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Shulou Tech Info NVidia MySQL