目錄4、測試
ribbon主要負責負載均衡呼叫,是基於netflix ribbon實現的一套客戶端。主要功能是提供客戶端的軟體負載均衡演算法和服務呼叫。ribbon會自動的幫助你基於某種規則去連這些機器。
簡單來說 ribbon = 負載均衡 + resttemplate
nginx是伺服器負載均衡,集中式負載均衡,客戶端的所有請求都會交給nginx,然後由nginx實現**請求。即負載均衡是由服務端實現的。
ribbon本地負載均衡,進行內負載均衡,在呼叫為服務介面的時候,會在註冊中心上獲取資訊服務列表之後快取到jvm本地,從而在本地實現rpc遠端服務呼叫技術。
ribbon在工作時分成兩步:
ribbon就是乙個軟負載均衡的客戶端元件,它可以和其他所需請求的客戶端結合使用,和eureka結合只是其中的乙個例項。
eureka-client、eureka-client2
eureka-client2就是 eureka-client的複製版,注意埠號要更改。
在eureka-client中編寫controller,用於服務呼叫測試。
3.2.1、編寫配置類@restcontroller
public class clientcontroller ")
private string port;
@responsebody
public string getport()
}
3.2.2、編寫controller@configuration
@bean
@loadbalanced
public resttemplate getresttemplate()
}
啟動eureka-server、eureka-client、eureka-client2、eureka-consumer。@restcontroller
public class consumercontroller
}
訪問localhost:8001/port進行自測。
訪問localhost:8002/port進行自測。
自測成功後,不停訪問localhost:7001/consumer/port。
發現埠號會變更,說明ribbon負載均衡已生效。
Spring Cloud Ribbon實現負載均衡
之前文章 spring cloud 服務註冊和發現 spring cloud 服務端高可用 負載均衡在系統架構中是乙個非常重要的角色,在前面大型 架構學習總結中,可以看到,高可用,伸縮性,效能幾個架構要素中,負載均衡都有著很重要的地位,是系統壓力緩解,系統擴容的重要手段之一。一般來說,我們講的負載均...
四 SpringCloud Ribbon負載均衡
在服務消費者 user consumer 的啟動類中的resttemplate類加上 loadbalanced註解 enablediscoveryclient public class bean loadbalanced public resttemplate resttemplate 修改dao,...
springcloud(Ribbon負載均衡)
搭建步驟 1.使用者服務的集群 2.訂單服務整合ribbon 2.1.匯入ribbon的依賴 2.2.在resttemplate的bean定義方法上加上 loadbalanced註解 2.3.把訂單的controller向使用者發起呼叫的resttemplate的url使用服務名呼叫,如 http ...