0
点赞
收藏
分享

微信扫一扫

Ribbon源码分析之@LoadBalanced

Ribbon源码分析之@LoadBalanced

@LoadBalanced注解用来给RestTemplate做标记,方便使用负载均衡的客户端LoadBalancerClient来配置它。

LoadBalancerClient:

public interface LoadBalancerClient {
   /**
    * Choose a ServiceInstance from the LoadBalancer for the specified service
    * @param serviceId the service id to look up the LoadBalancer
    * @return a ServiceInstance that matches the serviceId
    */
   ServiceInstance choose(String serviceId);

   /**
    * execute request using a ServiceInstance from the LoadBalancer for the specified
    * service
    * @param serviceId the service id to look up the LoadBalancer
    * @param request allows implementations to execute pre and post actions such as
    * incrementing metrics
    * @return the result of the LoadBalancerRequest callback on the selected
    * ServiceInstance
    */
   <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException;

   /**
    * Create a proper URI with a real host and port for systems to utilize.
    * Some systems use a URI with the logical serivce name as the host,
    * such as http://myservice/path/to/service.  This will replace the
    * service name with the host:port from the ServiceInstance.
    * @param instance
    * @param original a URI with the host as a logical service name
    * @return a reconstructed URI
    */
   URI reconstructURI(ServiceInstance instance, URI original);

}

接口中,我们通过定义抽象方法来了解客户端负载均衡器中应具备的能力

  • ServiceInstance choose(String serviceId); 根据传入的服务名serviceId从负载均衡器中挑选一个对应服务的实例

  • T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException; 从负载均衡器中挑选出的服务实例来执行请求内容

  • URI reconstructURI(ServiceInstance instance, URI original); 为系统构建一个合适的host:port 形式的URI

    在分布式系统中,我们使用逻辑上的服务名称作为host来构建URI进行请求。在操作定义中,ServiceInstance对象带有host和port具体服务实例,后者URI对象使用逻辑服务名定义为host的URI,返回的URI内容是通过ServiceInstance 的服务实例详情拼接出来的host:port形式的请求地址。

举报

相关推荐

0 条评论