Introducing Zuul
Zuul's processing of Http requests is based on SpringMVC. You must have noticed that when you build a zuul and configure the back-end to map requests /apps/** to your back-end services, regardless of /apps/** ** or /zuul/apps/**** can reach your backend service.
So how does this arrive?
ZuulServlet
Zuul has a self-made Servlet -- ZuulServlet , which contains the main branches of all Zuul processing processes. It will not be introduced in detail here. Later chapters will introduce Zuul's processing processes in detail.
SpringBoot has a ServletRegistrationBean dedicated to registering custom servlets.
public ServletRegistrationBean zuulServlet() {
ServletRegistrationBean servlet = new ServletRegistrationBean(new ZuulServlet(),
this.zuulProperties.getServletPattern());
servlet.addInitParameter("buffer-requests", "false");
return servlet;
}
There is also a use of defining roadbed path, the default is /zuul, you can define it as other through the configuration file, change it to /api as follows
zuul.servletPath=/api
If you don't use SpringBoot, then in fact, the old method only needs to configure ZuulServlet in web.xml, and the path is also configured in web.xml.
ZuulHandlerMapping
Let's see which faction this is from.
RequestMappingHandlerMapping is not unfamiliar. SpringMVC uses this by default. It will capture all @Controller and @RequetMapping annotated methods to handle different URLs.
Before SpringMVC3.1, the above class was not actually used, but the faction of AbstractUrlHandlerMapping - DefaultAnnotationHandlerMapping , this class is a faction of ZuulHandlerMapping , and it is based on the URL to choose what to use for processing.
So what's the deal with it? The registerHandlers method in ZuulHandlerMapping answers this question
private void registerHandlers() {
Collection<Route> routes = this.routeLocator.getRoutes();
if (routes.isEmpty()) {
this.logger.warn("No routes found from RouteLocator");
}
else {
for (Route route : routes) {
registerHandler(route.getFullPath(), this.zuul);
}
}
}
what is this.zuul? ZuulController
public class ZuulController extends ServletWrappingController
It turned out that the servlet was packaged as a Controller, but it was actually ZuulServlet
public class ZuulController extends ServletWrappingController {
public ZuulController() {
setServletClass(ZuulServlet.class); // <<======= See Here!
setServletName("zuul");
setSupportedMethods((String[]) null); // Allow all
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
return super.handleRequestInternal(request, response);
}
finally {
RequestContext.getCurrentContext().unset();
}
}
}
summary:
It's clear how Zuul's Request came in. Let's analyze what happened after it came in.
For detailed code analysis, see SpringCloud's Zuul source code detailed notes