SpringCloud:Ribbon與Feign
127.0.0.1 eureka1.com127.0.0.1 eureka2.com
spring:application:name: spring-cloud-learn-eurekaserver:port: 8761eureka:instance:hostname: eureka1.comclient:#表示是否將自己注冊到Eureka Server,默認為true。registerWithEureka: false#表示是否從Eureka Server獲取注冊信息,默認為true。fetchRegistry: falseserviceUrl:defaultZone: http://eureka2.com:8762/eureka/
spring:application:name: spring-cloud-learn-eurekaserver:port: 8762eureka:instance:hostname: eureka2.comclient:#表示是否將自己注冊到Eureka Server,默認為true。registerWithEureka: false#表示是否從Eureka Server獲取注冊信息,默認為true。fetchRegistry: falseserviceUrl:defaultZone: http://eureka1.com:8761/eureka/
spring:application:name: spring-cloud-learn-provider-deptserver:port: 8763eureka:client:serviceUrl:defaultZone: http://eureka1.com:8761/eureka/,http://eureka2.com:8762/eureka/
-
一致性(Consistency,C):在分布式系統(tǒng)中的所有數(shù)據(jù)備份,在同一時刻是否同樣的值。(等同于所有節(jié)點訪問同一份最新的數(shù)據(jù)副本)。 -
可用性(Availability,A):在一個分布式系統(tǒng)的集群中一部分節(jié)點故障后,該集群是否還能夠正常響應客戶端的讀寫請求。(對數(shù)據(jù)更新具備高可用性)。 -
分區(qū)容錯性(Partition tolerance,P):大多數(shù)的分布式系統(tǒng)都分布在多個子網(wǎng)絡(luò)中,而每個子網(wǎng)絡(luò)就叫做一個區(qū)(partition)。分區(qū)容錯的意思是,區(qū)間通信可能失敗。在一個分布式系統(tǒng)中一般分區(qū)容錯是無法避免的,因此可以認為 CAP 中的 P 總是成立的。CAP 理論告訴我們,在 C 和 A 之間是無法同時做到。
簡單的說,Ribbon是Netflix發(fā)布的開源項目,主要功能是提供客戶端的軟件負載均衡算法,將Netflix的中間層服務(wù)連接在一起。Ribbon客戶端組件提供一系列完善的配置項如連接超時,重試等。簡單的說,就是在配置文件中列出Load Balancer(簡稱LB)后面所有的機器,Ribbon會自動的幫助你基于某種規(guī)則(如簡單輪詢,隨機連接等)去連接這些機器。我們也很容易使用Ribbon實現(xiàn)自定義的負載均衡算法。
Ribbon的配置在上一篇中已經(jīng)給出,實現(xiàn)也是非常的簡單,主要看幾種負載均衡算法:
| 策略名 | 策略描述 | 實現(xiàn)說明 |
|---|---|---|
| BestAvailableRule | 選擇一個最小的并發(fā)請求的server | 逐個考察Server,如果Server被tripped了,則忽略,在選擇其中ActiveRequestsCount最小的server |
| AvailabilityFilteringRule | 過濾掉那些因為一直連接失敗的被標記為circuit tripped的后端server,并過濾掉那些高并發(fā)的的后端server(active connections 超過配置的閾值) | 使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status里記錄的各個server的運行狀態(tài) |
| WeightedResponseTimeRule | 根據(jù)相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。 | 一個后臺線程定期的從status里面讀取評價響應時間,為每個server計算一個weight。Weight的計算也比較簡單responsetime 減去每個server自己平均的responsetime是server的權(quán)重。當剛開始運行,沒有形成statas時,使用roubine策略選擇server。 |
| RetryRule | 對選定的負載均衡策略機上重試機制。 | 在一個配置時間段內(nèi)當選擇server不成功,則一直嘗試使用subRule的方式選擇一個可用的server |
| RoundRobinRule | roundRobin方式輪詢選擇server | 輪詢index,選擇index對應位置的server |
| RandomRule | 隨機選擇一個server | 在index上隨機,選擇index對應位置的server |
| ZoneAvoidanceRule | 復合判斷server所在區(qū)域的性能和server的可用性選擇server | 使用ZoneAvoidancePredicate和AvailabilityPredicate來判斷是否選擇某個server,前一個判斷判定一個zone的運行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于過濾掉連接數(shù)過多的Server。 |
當想要修改負載均衡的策略時,直接返回IRule實現(xiàn)即可,例:
@Beanpublic IRule myRule(){return new RandomRule();}
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.yuanqinnan</groupId><artifactId>spring-cloud-learn-parent</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>spring-cloud-learn-consumer-dept-feign</artifactId><packaging>jar</packaging><dependencies><!-- Spring Boot Begin --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Boot End --><!-- Spring Cloud Begin --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- Spring Cloud End --></dependencies></project>
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class ConsumerDeptFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerDeptFeignApplication.class, args);}}
spring:application:name: spring-cloud-learn-consumer-dept-feignserver:port: 8765eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
@FeignClient(value = "spring-cloud-learn-provider-dept")public interface DeptService {@RequestMapping(value = "hi", method = RequestMethod.GET)String sayHi(@RequestParam(value = "message") String message);}
@RestControllerpublic class DeptController {@Autowiredprivate DeptService deptService;@RequestMapping(value = "hi", method = RequestMethod.GET)public String sayHi(@RequestParam String message) {return deptService.sayHi(message);}}

剩下的就不會給大家一展出來了,以上資料按照一下操作即可獲得
——將文章進行轉(zhuǎn)發(fā)和評論,關(guān)注公眾號【Java烤豬皮】,關(guān)注后繼續(xù)后臺回復領(lǐng)取口令“ 666 ”即可免費領(lǐng)文章取中所提供的資料。
騰訊、阿里、滴滴后臺試題匯集總結(jié) — (含答案)
面試:史上最全多線程序面試題!
最新阿里內(nèi)推Java后端試題
JVM難學?那是因為你沒有真正看完整這篇文章
關(guān)注作者微信公眾號 — 《JAVA烤豬皮》
了解了更多java后端架構(gòu)知識以及最新面試寶典
看完本文記得給作者點贊+在看哦~~~大家的支持,是作者來源不斷出文的動力~
評論
圖片
表情
