In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.