1: How does Zuul make a gateway. Everyone, Baidu. There are many examples. For example, this article is a good introduction to the usage
https://blog.csdn.net/zhouhao88410234/article/details/88927686
2: What I want to introduce is the problems encountered by our system to realize the gateway.
Our system used to load routing with configuration files when it was started.
This transformation. We put the configuration in the database to load the database.
Because the previous system is relatively stable. So if after reconstruction, add a switch, if the switch is to take the configuration of the database.
We cut to the refactored code and went to the database. If the switch is configured. We just call the old code. Take the startup configuration route.
Inherited route loading is written like this. I thought it would be fine to write this way.
Later, it was discovered that when the configuration was taken, the routing in the startup configuration was merged with the database in the source code. This is where the routing is merged.
In addition to the route locators written by ourselves, there are also programs that are automatically loaded for us in routelocators. The DiscoveryClientRouteLocator class is the culprit. This route locator is not needed by us. That is, he will merge the routing. After discovering this problem. We began to think of ways not to load this class. It was found that the startup could not be eliminated at all. So we decided to rewrite this class. Do nothing inside. Specifically on the code.
a: In the startup class
@SpringBootApplication(scanBasePackages = "com.gateway",exclude = {ZuulProxyAutoConfiguration.class})
b: The program automatically loads this configuration, and then inherits ZuulProxyAutoConfiguration
@Configuration public class MyZuulProxyAutoConfiguration extends ZuulProxyAutoConfiguration { @Autowired protected ZuulProperties zuulProperties; @Bean public DiscoveryClientRouteLocator discoveryRouteLocator() { return new NopDiscoveryClientRouteLocator(null, null, zuulProperties, null, null); } }
c: Rewrite DiscoveryClientRouteLocator routing
public class NopDiscoveryClientRouteLocator extends DiscoveryClientRouteLocator { public NopDiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery, ZuulProperties properties, ServiceRouteMapper serviceRouteMapper, ServiceInstance localServiceInstance) { super(servletPath, discovery, properties, serviceRouteMapper, localServiceInstance); } @Override public void addRoute(String path, String location) { } @Override public void addRoute(ZuulRoute route) { } @Override public void refresh() { } @Override public List<Route> getRoutes() { return new ArrayList<>(); } @Override public Collection<String> getIgnoredPaths() { return new ArrayList<>(); } @Override public Route getMatchingRoute(String path) { return null; } @Override public int getOrder() { return 0; } @Override public void setOrder(int order) { } @Override protected LinkedHashMap<String, ZuulRoute> locateRoutes() { return new LinkedHashMap<>(); } @Override protected String mapRouteToService(String serviceId) { return null; } @Override protected void addConfiguredRoutes(Map<String, ZuulRoute> routes) { } @Override protected Map<String, ZuulRoute> getRoutesMap() { return new HashMap<>(); } @Override protected Route getSimpleMatchingRoute(String path) { return null; } @Override protected ZuulRoute getZuulRoute(String adjustedPath) { return null; } @Override protected Route getRoute(ZuulRoute route, String path) { return null; } @Override protected void doRefresh() { } @Override protected boolean matchesIgnoredPatterns(String path) { return false; } }