Spring Cloud Zuul 源码解析

本文涉及的产品
网络型负载均衡 NLB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: Spring Cloud Zuul 源码解析

Zuul 架构图

Zuul的官方文档中的架构图
image.png
从架构图中可以看到 Zuul 通过Zuul Servlet 和 一系列 Zuul Filter 来完成智能路由和过滤器的功能。

Zuul 工作原理概述(转)

Zuul中,整个请求的过程是这样的,首先将请求给 ZuulServlet 处理,ZuulServlet中有一个ZuulRunner对象,该对象中初始化了RequestContext, RequestContext 作为整个请求的上下文,封装了请求的一些数据,并被所有的ZuulFilter共享。
ZuulRunner 中还有 FilterProcessorFilterProcessor作为执行所有的ZuulFilter的管理器。FilterProcessor FilterLoader 中获取ZuulFilter,而ZuulFilter是被FilterFileManager所加载,并支持groovy热加载,采用了轮询的方式热加载。
有了这些Filter之后,ZuulServlet首先执行的pre类型的过滤器,再执行route类型的过滤器,最后执行的是post 类型的过滤器。
如果在执行这些过滤器有错误的时候则会执行error类型的过滤器。执行完这些过滤器,最终将请求的结果返回给客户端。

Zuul 启动—源码分析

在程序的启动类上加@EnableZuulProxy 注解,我们可以使用Zuul 提供的功能了,该注解的源码为:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({ZuulProxyMarkerConfiguration.class})
public @interface EnableZuulProxy {
}

源码中,@EnableZuulProxy 引入了ZuulProxyMarkerConfiguration 配置类,跟踪ZuulProxyMarkerConfiguration 类:

public class ZuulProxyMarkerConfiguration {
    public ZuulProxyMarkerConfiguration() {
    }

    @Bean
    public ZuulProxyMarkerConfiguration.Marker zuulProxyMarkerBean() {
        return new ZuulProxyMarkerConfiguration.Marker();
    }

    class Marker {
        Marker() {
        }
    }
}

ZuulProxyMarkerConfiguration 配置类中,发现只是注册了一个ZuulProxyMarkerConfiguration.Markerbean。我们通过分析应该会有依赖这个bean的配置类。然后我们找到了 ZuulProxyAutoConfiguration 依赖了ZuulProxyMarkerConfiguration.Markerbean
跟踪 ZuulProxyAutoConfiguration

@Configuration
@Import({RestClientRibbonConfiguration.class, OkHttpRibbonConfiguration.class, HttpClientRibbonConfiguration.class, HttpClientConfiguration.class})
@ConditionalOnBean({Marker.class})
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
  
    @Bean
    @ConditionalOnMissingBean({DiscoveryClientRouteLocator.class})
    public DiscoveryClientRouteLocator discoveryRouteLocator() {
        return new DiscoveryClientRouteLocator(this.server.getServlet().getContextPath(), this.discovery, this.zuulProperties, this.serviceRouteMapper, this.registration);
    }

    @Bean
    @ConditionalOnMissingBean({PreDecorationFilter.class})
    public PreDecorationFilter preDecorationFilter(RouteLocator routeLocator, ProxyRequestHelper proxyRequestHelper) {
        return new PreDecorationFilter(routeLocator, this.server.getServlet().getContextPath(), this.zuulProperties, proxyRequestHelper);
    }

    @Bean
    @ConditionalOnMissingBean({RibbonRoutingFilter.class})
    public RibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper, RibbonCommandFactory<?> ribbonCommandFactory) {
        RibbonRoutingFilter filter = new RibbonRoutingFilter(helper, ribbonCommandFactory, this.requestCustomizers);
        return filter;
    }

    @Bean
    @ConditionalOnMissingBean({SimpleHostRoutingFilter.class, CloseableHttpClient.class})
    public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties, ApacheHttpClientConnectionManagerFactory connectionManagerFactory, ApacheHttpClientFactory httpClientFactory) {
        return new SimpleHostRoutingFilter(helper, zuulProperties, connectionManagerFactory, httpClientFactory);
    }

    @Bean
    @ConditionalOnMissingBean({SimpleHostRoutingFilter.class})
    public SimpleHostRoutingFilter simpleHostRoutingFilter2(ProxyRequestHelper helper, ZuulProperties zuulProperties, CloseableHttpClient httpClient) {
        return new SimpleHostRoutingFilter(helper, zuulProperties, httpClient);
    }

    @Bean
    @ConditionalOnMissingBean({ServiceRouteMapper.class})
    public ServiceRouteMapper serviceRouteMapper() {
        return new SimpleServiceRouteMapper();
    }
}

