本篇內(nèi)容主要講解“SpringCloud中Ribbon的簡(jiǎn)介及工作流程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“SpringCloud中Ribbon的簡(jiǎn)介及工作流程”吧!
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)相山,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
一:Ribbon是什么?
Ribbon是Netflix發(fā)布的開源項(xiàng)目,主要功能是提供客戶端的軟件負(fù)載均衡算法,將Netflix的中間層服務(wù)連接在一起。Ribbon客戶端組件提供一系列完善的配置項(xiàng)如連接超時(shí),重試等。簡(jiǎn)單的說,就是在配置文件中列出Load Balancer(簡(jiǎn)稱LB)后面所有的機(jī)器,Ribbon會(huì)自動(dòng)的幫助你基于某種規(guī)則(如簡(jiǎn)單輪詢,隨即連接等)去連接這些機(jī)器。我們也很容易使用Ribbon實(shí)現(xiàn)自定義的負(fù)載均衡算法。
二:LB方案分類
目前主流的LB方案可分成兩類:一種是集中式LB, 即在服務(wù)的消費(fèi)方和提供方之間使用獨(dú)立的LB設(shè)施(可以是硬件,如F5, 也可以是軟件,如nginx), 由該設(shè)施負(fù)責(zé)把訪問請(qǐng)求通過某種策略轉(zhuǎn)發(fā)至服務(wù)的提供方;另一種是進(jìn)程內(nèi)LB,將LB邏輯集成到消費(fèi)方,消費(fèi)方從服務(wù)注冊(cè)中心獲知有哪些地址可用,然后自己再?gòu)倪@些地址中選擇出一個(gè)合適的服務(wù)器。Ribbon就屬于后者,它只是一個(gè)類庫(kù),集成于消費(fèi)方進(jìn)程,消費(fèi)方通過它來獲取到服務(wù)提供方的地址。
三:Ribbon的主要組件與工作流程
Ribbon的核心組件(均為接口類型)有以下幾個(gè):
ServerList
用于獲取地址列表。它既可以是靜態(tài)的(提供一組固定的地址),也可以是動(dòng)態(tài)的(從注冊(cè)中心中定期查詢地址列表)。
ServerListFilter
僅當(dāng)使用動(dòng)態(tài)ServerList時(shí)使用,用于在原始的服務(wù)列表中使用一定策略過慮掉一部分地址。
IRule
選擇一個(gè)最終的服務(wù)地址作為L(zhǎng)B結(jié)果。選擇策略有輪詢、根據(jù)響應(yīng)時(shí)間加權(quán)、斷路器(當(dāng)Hystrix可用時(shí))等。
Ribbon在工作時(shí)首選會(huì)通過ServerList來獲取所有可用的服務(wù)列表,然后通過ServerListFilter過慮掉一部分地址,最后在剩下的地址中通過IRule選擇出一臺(tái)服務(wù)器作為最終結(jié)果。
四:Ribbon提供的主要負(fù)載均衡策略介紹
1:簡(jiǎn)單輪詢負(fù)載均衡(RoundRobin)
以輪詢的方式依次將請(qǐng)求調(diào)度不同的服務(wù)器,即每次調(diào)度執(zhí)行i = (i + 1) mod n,并選出第i臺(tái)服務(wù)器。
2:隨機(jī)負(fù)載均衡 (Random)
隨機(jī)選擇狀態(tài)為UP的Server
3:加權(quán)響應(yīng)時(shí)間負(fù)載均衡 (WeightedResponseTime)
根據(jù)相應(yīng)時(shí)間分配一個(gè)weight,相應(yīng)時(shí)間越長(zhǎng),weight越小,被選中的可能性越低。
4:區(qū)域感知輪詢負(fù)載均衡(ZoneAvoidanceRule)
復(fù)合判斷server所在區(qū)域的性能和server的可用性選擇server
Ribbon自帶負(fù)載均衡策略比較
策略名 | 策略聲明 | 策略描述 | 實(shí)現(xiàn)說明 |
BestAvailableRule | public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule | 選擇一個(gè)最小的并發(fā)請(qǐng)求的server | 逐個(gè)考察Server,如果Server被tripped了,則忽略,在選擇其中ActiveRequestsCount最小的server |
AvailabilityFilteringRule | public class AvailabilityFilteringRule extends PredicateBasedRule | 過濾掉那些因?yàn)橐恢边B接失敗的被標(biāo)記為circuit tripped的后端server,并過濾掉那些高并發(fā)的的后端server(active connections 超過配置的閾值) | 使用一個(gè)AvailabilityPredicate來包含過濾server的邏輯,其實(shí)就就是檢查status里記錄的各個(gè)server的運(yùn)行狀態(tài) |
WeightedResponseTimeRule | public class WeightedResponseTimeRule extends RoundRobinRule | 根據(jù)相應(yīng)時(shí)間分配一個(gè)weight,相應(yīng)時(shí)間越長(zhǎng),weight越小,被選中的可能性越低。 | 一 個(gè)后臺(tái)線程定期的從status里面讀取評(píng)價(jià)響應(yīng)時(shí)間,為每個(gè)server計(jì)算一個(gè)weight。Weight的計(jì)算也比較簡(jiǎn)單responsetime 減去每個(gè)server自己平均的responsetime是server的權(quán)重。當(dāng)剛開始運(yùn)行,沒有形成statas時(shí),使用roubine策略選擇 server。 |
RetryRule | public class RetryRule extends AbstractLoadBalancerRule | 對(duì)選定的負(fù)載均衡策略機(jī)上重試機(jī)制。 | 在一個(gè)配置時(shí)間段內(nèi)當(dāng)選擇server不成功,則一直嘗試使用subRule的方式選擇一個(gè)可用的server |
RoundRobinRule | public class RoundRobinRule extends AbstractLoadBalancerRule | roundRobin方式輪詢選擇server | 輪詢index,選擇index對(duì)應(yīng)位置的server |
RandomRule | public class RandomRule extends AbstractLoadBalancerRule | 隨機(jī)選擇一個(gè)server | 在index上隨機(jī),選擇index對(duì)應(yīng)位置的server |
ZoneAvoidanceRule | public class ZoneAvoidanceRule extends PredicateBasedRule | 復(fù)合判斷server所在區(qū)域的性能和server的可用性選擇server | 使 用ZoneAvoidancePredicate和AvailabilityPredicate來判斷是否選擇某個(gè)server,前一個(gè)判斷判定一個(gè) zone的運(yùn)行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于過濾掉連接數(shù)過多的 Server。 |
五:Ribbon單獨(dú)使用
創(chuàng)建一個(gè)maven工程 名稱 ribbon_client
pom內(nèi)容
<dependencies> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-core</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-httpclient</artifactId> <version>2.2.0</version> </dependency> </dependencies>
sample-client.properties配置文件
# Max number of retries sample-client.ribbon.MaxAutoRetries=1 # Max number of next servers to retry (excluding the first server) sample-client.ribbon.MaxAutoRetriesNextServer=1 # Whether all operations can be retried for this client sample-client.ribbon.OkToRetryOnAllOperations=true # Interval to refresh the server list from the source sample-client.ribbon.ServerListRefreshInterval=2000 # Connect timeout used by Apache HttpClient sample-client.ribbon.ConnectTimeout=3000 # Read timeout used by Apache HttpClient sample-client.ribbon.ReadTimeout=3000 # Initial list of servers, can be changed via Archaius dynamic property at runtime sample-client.ribbon.listOfServers=www.sohu.com:80,www.163.com:80,www.sina.com.cn:80 sample-client.ribbon.EnablePrimeConnections=true
RibbonMain代碼
import java.net.URI; import com.netflix.client.ClientFactory; import com.netflix.client.http.HttpRequest; import com.netflix.client.http.HttpResponse; import com.netflix.config.ConfigurationManager; import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.niws.client.http.RestClient; public class RibbonMain { public static void main( String[] args ) throws Exception { ConfigurationManager.loadPropertiesFromResources("sample-client.properties"); System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers")); RestClient client = (RestClient)ClientFactory.getNamedClient("sample-client"); HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); for(int i = 0; i < 4; i ++) { HttpResponse response = client.executeWithLoadBalancer(request); System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus()); } ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer(); System.out.println(lb.getLoadBalancerStats()); ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "ccblog.cn:80,www.linkedin.com:80"); System.out.println("changing servers ..."); Thread.sleep(3000); for(int i = 0; i < 3; i ++) { HttpResponse response = client.executeWithLoadBalancer(request); System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus()); } System.out.println(lb.getLoadBalancerStats()); } }
代碼解析
使用 Archaius ConfigurationManager 加載屬性;
使用 ClientFactory 創(chuàng)建客戶端和負(fù)載均衡器;
使用 builder 構(gòu)建 http 請(qǐng)求。注意我們只支持 URI 的 "/" 部分的路徑,一旦服務(wù)器被負(fù)載均衡器選中,會(huì)由客戶端計(jì)算出完整的 URI;
調(diào)用 API client.executeWithLoadBalancer(),不是 exeucte() API;
動(dòng)態(tài)修正配置中的服務(wù)器池;
等待服務(wù)器列表刷新(配置文件中定義的刷新間隔是為 3 秒鐘);
打印出負(fù)載均衡器記錄的服務(wù)器統(tǒng)計(jì)信息。
六:Ribbon結(jié)合eureka使用
先要啟動(dòng)eureka_register_service工程(注冊(cè)中心)和biz-service-0工程(服務(wù)生產(chǎn)者)
創(chuàng)建maven工程 eureka_ribbon_client 該工程啟動(dòng)和相關(guān)配置依賴eureka_register_service和biz-service-0
pom加入
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在應(yīng)用主類中,通過@EnableDiscoveryClient注解來添加發(fā)現(xiàn)服務(wù)能力。創(chuàng)建RestTemplate實(shí)例,并通過@LoadBalanced注解開啟均衡負(fù)載能力。
@SpringBootApplication @EnableDiscoveryClient public class RibbonApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } }
創(chuàng)建ConsumerController來消費(fèi)biz-service-0的getuser服務(wù)。通過直接RestTemplate來調(diào)用服務(wù)
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET) public String add() { return restTemplate.getForEntity("http://biz-service-0/getuser", String.class).getBody(); } }
application.properties中配置eureka服務(wù)注冊(cè)中心
spring.application.name=ribbon-consumer server.port=8003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
完成后可以打開http://localhost:8003/getuserinfo 可以看到結(jié)果
總結(jié):Ribbon其實(shí)就是一個(gè)軟負(fù)載均衡的客戶端組件,他可以和其他所需請(qǐng)求的客戶端結(jié)合使用,和eureka結(jié)合只是其中的一個(gè)實(shí)例。
到此,相信大家對(duì)“SpringCloud中Ribbon的簡(jiǎn)介及工作流程”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文題目:SpringCloud中Ribbon的簡(jiǎn)介及工作流程
本文鏈接:http://m.newbst.com/article48/gcsghp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、域名注冊(cè)、小程序開發(fā)、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)