Original Address https://my.oschina.net/u/3300636/blog/851984?nocache=1491877235421
Zuul Open
First, the @enablezuulproxy annotation is the annotation that opens the Zuul.
@EnableCircuitBreaker
@EnableDiscoveryClient
@Target ({elementtype.type})
@Retention ( Retentionpolicy.runtime)
//introduction of Zuul configuration
@Import ({zuulproxyconfiguration.class}) public
@interface Enablezuulproxy {
}
Zuulproxyconfiguration inherited Zuulconfiguration.
This configuration is equivalent to beans in XML
@Configuration public
class Zuulproxyconfiguration extends Zuulconfiguration {
The configuration
is not loaded @Configuration @EnableConfigurationProperties ({zuulproperties.class})//Zuulservlet Loaded @ConditionalOnClass ({zuulservlet.class})
@Import ({serverpropertiesautoconfiguration.class})
Public Class Zuulconfiguration {
These two classes mainly load four types of things:
1, register Zuulservlet,zuulcontroller This is the entry that is called after some column zuulfilter.
Zuulcontroller implements Servletwrappingcontroller
protected Modelandview handlerequestinternal (httpservletrequest Request, HttpServletResponse response) throws Exception {
Modelandview var3;
try {
//Call Here the method in Zuulservlet
var3 = super.handlerequestinternal (request, response);
} finally {
Requestcontext.getcurrentcontext (). unset ();
}
return var3;
}
2, loading Zuul various filter, such as Predecorationfilter, Ribbonroutingfilter, etc.
In this version (1.1.2.RELEASE), Zuul default load filter has: Pre type FILTER5, Route 3, Post 2, a total of 10 filter. The function of each filter is not detailed here.
In some scenarios, we want to disable some of the filters, what to do now. See this method on the Internet:
You can disable the filter for Simpleclassname by simply setting the zuul.<simpleclassname>.<filtertype>.disable=true. Take filter Org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter as an example, just set zuul.SendResponseFilter.post.disable =true, you can disable the filter.
3, load zuulfilterconfiguration, this is the configuration defined in the Zuulconfiguration. This configuration collects all the filter (s) from the Zuulfilterinitializer into the filterregistry. When invoked, the filter is taken from this.
@Configuration
protected Static class Zuulfilterconfiguration {
//all zuulfilter are injected into the map by type
@Autowired
private map<string, zuulfilter> filters;
Protected zuulfilterconfiguration () {
}
@Bean public
zuulfilterinitializer Zuulfilterinitializer () {
return new Zuulfilterinitializer (this.filters);
}
}
4, Load Zuulrefreshlistener, zuuldiscoveryrefreshlistener for dynamic refresh Zuul configuration
P.s. When the app is started, the load order is: Configuration config Conditionalonclass annotated bean "Configuration" Custom bean The Bean in the configuration of the bean in the configuration Zuul the filter call
The basic call order is not detailed, reproduced in the article described in more detail. Here's a little bit of zuul around here, please. In the Zuulservlet
public void Service (ServletRequest servletrequest, Servletresponse servletresponse) throws Servletexception,
IOException {try {this.init ((httpservletrequest) ServletRequest, (HttpServletResponse) servletresponse);
RequestContext e = Requestcontext.getcurrentcontext ();
E.setzuulengineran ();
try {this.preroute ();
} catch (Zuulexception var12) {this.error (var12);
This.postroute ();
Return
} try {This.route ();
} catch (Zuulexception Var13) {this.error (VAR13);
This.postroute ();
Return
} try {This.postroute ();
} catch (Zuulexception Var11) {this.error (VAR11); }} catch (Throwable var14) {this.error (new Zuulexception (Var14, $, "unhandled_exception_" + var14.getclass
(). GetName ())); } finally {//empty context Requestcontext.getcurrentcontext (). unset (); }
}
If the filter that executes the pre type throws Zuulexception, then the filter of the error type is executed, and then the filter of the post type is executed
If the filter that executes the route type throws Zuulexception, the filter of the error type is executed, and then the filter of the post type is executed
If the filter that executes the post type throws Zuulexception, the filter of the error type is executed
If an exception is thrown after throwing another exception, or after a catch zuulexception, the filter of the error type is executed
It's a bit around here, if you need to pay attention when using it. In addition Zuul many of the default filter, although Zuul developers are thoughtful, but in our own custom function, can also cause trouble.