我们发现在类ZuulProxyAutoConfiguration中,引入了RestClientRibbonConfiguration, OkHttpRibbonConfiguration, HttpClientRibbonConfiguration, HttpClientConfigurationZuul默认是用HttpClientRibbonConfiguration做负载均衡配置, 注入了DiscoveryClientRibbonConfiguration用作负载均衡相关。注入了一些列的Filter,
pre类型: PreDecorationFilter ; // 装饰 Request
route类型: RibbonRoutingFilter , SimpleHostRoutingFilter ; // 路由Filter
ZuulProxyAutoConfiguration 的父类ZuulServerAutoConfiguration中,也引入了一些配置信息:

@EnableConfigurationProperties({ZuulProperties.class})
@ConditionalOnClass({ZuulServlet.class, ZuulServletFilter.class})
@ConditionalOnBean({Marker.class})
public class ZuulServerAutoConfiguration {

    // 在缺失`ZuulServlet`的情况下注入`ZuulServlet`
    @Bean
    @ConditionalOnMissingBean(
        name = {"zuulServlet"}
    )
    @ConditionalOnProperty(
        name = {"zuul.use-filter"},
        havingValue = "false",
        matchIfMissing = true
    )
    public ServletRegistrationBean zuulServlet() {
        ServletRegistrationBean<ZuulServlet> servlet = new ServletRegistrationBean(new ZuulServlet(), new String[]{this.zuulProperties.getServletPattern()});
        servlet.addInitParameter("buffer-requests", "false");
        return servlet;
    }
    
    // 在缺失`ZuulServletFilter`的情况下注入`ZuulServletFilter`
    @Bean
    @ConditionalOnMissingBean(
        name = {"zuulServletFilter"}
    )
    @ConditionalOnProperty(
        name = {"zuul.use-filter"},
        havingValue = "true",
        matchIfMissing = false
    )
    public FilterRegistrationBean zuulServletFilter() {
        FilterRegistrationBean<ZuulServletFilter> filterRegistration = new FilterRegistrationBean();
        filterRegistration.setUrlPatterns(Collections.singleton(this.zuulProperties.getServletPattern()));
        filterRegistration.setFilter(new ZuulServletFilter());
        filterRegistration.setOrder(2147483647);
        filterRegistration.addInitParameter("buffer-requests", "false");
        return filterRegistration;
    }
    // 注入  `ServletDetectionFilter`
    @Bean
    public ServletDetectionFilter servletDetectionFilter() {
        return new ServletDetectionFilter();
    }

    // 注入  `FormBodyWrapperFilter`
    @Bean
    public FormBodyWrapperFilter formBodyWrapperFilter() {
        return new FormBodyWrapperFilter();
    }

    // 注入  `DebugFilter`
    @Bean
    public DebugFilter debugFilter() {
        return new DebugFilter();
    }

   // 注入  `Servlet30WrapperFilter`
    @Bean
    public Servlet30WrapperFilter servlet30WrapperFilter() {
        return new Servlet30WrapperFilter();
    }
    
  // 注入  `SendResponseFilter`
    @Bean
    public SendResponseFilter sendResponseFilter(ZuulProperties properties) {
        return new SendResponseFilter(this.zuulProperties);
    }

   // 注入  `SendErrorFilter`
    @Bean
    public SendErrorFilter sendErrorFilter() {
        return new SendErrorFilter();
    }
 
  // 注入  `SendForwardFilter`
    @Bean
    public SendForwardFilter sendForwardFilter() {
        return new SendForwardFilter();
    }
    
    
     @Configuration
    protected static class ZuulFilterConfiguration {
        @Autowired
        private Map<String, ZuulFilter> filters;

        protected ZuulFilterConfiguration() {
        }
       //  ZuulFilterInitializer,在初始化类中将Filter向FilterRegistry注册
        @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);
        }
    }
}

父类ZuulServerAutoConfiguration中,在缺失ZuulServletZuulServletFilterbean的情况下,注入ZuulServletZuulServletFilter。同时也注入了其他的过滤器,
pre类型: ServletDetectionFilterDebugFilterServlet30WrapperFilter ;
post类型: SendResponseFilter ; // 响应处理Filter
route类型: SendForwardFilter ; // 重定向处理Filter
error类型 : SendErrorFilter ; // 错误处理Filter

初始化ZuulFilterInitializer类,通过FilterLoader 将所有的FilterFilterRegistry注册。我们看一下ZuulFilterInitializer类中部分代码

public class ZuulFilterInitializer {
   
