<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # ribbon-spring-boot-starter ## ribbon客戶端負載均衡 > ribbon客戶端負載均衡: 消費方從注冊中心上獲取接口調用地址列表,客戶端實現負載均衡算法(輪詢,隨機,hash一致性,權重)等原理獲取接口地址列表,采用算法規則獲取選擇一個接口地址,并發起遠程調用 ## 負載均衡主要組件 ![](https://img.kancloud.cn/3e/69/3e692edf8c833765456dd723a8a746f2_1133x559.png) | 組件 | 作用 | | --- | --- | | ILoadBalancer | 定義一系列的操作接口,比如選擇服務實例 | | IRule| 算法策略,內置算法策略來為服務實例的選擇提供服務| | ServerList| 負責服務實例信息的獲取,可以獲取配置文件中的,也可以從注冊中心獲取| | ServerListFilter| 過濾掉某些不想要的服務實例信息| | ServerListUpdater| 更新本地緩存的服務實例信息| | IPing| 對已有的服務實例進行可用性檢查,保證選擇的服務都是可用的| ## 規則接口 ![](https://img.kancloud.cn/22/5e/225ee4d612a1d96a86924a562d73a802_723x279.png) ![](https://img.kancloud.cn/36/a8/36a8db4ef04f1c9e2d88544ea27c5d5e_516x236.png) ## 默認規則 ![](https://img.kancloud.cn/cb/df/cbdff2c642ef0495dfe668fdf3613366_848x213.png) ![](https://img.kancloud.cn/fd/c0/fdc06f836e0b4485eff1937bc8cc835b_1227x526.png) ## 自定義規則 ![](https://img.kancloud.cn/41/a2/41a29005c1f2badbef1c78c21eb98d25_1631x858.png) ## 替換自定義規則 ![](https://img.kancloud.cn/2d/93/2d93a3951ec076bb1672c8ddd2bd3461_1621x664.png) ![](https://img.kancloud.cn/b3/cf/b3cf8400b1c9f9086b191f11afad7da3_1433x674.png) ![](https://img.kancloud.cn/63/72/637203b7f244dabde6b335017557d4f2_430x156.png) ## 自定義過濾規則 ![](https://img.kancloud.cn/1a/a2/1aa22d466f41a82206a635382e7ede6d_957x350.png) ### 默認規則返回false時 ![](https://img.kancloud.cn/4d/82/4d8281f0269b216b3582c918a11ec471_1388x515.png) ![](https://img.kancloud.cn/a7/93/a793dd57ed7fe5da85fed8ba81a6dfde_1416x734.png) ![](https://img.kancloud.cn/ed/9f/ed9f52df172de2ef5fc4a7697873355a_1436x578.png) #### 在上面`choose`函數中調用的`chooseRoundRobinAfterFiltering`方法先通過內部定義的`getEligibleServers`函數來獲取備選的實例清單(實現了過濾),由于MetadataAwarePredicate返回了false,返回的清單為空,則用`Optional.absent()`來表示不存在 ![](https://img.kancloud.cn/3d/5c/3d5cd950a4951b27ab345ebdffb42b81_1579x668.png) ![](https://img.kancloud.cn/9f/e3/9fe332e40817757762d2fd36151bf642_1708x637.png) ### 默認規則返回true時 ![](https://img.kancloud.cn/a6/1f/a61f02db23cbfd68ae2a648df812a215_1228x666.png) #### 在上面`choose`函數中調用的`chooseRoundRobinAfterFiltering`方法先通過內部定義的`getEligibleServers`函數來獲取備選的實例清單(實現了過濾),由于MetadataAwarePredicate返回了true ,以線性輪詢的方式從備選清單中獲取一個實例。 ``` // 線性輪詢 private int incrementAndGetModulo(int modulo) { for (;;) { int current = nextIndex.get(); int next = (current + 1) % modulo; if (nextIndex.compareAndSet(current, next)) return current; } } ``` ## 總結: 在了解了整體邏輯之后,我們來詳細看看實現過濾功能的`getEligibleServers`函數。從源碼上看,它的實現結構非常簡單清晰,通過遍歷服務清單,使用`this.apply`方法來判斷實例是否需要保留,是就添加到結果列表中。 ## Predicate 介紹 ### Predicate 流程處理 #### `chooseRoundRobinAfterFiltering` 內部的細節 ``` @Override public Server choose(Object key) { ILoadBalancer lb = getLoadBalancer(); // 過濾之后循環選擇server Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(lb.getAllServers(), key); if (server.isPresent()) { return server.get(); } else { return null; } } ``` ![](https://img.kancloud.cn/11/1f/111f36fabf0aa23551593273cc073632_1231x652.png) #### `getEligibleServers`函數來獲取備選的實例清單 ``` @Override public List<Server> getEligibleServers(List<Server> servers, Object loadBalancerKey) { // 先調用抽象類過濾 List<Server> result = super.getEligibleServers(servers, loadBalancerKey); // 再進行過濾 Iterator<AbstractServerPredicate> i = fallbacks.iterator(); while (!(result.size() >= minimalFilteredServers && result.size() > (int) (servers.size() * minimalFilteredPercentage)) && i.hasNext()) { AbstractServerPredicate predicate = i.next(); result = predicate.getEligibleServers(servers, loadBalancerKey); } return result; } ``` ![](https://img.kancloud.cn/76/e2/76e2a01e222e545417467ed8c8ea6c7c_1286x353.png) #### 通過遍歷服務清單 ![](https://img.kancloud.cn/30/a8/30a84f1eb74133c79c4b39f2b623099b_1224x658.png) ![](https://img.kancloud.cn/38/c7/38c77110da6a42eab97dbcfd25e200d7_1223x637.png) #### 使用`this.apply`方法來判斷實例是否需要保留,是就添加到結果列表中 ![](https://img.kancloud.cn/63/ac/63acca6657420bc49bb3b36a53036a0f_1229x658.png) ![](https://img.kancloud.cn/d0/93/d09346ceefc015cf8e43820780e91c9f_1216x656.png) ![](https://img.kancloud.cn/0b/4c/0b4cb8cb3b7c0fa901adcb78348709ef_1412x642.png) ![](https://img.kancloud.cn/41/46/4146cc24e75c72ff87ef2b1940b3aee4_1168x491.png) 以上是Predicate 流程處理細節, 下面將要介紹的兩個策略就是基于此抽象策略實現,只是它們使用了不同的`Predicate`實現來完成過濾邏輯以達到不同的實例選擇效果。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看