一、Ribbon中的負載均衡策略
1、Ribbon中支持的負載均衡策略
AvailabilityFilteringRule:過濾掉那些因為一直連接失敗的被標記為circuit tripped的后端server,並過濾掉那些高並發的的后端server(active connections 超過配置的閾值) | 使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status里記錄的各個server的運行狀態
RandomRule:隨機選擇一個server
BestAvailabl:選擇一個最小的並發請求的server,逐個考察Server,如果Server被tripped了,則忽略
RoundRobinRule:roundRobin方式輪詢選擇, 輪詢index,選擇index對應位置的server
WeightedResponseTimeRule:根據響應時間分配一個weight(權重),響應時間越長,weight越小,被選中的可能性越低
RetryRule:對選定的負載均衡策略機上重試機制,在一個配置時間段內當選擇server不成功,則一直嘗試使用subRule的方式選擇一個可用的server
ZoneAvoidanceRule:復合判斷server所在區域的性能和server的可用性選擇server
ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一樣的,ResponseTimeWeightedRule后來改名為WeightedResponseTimeRule
二、驗證
1、自定義負載均衡策略
-
# 自定義負載均衡策略 springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定義使用隨機策略,springboot-h2是服務應用名
2、修改調用代碼
package com.chhliu.springboot.restful.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController public class RestTemplateController { @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/template/{id}") public User findById(@PathVariable Long id) { ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2"); System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());// 打印當前調用服務的信息 User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class); System.out.println(u); return u; } }
3、測試
服務調用關系如下:
測試結果如下:
-
===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7902 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] ===:springboot-h2:127.0.0.1:7901 User [id=2, username=user2, name=李四, age=20, balance=100.00] 發現選擇7901端口服務和7902端口服務確實是隨機的!