    // 初始化完成后注册所有的Filter
    @PostConstruct
    public void contextInitialized() {
        log.info("Starting filter initializer");
        TracerFactory.initialize(this.tracerFactory);
        CounterFactory.initialize(this.counterFactory);
        Iterator var1 = this.filters.entrySet().iterator();

        while(var1.hasNext()) {
            Entry<String, ZuulFilter> entry = (Entry)var1.next();
            this.filterRegistry.put((String)entry.getKey(), (ZuulFilter)entry.getValue());
        }

    }
    // 销毁前移除所有注册所有的Filter
    @PreDestroy
    public void contextDestroyed() {
        log.info("Stopping filter initializer");
        Iterator var1 = this.filters.entrySet().iterator();

        while(var1.hasNext()) {
            Entry<String, ZuulFilter> entry = (Entry)var1.next();
            this.filterRegistry.remove((String)entry.getKey());
        }

        this.clearLoaderCache();
        TracerFactory.initialize((TracerFactory)null);
        CounterFactory.initialize((CounterFactory)null);
    }
}

Zuul 路由-源码分析

Filter 的执行

我们站在了源码的角度分析了 Zuul启动过程。接下来我们通过源码来分析,我们注入的FilterZuul 在一次路由的过程是怎样的执行的。在上部分内容,我们介绍了Zuul的核心类 ZuulServlet是所有请求的入口,我们来进入 ZuulServlet的源码:

public class ZuulServlet extends HttpServlet {

      public ZuulServlet() {
      }
      // 在初始化方法里初始化了ZuulRunner
      public void init(ServletConfig config) throws ServletException {
          super.init(config);
          String bufferReqsStr = config.getInitParameter("buffer-requests");
          boolean bufferReqs = bufferReqsStr != null && bufferReqsStr.equals("true");
          this.zuulRunner = new ZuulRunner(bufferReqs);
      }
      // 标准的Servlet的service方法
      public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
          try {
              // 调用ZuulRunner的init方法
              this.init((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse);
              RequestContext context = RequestContext.getCurrentContext();
              context.setZuulEngineRan();

              try {
                 // 调用ZuulRunner的preRoute方法
                  this.preRoute();
              } catch (ZuulException var12) {
                  this.error(var12);
                 // 调用ZuulRunner的postRoute方法
                  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, 500, "UNHANDLED_EXCEPTION_" + var14.getClass().getName()));
          } finally {
              RequestContext.getCurrentContext().unset();
          }
      }
  }

ZuulServlet 源码中, service方法调用 this.init(request,response) , 跟进 init(request,response)方法:

  void init(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        this.zuulRunner.init(servletRequest, servletResponse);
    }

发现调用的是 ZuulServlet 持有的 ZuulRunnerinit方法,进入 ZuulRunnerinit方法:

 public void init(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        RequestContext ctx = RequestContext.getCurrentContext();
        if (this.bufferRequests) {
            ctx.setRequest(new HttpServletRequestWrapper(servletRequest));
        } else {
            ctx.setRequest(servletRequest);
        }

        ctx.setResponse(new HttpServletResponseWrapper(servletResponse));
    }

ZuulRunnerinit方法中,我们发现只是对Request,Response进行了有条件的包装。
我们回退到 ZuulServlet 的方法中,在执行完 init 方法后,调用 this.preRoute() , 跟进preRoute()方法:

  void preRoute() throws ZuulException {
        this.zuulRunner.preRoute();
    }

调用了 ZuulRunnerpreRoute()方法, 进入ZuulRunner.preRoute() :

   public void preRoute() throws ZuulException {
        FilterProcessor.getInstance().preRoute();
    }

ZuulRunner.preRoute() 中获取了一个FilterProcessor的实例并且执行了其preRoute() 方法,进入FilterProcessor.preRoute()方法:

 public void preRoute() throws ZuulException {
        try {
            this.runFilters("pre");
        } catch (ZuulException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new ZuulException(var3, 500, "UNCAUGHT_EXCEPTION_IN_PRE_FILTER_" + var3.getClass().getName());
        }
    }

FilterProcessor.preRoute()方法中,执行this.runFilters()方法并且传入参数pre,进入this.runFilters()方法中:

    public Object runFilters(String sType) throws Throwable {
        if (RequestContext.getCurrentContext().debugRouting()) {
            Debug.addRoutingDebug("Invoking {" + sType + "} type filters");
        }

        boolean bResult = false;
        //通过FilterLoader的实例获取所有pre的Filter
        List<ZuulFilter> list = FilterLoader.getInstance().getFiltersByType(sType);
        if (list != null) {
            for(int i = 0; i < list.size(); ++i) {
                ZuulFilter zuulFilter = (ZuulFilter)list.get(i);
                // 执行Filter
                Object result = this.processZuulFilter(zuulFilter);
                if (result != null && result instanceof Boolean) {
                    bResult |= (Boolean)result;
                }
            }
        }

        return bResult;
    }

