【Java】Zuul- 启动

@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中。
【Java】Zuul- 启动

以上是 【Java】Zuul- 启动 的全部内容, 来源链接: utcz.com/a/87329.html

回到顶部