@EnableZuulProxy
我們使用zuul的時候,就會用這個註解,這個主鍵的功能和Eureka Server一樣。import了ZuulProxyMarkerConfiguration類。
@EnableCircuitBreaker
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ZuulProxyMarkerConfiguration.class)
public @interface EnableZuulProxy {
}
ZuulProxyMarkerConfiguration類的主要作用是載入了ZuulProxyMarkerConfiguration#Marker類。
@Configuration(proxyBeanMethods = false)
public class ZuulProxyMarkerConfiguration {
@Bean
public Marker zuulProxyMarkerBean() {
return new Marker();
}
class Marker {
}
}
ZuulServerAutoConfiguration
我們從spring.factories看,可以看到他包括了ZuulServerAutoConfiguration和ZuulProxyAutoConfiguration,這兩個類能不能被載入,他的條件就是有沒有ZuulProxyMarkerConfiguration#Marker類。
zuulRefreshRoutesListener
這個類是ApplicationListener<ApplicationEvent>,當有ContextRefreshedEvent、RefreshScopeRefreshedEvent、RoutesRefreshedEvent、InstanceRegisteredEvent的時候,就會觸發重置,動態路由就是透過這個來實現的。
@Bean
public ApplicationListener<ApplicationEvent> zuulRefreshRoutesListener() {
return new ZuulRefreshListener();
}
zuulServlet
建立ZuulServlet,這個是HttpServlet,初始化會呼叫init方法,在ZuulServlet#init方法中,會建立一個ZuulRunner物件。
@Bean
@ConditionalOnMissingBean(name = "zuulServlet")
@ConditionalOnProperty(name = "zuul.use-filter", havingValue = "false",
matchIfMissing = true)
public ServletRegistrationBean zuulServlet() {
ServletRegistrationBean<ZuulServlet> servlet = new ServletRegistrationBean<>(
new ZuulServlet(), this.zuulProperties.getServletPattern());
// The whole point of exposing this servlet is to provide a route that doesn't
// buffer requests.
servlet.addInitParameter("buffer-requests", "false");
return servlet;
}
與zuulServlet類似的是ZuulServletFilter,預設沒有載入。
ZuulRouteApplicationContextInitializer
我們看過Ribbon - 懶載入就知道了,懶載入轉直接載入。
@Bean
@ConditionalOnProperty("zuul.ribbon.eager-load.enabled")
public ZuulRouteApplicationContextInitializer zuulRoutesApplicationContextInitiazer(
SpringClientFactory springClientFactory) {
return new ZuulRouteApplicationContextInitializer(springClientFactory,
zuulProperties);
}
ZuulFilterInitializer
先獲取ZuulFilter,然後例項化ZuulFilterInitializer,他有個@PostConstruct註解,所以會呼叫ZuulFilterInitializer#contextInitialized方法。
@Configuration(proxyBeanMethods = false)
protected static class ZuulFilterConfiguration {
@Autowired
private Map<String, ZuulFilter> filters;
@Bean
public ZuulFilterInitializer zuulFilterInitializer(CounterFactory counterFactory,
TracerFactory tracerFactory) {
FilterLoader filterLoader = FilterLoader.getInstance();
FilterRegistry filterRegistry = FilterRegistry.instance();
return new ZuulFilterInitializer(this.filters, counterFactory, tracerFactory,
filterLoader, filterRegistry);
}
}
ZuulFilterInitializer#contextInitialized
在這個方法中,比較重要的就是把ZuulFilter集合存入FilterRegistry的filters中。
@PostConstruct
public void contextInitialized() {
log.info("Starting filter initializer");
TracerFactory.initialize(tracerFactory);
CounterFactory.initialize(counterFactory);
for (Map.Entry<String, ZuulFilter> entry : this.filters.entrySet()) {
filterRegistry.put(entry.getKey(), entry.getValue());
}
}
總結
所以zuul啟動的時候,就會把ZuulFilter集合存入FilterRegistry的filters中。