this.runFilters()方法中通过FilterLoader的实例获取所有pre类型的Filter,并调用this.processZuulFilte(Filter) 执行所有pre类型的Filter
上述分析过程中我们了解了pre类型的Filter在一次路由中优先执行,我们通过一个简单的图了加深一下这个过程
image.png
这就是一个完整的Filter的执行过程,routepost 类型的Filter执行过程也是一致的。

Filter 路由

上部分内容我们了解Filter的执行入口,这部分内容我们来了解Zuul是怎么选择路由和负载均衡的。
在第一部分Zuul的启动过程中,Zuul 注入了pre类型的Filter,有PreDecorationFilter通过名字我们可以猜测,这个PreDecorationFilter是起到装饰Filter的作用,我们进入PreDecorationFilter.run()源码:

 public Object run() {
      RequestContext ctx = RequestContext.getCurrentContext();
      String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest());
      // 获取请求路径匹配的路由信息
      Route route = this.routeLocator.getMatchingRoute(requestURI);
      String location;
      if (route != null) {
          location = route.getLocation();
          if (location != null) {
              // 在RequextContext中放入请求路径,路由的标识
              ctx.put("requestURI", route.getPath());
              ctx.put("proxy", route.getId());
              if (!route.isCustomSensitiveHeaders()) {
                  this.proxyRequestHelper.addIgnoredHeaders((String[])this.properties.getSensitiveHeaders().toArray(new String[0]));
              } else {
                  this.proxyRequestHelper.addIgnoredHeaders((String[])route.getSensitiveHeaders().toArray(new String[0]));
              }
              //在RequextContext中放入该请求是否是可重试的标识
              if (route.getRetryable() != null) {
                  ctx.put("retryable", route.getRetryable());
              }

              if (!location.startsWith("http:") && !location.startsWith("https:")) {
                  if (location.startsWith("forward:")) {
                      ctx.set("forward.to", StringUtils.cleanPath(location.substring("forward:".length()) + route.getPath()));
                      ctx.setRouteHost((URL)null);
                      return null;
                  }
                 //设置路由配置的ServiceId
                  ctx.set("serviceId", location);
                  ctx.setRouteHost((URL)null);
                  ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
              } else {
                  ctx.setRouteHost(this.getUrl(location));
                  ctx.addOriginResponseHeader("X-Zuul-Service", location);
              }

        
          }
      } else {
          log.warn("No route found for uri: " + requestURI);
          location = this.getForwardUri(requestURI);
          ctx.set("forward.to", location);
      }

      return null;
  }

run方法中,获取和当前路径匹配的路由信息,将路由相关信息放入RequestContenxt中,路由信息是读取配置文件中的配置。并且放入了一个很重要的标识retryable这个就是决定我们这次请求是否可重试的开关,而这个读取配置文件中的zuul.retryable来决定的。
Zuul到底是怎么进行负载均衡的呢?
我们知道Zuul负载均衡底层是通过Ribbon来实现的,并且在启动Zuul的时候,我们注入了一个RibbonRoutingFilter的过滤器。这个类很重要,它主要是完成请求的路由转发。接下来我们看下他的 run方法

    public Object run() {
      RequestContext context = RequestContext.getCurrentContext();
      this.helper.addIgnoredHeaders(new String[0]);

      try {
          RibbonCommandContext commandContext = this.buildCommandContext(context);
          ClientHttpResponse response = this.forward(commandContext);
          this.setResponse(response);
          return response;
      } catch (ZuulException var4) {
          throw new ZuulRuntimeException(var4);
      } catch (Exception var5) {
          throw new ZuulRuntimeException(var5);
      }
  }

run中 可以看到,先构建了一个 RibbonCommandContext 然后通过forward()方法转发的,进入forward方法:


    protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
        Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(), context.getHeaders(), context.getParams(), context.getRequestEntity());
        RibbonCommand command = this.ribbonCommandFactory.create(context);

        try {
            ClientHttpResponse response = (ClientHttpResponse)command.execute();
            this.helper.appendDebug(info, response.getRawStatusCode(), response.getHeaders());
            return response;
        } catch (HystrixRuntimeException var5) {
            return this.handleException(info, var5);
        }
    }

