摘要:第篇電影微服務(wù),使用在客戶端進(jìn)行負(fù)載均衡一大致介紹是發(fā)布的云中間層服務(wù)開源項(xiàng)目,主要功能是提供客戶端負(fù)載均衡算法。而被注解后,能過(guò)用負(fù)載均衡,主要是維護(hù)了一個(gè)被注解的列表,并給列表中的添加攔截器,進(jìn)而交給負(fù)載均衡器去處理。
SpringCloud(第 006 篇)電影微服務(wù),使用 Ribbon 在客戶端進(jìn)行負(fù)載均衡
-
一、大致介紹1、Ribbon 是 Netflix 發(fā)布的云中間層服務(wù)開源項(xiàng)目,主要功能是提供客戶端負(fù)載均衡算法。 2、Ribbon 客戶端組件提供一系列完善的配置項(xiàng),如,連接超時(shí),重試等。簡(jiǎn)單的說(shuō),Ribbon是一個(gè)客戶端負(fù)載均衡器,我們可以在配置文件中列出load Balancer后面所有的機(jī)器,Ribbon會(huì)自動(dòng)的幫助你基于某種規(guī)則(如簡(jiǎn)單輪詢,隨機(jī)連接等)去連接這些機(jī)器,我們也很容易使用Ribbon實(shí)現(xiàn)自定義的負(fù)載均衡算法。 3、Ribbon 實(shí)現(xiàn)軟負(fù)載均衡,核心有三點(diǎn): * 服務(wù)發(fā)現(xiàn),發(fā)現(xiàn)依賴服務(wù)的列表 * 服務(wù)選擇規(guī)則,在多個(gè)服務(wù)中如何選擇一個(gè)有效服務(wù) * 服務(wù)監(jiān)聽,檢測(cè)失效的服務(wù),高效剔除失效服務(wù) 4、本章節(jié)僅僅只是簡(jiǎn)單使用 Ribbon 來(lái)達(dá)到客戶端負(fù)載均衡選擇后端微服務(wù)列表而已,深入了解 Ribbon 使用的話請(qǐng)繼續(xù)期待后序有關(guān) Ribbon 章節(jié)的更新。二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-consumer-movie-ribbonsrcmainresourcesapplication.yml)4.0.0 springms-consumer-movie-ribbon 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator
spring: application: name: springms-consumer-movie-ribbon server: port: 8010 #做負(fù)載均衡的時(shí)候,不需要這個(gè)動(dòng)態(tài)配置的地址 #user: # userServicePath: http://localhost:7900/simple/ eureka: client: # healthcheck: # enabled: true serviceUrl: defaultZone: http://admin:admin@localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}2.3 添加實(shí)體用戶類User(springms-consumer-movie-ribbonsrcmainjavacomspringmscloudentityUser.java)
package com.springms.cloud.entity; import java.math.BigDecimal; public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Short getAge() { return this.age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return this.balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } }2.4 添加電影Web訪問層Controller(springms-consumer-movie-ribbonsrcmainjavacomspringmscloudcontrollerMovieRibbonController.java)
package com.springms.cloud.controller; import com.springms.cloud.entity.User; import org.springframework.beans.factory.annotation.Autowired; 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; /** * 電影微服務(wù)Ribbon的Web控制層,使用采用 LoadBalanced 注解后的 restTemplate 進(jìn)行負(fù)載均衡調(diào)度不同后端微服務(wù); * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/17 * */ @RestController public class MovieRibbonController { @Autowired private RestTemplate restTemplate; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { // http://localhost:7900/simple/ // VIP:virtual IP // HAProxy Heartbeat return this.restTemplate.getForObject("http://springms-provider-user/simple/" + id, User.class); } /** * 添加給 springms-sidecar 微服務(wù)做測(cè)試用的代碼。 * * @return */ @GetMapping("/sidecar") public String sidecar() { return this.restTemplate.getForObject("http://springms-sidecar/", String.class); } /** * 添加給 springms-sidecar 微服務(wù)做測(cè)試用的代碼。 * * @return */ @GetMapping("/sidecar/health.json") public String sidecarHealth() { return this.restTemplate.getForObject("http://springms-sidecar/health.json", String.class); } }2.5 添加電影微服務(wù)啟動(dòng)類(springms-consumer-movie-ribbonsrcmainjavacomspringmscloudMsConsumerMovieRibbonApplication.java)
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * 電影微服務(wù),使用 Ribbon 在客戶端進(jìn)行負(fù)載均衡。 * * LoadBalanced:該負(fù)載均衡注解,已經(jīng)整合了 Ribbon; * * 在瀏覽器輸入http://localhost:8010/movie/3 地址后,注解 LoadBalanced 會(huì)進(jìn)行負(fù)載均衡將請(qǐng)求分配到不同的【用戶微服務(wù)】上面; * * Ribbon 的默認(rèn)負(fù)載均衡的算法為:輪詢; * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/17 * */ @SpringBootApplication @EnableEurekaClient public class MsConsumerMovieRibbonApplication { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MsConsumerMovieRibbonApplication.class, args); System.out.println("【【【【【【 電影微服務(wù)-Ribbon 】】】】】】已啟動(dòng)."); } }三、Ribbon 簡(jiǎn)述流程分析
Ribbon的負(fù)載均衡,主要通過(guò)LoadBalancerClient來(lái)實(shí)現(xiàn)的,而LoadBalancerClient具體交給了ILoadBalancer來(lái)處理, ILoadBalancer通過(guò)配置IRule、IPing等信息,并向EurekaClient獲取注冊(cè)列表的信息,并默認(rèn)10秒一次向EurekaClient發(fā)送“ping”, 進(jìn)而檢查是否更新服務(wù)列表,最后,得到注冊(cè)列表后,ILoadBalancer根據(jù)IRule的策略進(jìn)行負(fù)載均衡。 而RestTemplate 被@LoadBalance注解后,能過(guò)用負(fù)載均衡,主要是維護(hù)了一個(gè)被@LoadBalance注解的RestTemplate列表,并給列表中的RestTemplate添加攔截器,進(jìn)而交給負(fù)載均衡器去處理。四、測(cè)試
/**************************************************************************************** 一、電影微服務(wù),使用 Ribbon 在客戶端進(jìn)行負(fù)載均衡: 1、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 2、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)3個(gè)端口(7900、7899、7898); 3、啟動(dòng) springms-consumer-movie-ribbon 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、在瀏覽器輸入地址 http://localhost:7900/simple/1 可以看到信息成功的被打印出來(lái),說(shuō)明用戶微服務(wù)正常; 5、在瀏覽器輸入地址 http://localhost:8761 并輸入用戶名密碼 admin/admin 進(jìn)入Eureka微服務(wù)顯示在網(wǎng)頁(yè)中,驗(yàn)證三個(gè)用戶微服務(wù)、1電影Ribbon微服務(wù)確實(shí)注冊(cè)到了 eureka 服務(wù)中; 6、在瀏覽器輸入地址http://localhost:8010/movie/1 連續(xù)在瀏覽器回車6次; 7、然后跑到用戶微服務(wù)的三臺(tái)服務(wù)器上查看 Hibernate 查詢用戶的 sql 日志,每臺(tái)用戶微服務(wù)的都被均勻的調(diào)用了 2 次用戶信息; 總結(jié):由此可見,啟動(dòng)了3臺(tái)用戶微服務(wù)都注冊(cè)到eureka服務(wù)中心,然后通過(guò)Ribbon對(duì)電影微服務(wù)進(jìn)行客戶端負(fù)載均衡調(diào)度,從而實(shí)現(xiàn)了客戶端負(fù)載均衡算法,默認(rèn)調(diào)度算法為輪詢; ****************************************************************************************/五、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70443.html
摘要:第篇電影微服務(wù),使用配置文件配置在客戶端進(jìn)行負(fù)載均衡調(diào)度算法一大致介紹通過(guò)配置來(lái)設(shè)置客戶端進(jìn)行負(fù)載均衡的調(diào)度算法通過(guò)兩種代碼調(diào)用方式來(lái)測(cè)試客戶端負(fù)載均衡算法二實(shí)現(xiàn)步驟添加引用包模塊客戶端發(fā)現(xiàn)模塊 SpringCloud(第 008 篇)電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進(jìn)行負(fù)載均衡調(diào)度算法 - 一、大致介紹 1、通過(guò) applicat...
摘要:第篇電影微服務(wù),脫離使用配置進(jìn)行客戶端負(fù)載均衡調(diào)度一大致介紹通過(guò)嘗試脫離服務(wù)治理框架,脫離生態(tài)圈,單獨(dú)操作客戶端負(fù)載均衡調(diào)度本章節(jié)僅僅只是使用了來(lái)測(cè)試客戶端負(fù)載均衡算法二實(shí)現(xiàn)步驟添加引用包模塊客 SpringCloud(第 011 篇)電影Ribbon微服務(wù),脫離Eureka使用配置listOfServers進(jìn)行客戶端負(fù)載均衡調(diào)度 - 一、大致介紹 1、通過(guò)嘗試脫離服務(wù)治理框架,脫離 ...
摘要:中的名稱,一定要是服務(wù)中注冊(cè)的名稱。添加這個(gè)的注解,主要是因?yàn)槎x的時(shí)候報(bào)錯(cuò),也就是說(shuō)明沒有被實(shí)例化。采用隨機(jī)分配的策略。添加訪問層添加電影微服務(wù)啟動(dòng)類電影微服務(wù),使用定制化在客戶端進(jìn)行負(fù)載均衡,使用不同服務(wù)不同配置策略。 SpringCloud(第 007 篇)電影微服務(wù),使用定制化 Ribbon 在客戶端進(jìn)行負(fù)載均衡,使用 RibbonClient 不同服務(wù)不同配置策略 - 一、大...
摘要:添加電影微服務(wù)啟動(dòng)類電影微服務(wù)接入進(jìn)行客戶端負(fù)載均衡,通過(guò)調(diào)用遠(yuǎn)程微服務(wù)。注解表示該電影微服務(wù)已經(jīng)接入模塊。 SpringCloud(第 012 篇)電影微服務(wù)接入 Feign 進(jìn)行客戶端負(fù)載均衡,通過(guò) FeignClient 調(diào)用遠(yuǎn)程 Http 微服務(wù) - 一、大致介紹 1、本章節(jié)主要介紹在 SpringCloud 生態(tài)圈中,使用一個(gè)類似于 Java HTTP 客戶端的工具 Feig...
摘要:當(dāng)服務(wù)宕機(jī)或者不可用時(shí),即請(qǐng)求超時(shí)后會(huì)調(diào)用此方法。添加電影微服務(wù)啟動(dòng)類電影微服務(wù)集成斷路器實(shí)現(xiàn)失敗快速響應(yīng),達(dá)到熔斷效果。 SpringCloud(第 014 篇)電影 Ribbon 微服務(wù)集成 Hystrix 斷路器實(shí)現(xiàn)失敗快速響應(yīng),達(dá)到熔斷效果 - 一、大致介紹 1、Hystrix 斷路器的原理很簡(jiǎn)單,如同電力過(guò)載保護(hù)器。它可以實(shí)現(xiàn)快速失敗,如果它在一段時(shí)間內(nèi)偵測(cè)到許多類似的錯(cuò)誤,...
閱讀 2681·2021-11-11 16:55
閱讀 1316·2021-09-22 15:25
閱讀 1827·2019-08-29 16:26
閱讀 1019·2019-08-29 13:21
閱讀 2342·2019-08-23 16:19
閱讀 2818·2019-08-23 15:10
閱讀 801·2019-08-23 14:24
閱讀 1881·2019-08-23 13:48