Ribbon Loadbalance核心接口

Ribbon Loadbalance核心接口

接口列表

  • IRule
  • ILoadBalancer
  • ServerList
  • ServerListFilter
  • IPing

功能介紹

配置入口:RibbonClientConfiguration

合適的方式選擇合適的節點

ILoadBalancer

接口定義:com.netflix.loadbalancer.ILoadBalancer

核心方法:

	/**
	 * Choose a server from load balancer.
	 * 
	 * @param key An object that the load balancer may use to determine which server to return. null if 
	 *         the load balancer does not use this parameter.
	 * @return server chosen
	 */
	public Server chooseServer(Object key);
/**
 * @return Only the servers that are up and reachable.
    */
   public List<Server> getReachableServers();

   /**
    * @return All known servers, both reachable and unreachable.
    */
public List<Server> getAllServers();

核心作用:維護全量服務列表及可用列表,依賴IPing接口標記失效節點,標識失效依賴IPing實現

默認實現:ZoneAwareLoadBalancer

IRule

接口定義:com.netflix.loadbalancer.IRule

核心方法:

/*
 * choose one alive server from lb.allServers or
 * lb.upServers according to key
 * 
 * @return choosen Server object. NULL is returned if none
 *  server is available 
 */

public Server choose(Object key);

核心作用:從loadbalance裏面選取一個server;提供多種選擇策略

默認實現:ZoneAvoidanceRule

ServerList

接口定義:com.netflix.loadbalancer.ServerList

核心方法:

/**
 * Return updated list of servers. This is called say every 30 secs
 * (configurable) by the Loadbalancer's Ping cycle
 * 
 */
public List<T> getUpdatedListOfServers(); 

核心作用:獲取更新服務列表

默認實現:ConfigurationBasedServerList

ServerListFilter

接口定義:com.netflix.loadbalancer.ServerListFilter

核心方法:

public List<T> getFilteredListOfServers(List<T> servers);

核心作用:

默認實現:ZonePreferenceServerListFilter

IPing

接口定義:com.netflix.loadbalancer.IPing

核心方法:

/**
 * Checks whether the given <code>Server</code> is "alive" i.e. should be
 * considered a candidate while loadbalancing
 * 
 */
public boolean isAlive(Server server);

核心作用:檢測特定服務是否活着,在ILoadBalance中做失效服務檢測作用

默認實現:DummyPing

擴展思考

Ribbon最大的有點就是允許用戶定製化各種規則,很好的管理了節點狀態及訪問規則,基於規則擴展做一些好用的產品,比如灰度管理、目標節點能力不對等下的權重控制

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章