forward中 可以看到,通过 RibbonCommandFactory 创建一个RibbonCommand,然后执行RibbonCommandexecute方法,
这个 RibbonCommandFactory 是什么时候注入的呢?
Zuul启动的时候,在 ZuulProxyAutoConfiguration 配置类引入了HttpClientRibbonConfiguration 配置类

@Configuration
@Import({RestClientRibbonConfiguration.class, OkHttpRibbonConfiguration.class, HttpClientRibbonConfiguration.class, HttpClientConfiguration.class})
@ConditionalOnBean({Marker.class})
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
 }

HttpClientRibbonConfiguration 中注入了 RibbonCommandFactory ,源码如下:

    @Configuration
    @RibbonCommandFactoryConfiguration.ConditionalOnRibbonHttpClient
    protected static class HttpClientRibbonConfiguration {
        @Autowired(
            required = false
        )
        private Set<FallbackProvider> zuulFallbackProviders = Collections.emptySet();

        protected HttpClientRibbonConfiguration() {
        }

        @Bean
        @ConditionalOnMissingBean
        public RibbonCommandFactory<?> ribbonCommandFactory(SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
            return new HttpClientRibbonCommandFactory(clientFactory, zuulProperties, this.zuulFallbackProviders);
        }
    }

这个时候我们就明白了 RibbonCommandFactory 是何时注入的了,
然后我们在看一下 RibbonCommandFactory.create() 创建RibbonCommand的方法:
image.png
Zuul 默认使用HttpClientRibbonCommandFactory,进入到create()方法:

   public HttpClientRibbonCommand create(final RibbonCommandContext context) {
       /**
          *服务降级
          */ 
       FallbackProvider zuulFallbackProvider = this.getFallbackProvider(context.getServiceId());
        String serviceId = context.getServiceId();
         /**
            *负载均衡类,处理请求转发类
        */
        RibbonLoadBalancingHttpClient client = (RibbonLoadBalancingHttpClient)this.clientFactory.getClient(serviceId, RibbonLoadBalancingHttpClient.class);
        client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));
        /**
          *将降级、负载,请求转发类、以及其他一些内容
          *包装成HttpClientRibbonCommand(这个类继承了HystrixCommand)
          */ 
       return new HttpClientRibbonCommand(serviceId, client, context, this.zuulProperties, zuulFallbackProvider, this.clientFactory.getClientConfig(serviceId));
    }

create 方法中,分别创建了FallbackProviderRibbonLoadBalancingHttpClient ,从命名上我们就可以知道, FallbackProvider和熔断相关,RibbonLoadBalancingHttpClient和负载均衡相关。然后将这些作为参数创建了HttpClientRibbonCommand 。我们看一下HttpClientRibbonCommand的继承关系。
企业微信20200409031803.png
从类继承关系可以看出HttpClientRibbonCommand继承了AbstractRibbonCommand,并且AbstractRibbonCommand继承了HystrixCommand
这样我们就了解到HystrixCommand是如何集成进来的了。
HystrixCommand中定义了run抽象接口,并且在AbstractRibbonCommand中实现了该接口。
我们回退到RibbonRoutingFilter

   protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
        Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(), context.getHeaders(), context.getParams(), context.getRequestEntity());
        RibbonCommand command = this.ribbonCommandFactory.create(context);

        try {
            ClientHttpResponse response = (ClientHttpResponse)command.execute();
            this.helper.appendDebug(info, response.getRawStatusCode(), response.getHeaders());
            return response;
        } catch (HystrixRuntimeException var5) {
            return this.handleException(info, var5);
        }
    }

当完成了RibbonCommand 创建工作后,执行的command.execute() 方法,通过刚刚的分析我们知道了command其实指的是HttpClientRibbonCommand,同时我们也知道HttpClientRibbonCommand继承了HystrixCommand,所以当执行command.execute()时,其实执行的是HttpClientRibbonCommandrun方法。查看源码我们并没有发现run方法,但是其父类AbstractRibbonCommand实现了run方法。所以其实执行的是AbstractRibbonCommandrun方法,进入AbstractRibbonCommandrun方法:

   protected ClientHttpResponse run() throws Exception {
        RequestContext context = RequestContext.getCurrentContext();
        RQ request = this.createRequest();
        boolean retryableClient = this.client instanceof AbstractLoadBalancingClient && ((AbstractLoadBalancingClient)this.client).isClientRetryable((ContextAwareRequest)request);
        HttpResponse response;
        if (retryableClient) {
            response = (HttpResponse)this.client.execute(request, this.config);
        } else {
            response = (HttpResponse)this.client.executeWithLoadBalancer(request, this.config);
        }

        context.set("ribbonResponse", response);
        if (this.isResponseTimedOut() && response != null) {
            response.close();
        }

        return new RibbonHttpResponse(response);
    }

