一、Spring Config
1、通过BootstrapListener加载BootstrapConfiguration配置文件
2、DiscoveryClientConfigServiceBootstrapConfiguration生成后,通过Bean的@EventListener重写url地址
3、通过ConfigServiceBootstrapConfiguration加载ConfigServicePropertySourceLocator类,之后通过SpringBoot初始化方法执行ConfigServicePropertySourceLocator类locate方法,通过restTemplate从Spring cloud Config服务获取配置信息。
二、Spring Ribbon
1、通过RibbonAutoConfiguration注入负载客户端RibbonLoadBalancerClient、规则类ZoneAvoidanceRule、负载执行类ZoneAwareLoadBalancer。
2、通过LoadBalancerAutoConfiguration注入拦截器LoadBalancerInterceptor,在使用restTemplate时选择负载服务器。
3、Ribbon基于LoadBalancerInterceptor方式,在request请求时进行选择。
三、Spring Feign
1、通过启动类@EnableFeignClients将所有路径中包含@FeignClient的类注入,注入的BeanClass类型为FeignClientFactoryBean。
2、在其他类调用Feign接口时,通过FeignClientFactoryBean.getObject方法返回生成的对象。对象中通过创建代理类最后执行SynchronousMethodHandler.invoke方法。该方法最后通过Ribbon底层选取负载服务器。Feign基于Spring Bean对象注入,在Controller中注入Bean时会通过FeignClientFactoryBean来获取Bean对象,并通过动态代理方式进入invoke方法最后找到ZoneAwareLoadBalancer来进行调用。
3、Feign底层最后调用规则还是借助Ribbon的ZoneAwareLoadBalancer来选择具体服务。
四、Spring Zuul
1、Zuul的核心是一系列的filters
2、通过ZuulProxyAutoConfiguration装载DiscoveryClientRouteLocator类主要作用是发现路由配置等,并组装成Map<String, ZuulRoute>形式。PreDecorationFilter类主要是执行preFilter。filter类型是pre。RibbonRoutingFilter类执行routeFilter中。filter类型是route。
3、通过ZuulServerAutoConfiguration装载ZuulController是负责请求过来处理请求的Controller。装载ZuulHandlerMapping主要是处理DispatcherServlet请求访问过来的时候,需要AbstractHandlerMapping处理getHandlerInternal方法寻找handler时用的,找到ZuulController。
4、ZuulControllor在处理请求后,最终会调用ZuulServlet的service方法。处理preRoute方法找到对应的路由。
五、Spring Sleuth ZipKin
1、通过TraceFillter创建Span,创建完毕后通过finally方法判断是否需要发送zipKinServer。
2、通过创建Sampler类设置抽样类型,有四种Bean,AlwaysSampler、IsTracingSampler、NeverSampler、PercentageBasedSampler(默认)。
3、通过TraceRestTemplateInterceptor,将需要继续调用其他微服务的Span信息加入到requestHeader中。
4、通过AsyncReporter进行异步发送,每秒扫描一次是否有信息。