run 方法中,先判断是否是重试的client,通过分析,第一次执行的时候,clientRibbonLoadBalancingHttpClient 从而会调用executeWithLoadBalancer() 方法,但是RibbonLoadBalancingHttpClient并没有executeWithLoadBalancer() 方法,查看而类继承关系图,
企业微信20200409034247.png

其父类AbstractLoadBalancerAwareClient实现了executeWithLoadBalancer() ,进入AbstractLoadBalancerAwareClient.executeWithLoadBalancer()方法:

    public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {

        LoadBalancerCommand command = this.buildLoadBalancerCommand(request, requestConfig);

        try {
            return (IResponse)command.submit(new ServerOperation<T>() {
                public Observable<T> call(Server server) {
                    URI finalUri = AbstractLoadBalancerAwareClient.this.reconstructURIWithServer(server, request.getUri());
                    ClientRequest requestForServer = request.replaceUri(finalUri);

                    try {
                        return Observable.just(AbstractLoadBalancerAwareClient.this.execute(requestForServer, requestConfig));
                    } catch (Exception var5) {
                        return Observable.error(var5);
                    }
                }
            }).toBlocking().single();
        } catch (Exception var6) {
            Throwable t = var6.getCause();
            if (t instanceof ClientException) {
                throw (ClientException)t;
            } else {
                throw new ClientException(var6);
            }
        }
    }

executeWithLoadBalancer 方法中,首先进入buildLoadBalancerCommand() 方法:

   protected LoadBalancerCommand<T> buildLoadBalancerCommand(S request, IClientConfig config) {
    /**
     * 创建一个RetryHandler,这个很重要它是用来
     * 决定利用RxJava的Observable是否进行重试的标准。
     */
        RequestSpecificRetryHandler handler = this.getRequestSpecificRetryHandler(request, config);
        Builder<T> builder = LoadBalancerCommand.builder().withLoadBalancerContext(this).withRetryHandler(handler).withLoadBalancerURI(request.getUri());
        this.customizeLoadBalancerCommandBuilder(request, config, builder);
        return builder.build();
    }

buildLoadBalancerCommand() 中通过了 getRequestSpecificRetryHandler()获取 RequestSpecificRetryHandler 的处理类,这个是个抽象方法,在子类AbstractLoadBalancingClient中实现

  public RequestSpecificRetryHandler getRequestSpecificRetryHandler(final S request, final IClientConfig requestConfig) {
        if (this.okToRetryOnAllOperations) {
            return new RequestSpecificRetryHandler(true, true, this.getRetryHandler(), requestConfig);
        } else {
            return !request.getContext().getMethod().equals("GET") ? new RequestSpecificRetryHandler(true, false, this.getRetryHandler(), requestConfig) : new RequestSpecificRetryHandler(true, true, this.getRetryHandler(), requestConfig);
        }
    }

在方法中我们可以看到重试的默认机制。若不配置ribbon.OkToRetryOnAllOperations=true, 默认只是连接失败GET请求失败才会发生重试。
回到executeWithLoadBalancer()方法中:

public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {
        LoadBalancerCommand command = this.buildLoadBalancerCommand(request, requestConfig);

        try {
            return (IResponse)command.submit(new ServerOperation<T>() {
                public Observable<T> call(Server server) {
                    URI finalUri = AbstractLoadBalancerAwareClient.this.reconstructURIWithServer(server, request.getUri());
                    ClientRequest requestForServer = request.replaceUri(finalUri);

                    try {
                        return Observable.just(AbstractLoadBalancerAwareClient.this.execute(requestForServer, requestConfig));
                    } catch (Exception var5) {
                        return Observable.error(var5);
                    }
                }
            }).toBlocking().single();
        } catch (Exception var6) {
            Throwable t = var6.getCause();
            if (t instanceof ClientException) {
                throw (ClientException)t;
            } else {
                throw new ClientException(var6);
            }
        }
    }

调用command.submit() ,创建了一个Observable (RxJava)并且最终会调用AbstractLoadBalancerAwareClient.execute ()方法

   public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request, final IClientConfig configOverride) throws Exception {
        IClientConfig config = configOverride != null ? configOverride : this.config;
        RibbonProperties ribbon = RibbonProperties.from(config);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(ribbon.connectTimeout(this.connectTimeout)).setSocketTimeout(ribbon.readTimeout(this.readTimeout)).setRedirectsEnabled(ribbon.isFollowRedirects(this.followRedirects)).setContentCompressionEnabled(ribbon.isGZipPayload(this.gzipPayload)).build();
        request = this.getSecureRequest(request, configOverride);
        HttpUriRequest httpUriRequest = request.toRequest(requestConfig);
        HttpResponse httpResponse = ((CloseableHttpClient)this.delegate).execute(httpUriRequest);
        return new RibbonApacheHttpResponse(httpResponse, httpUriRequest.getURI());
    }

execute()方法中,我们看到创建连接并且发送了http请求并将结果返回。
回退到submit()方法中,创建了Observable 会监听execute()的执行状态从而决定是否重试请求:

public Observable<T> submit(final ServerOperation<T> operation) {
        final LoadBalancerCommand<T>.ExecutionInfoContext context = new LoadBalancerCommand.ExecutionInfoContext();
        if (this.listenerInvoker != null) {
            try {
                this.listenerInvoker.onExecutionStart();
            } catch (AbortExecutionException var6) {
                return Observable.error(var6);
            }
        }
     /**
      * 相同server重试次数,去除首次
    */
        final int maxRetrysSame = this.retryHandler.getMaxRetriesOnSameServer();
        /**
          * 集群内其他server重试次数
        **/
        final int maxRetrysNext = this.retryHandler.getMaxRetriesOnNextServer();
        /**
        * 创建一个Observable(RxJava)
        **/
        Observable<T> o = (this.server == null ? this.selectServer() : Observable.just(this.server)).concatMap(new Func1<Server, Observable<T>>() {
            public Observable<T> call(Server server) {
                context.setServer(server);
                final ServerStats stats = LoadBalancerCommand.this.loadBalancerContext.getServerStats(server);
                Observable<T> o = Observable.just(server).concatMap(new Func1<Server, Observable<T>>() {
                    public Observable<T> call(final Server server) {
                        context.incAttemptCount();
                        LoadBalancerCommand.this.loadBalancerContext.noteOpenConnection(stats);
                        if (LoadBalancerCommand.this.listenerInvoker != null) {
                            try {
                                LoadBalancerCommand.this.listenerInvoker.onStartWithServer(context.toExecutionInfo());
                            } catch (AbortExecutionException var3) {
                                return Observable.error(var3);
                            }
                        }

                        final Stopwatch tracer = LoadBalancerCommand.this.loadBalancerContext.getExecuteTracer().start();
                        return operation.call(server).doOnEach(new Observer<T>() {
                            private T entity;

                            public void onCompleted() {
                                this.recordStats(tracer, stats, this.entity, (Throwable)null);
                            }

                            public void onError(Throwable e) {
                                this.recordStats(tracer, stats, (Object)null, e);
                                LoadBalancerCommand.logger.debug("Got error {} when executed on server {}", e, server);
                                if (LoadBalancerCommand.this.listenerInvoker != null) {
                                    LoadBalancerCommand.this.listenerInvoker.onExceptionWithServer(e, context.toExecutionInfo());
                                }

                            }

                            public void onNext(T entity) {
                                this.entity = entity;
                                if (LoadBalancerCommand.this.listenerInvoker != null) {
                                    LoadBalancerCommand.this.listenerInvoker.onExecutionSuccess(entity, context.toExecutionInfo());
                                }

                            }

                            private void recordStats(Stopwatch tracerx, ServerStats statsx, Object entity, Throwable exception) {
                                tracerx.stop();
                                LoadBalancerCommand.this.loadBalancerContext.noteRequestCompletion(statsx, entity, exception, tracerx.getDuration(TimeUnit.MILLISECONDS), LoadBalancerCommand.this.retryHandler);
                            }
                        });
                    }
                });
                if (maxRetrysSame > 0) {
                    o = o.retry(LoadBalancerCommand.this.retryPolicy(maxRetrysSame, true));
                }

                return o;
            }
        });
        if (maxRetrysNext > 0 && this.server == null) {
            o = o.retry(this.retryPolicy(maxRetrysNext, false));
        }

        return o.onErrorResumeNext(new Func1<Throwable, Observable<T>>() {
            public Observable<T> call(Throwable e) {
                if (context.getAttemptCount() > 0) {
                    if (maxRetrysNext > 0 && context.getServerAttemptCount() == maxRetrysNext + 1) {
                        e = new ClientException(ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED, "Number of retries on next server exceeded max " + maxRetrysNext + " retries, while making a call for: " + context.getServer(), (Throwable)e);
                    } else if (maxRetrysSame > 0 && context.getAttemptCount() == maxRetrysSame + 1) {
                        e = new ClientException(ErrorType.NUMBEROF_RETRIES_EXEEDED, "Number of retries exceeded max " + maxRetrysSame + " retries, while making a call for: " + context.getServer(), (Throwable)e);
                    }
                }

                if (LoadBalancerCommand.this.listenerInvoker != null) {
                    LoadBalancerCommand.this.listenerInvoker.onExecutionFailed((Throwable)e, context.toFinalExecutionInfo());
                }

                return Observable.error((Throwable)e);
            }
        });
    }

讲到这里,就是一次完整的路由过程了。我们大致回顾一下这个路由过程。
A.Zuul的转发是通过RibbonRoutingFilter这个Filter进行操作的。
B. 在转发之前,Zuul包装请求为RibbonCommand,并且RibbonCommand继承了HystrixCommand,并且持有RibbonLoadBalancingHttpClientFallbackProvider,正应为这样才使得Zuul具有了服务降级(Fallback),和负载均衡的功能,同时HystrixCommand是具备超时时间的(默认是1s)。而且Zuul默认采用的隔离级别是信号量模式。
C.在RibbonCommand的内部Zuul再次将请求包装成一个Observable,(有关RxJava的知识请参照其官方文档)。并且为Observable设置了重试次数,默认只对GET请求失败连接失败重试。

目录
相关文章
|
29天前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
67 2
|
25天前
|
XML Java 数据库连接
Spring高手之路25——深入解析事务管理的切面本质
本篇文章将带你深入解析Spring事务管理的切面本质,通过AOP手动实现 @Transactional 基本功能,并探讨PlatformTransactionManager的设计和事务拦截器TransactionInterceptor的工作原理,结合时序图详细展示事务管理流程,最后引导分析 @Transactional 的代理机制源码,帮助你全面掌握Spring事务管理。
31 2
Spring高手之路25——深入解析事务管理的切面本质
|
13天前
|
PyTorch Shell API
Ascend Extension for PyTorch的源码解析
本文介绍了Ascend对PyTorch代码的适配过程,包括源码下载、编译步骤及常见问题,详细解析了torch-npu编译后的文件结构和三种实现昇腾NPU算子调用的方式:通过torch的register方式、定义算子方式和API重定向映射方式。这对于开发者理解和使用Ascend平台上的PyTorch具有重要指导意义。
|
17天前
|
缓存 监控 Java
Java线程池提交任务流程底层源码与源码解析
【11月更文挑战第30天】嘿,各位技术爱好者们,今天咱们来聊聊Java线程池提交任务的底层源码与源码解析。作为一个资深的Java开发者,我相信你一定对线程池并不陌生。线程池作为并发编程中的一大利器,其重要性不言而喻。今天,我将以对话的方式,带你一步步深入线程池的奥秘,从概述到功能点,再到背景和业务点,最后到底层原理和示例,让你对线程池有一个全新的认识。
47 12
|
22天前
|
Java 开发者 Spring
深入解析:Spring AOP的底层实现机制
在现代软件开发中,Spring框架的AOP(面向切面编程)功能因其能够有效分离横切关注点(如日志记录、事务管理等)而备受青睐。本文将深入探讨Spring AOP的底层原理,揭示其如何通过动态代理技术实现方法的增强。
49 8
|
19天前
|
前端开发 Java 开发者
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
58 2
|
19天前
|
前端开发 Java Spring
探索Spring MVC:@Controller注解的全面解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序的基石之一。它不仅简化了控制器的定义,还提供了一种优雅的方式来处理HTTP请求。本文将全面解析`@Controller`注解,包括其定义、用法、以及在Spring MVC中的作用。
40 2
|
20天前
|
前端开发 Java Maven
深入解析:如何用 Spring Boot 实现分页和排序
深入解析:如何用 Spring Boot 实现分页和排序
42 2
|
22天前
|
Java 开发者 Spring
Spring AOP深度解析:探秘动态代理与增强逻辑
Spring框架中的AOP(Aspect-Oriented Programming,面向切面编程)功能为开发者提供了一种强大的工具,用以将横切关注点(如日志、事务管理等)与业务逻辑分离。本文将深入探讨Spring AOP的底层原理,包括动态代理机制和增强逻辑的实现。
33 4
|
19天前
|
前端开发 Java 开发者
Spring MVC中的控制器:@Controller注解全解析
在Spring MVC框架中,`@Controller`注解是构建Web应用程序控制层的核心。它不仅简化了控制器的定义,还提供了灵活的请求映射和处理机制。本文将深入探讨`@Controller`注解的用法、特点以及在实际开发中的应用。
50 0

推荐镜像

更多