詳解spring cloud中使用Ribbon實(shí)現(xiàn)客戶(hù)端的軟負(fù)載均衡
開(kāi)篇
本例是在springboot整合H2內(nèi)存數(shù)據(jù)庫(kù),實(shí)現(xiàn)單元測(cè)試與數(shù)據(jù)庫(kù)無(wú)關(guān)性和使用RestTemplate消費(fèi)spring boot的Restful服務(wù)兩個(gè)示例的基礎(chǔ)上改造而來(lái)
在使用RestTemplate來(lái)消費(fèi)spring boot的Restful服務(wù)示例中,我們提到,調(diào)用spring boot服務(wù)的時(shí)候,需要將服務(wù)的URL寫(xiě)死或者是寫(xiě)在配置文件中,但這兩種方式,無(wú)論哪一種,一旦ip地址發(fā)生了變化,都需要改動(dòng)程序,并重新部署服務(wù),使用Ribbon的時(shí)候,可以有效的避免這個(gè)問(wèn)題。
前言:
軟負(fù)載均衡的實(shí)現(xiàn)方式有兩種,分別是服務(wù)端的負(fù)載均衡和客戶(hù)端的負(fù)載均衡
服務(wù)端負(fù)載均衡:當(dāng)瀏覽器向后臺(tái)發(fā)出請(qǐng)求的時(shí)候,會(huì)首先向反向代理服務(wù)器發(fā)送請(qǐng)求,反向代理服務(wù)器會(huì)根據(jù)客戶(hù)端部署的ip:port映射表以及負(fù)載均衡策略,來(lái)決定向哪臺(tái)服務(wù)器發(fā)送請(qǐng)求,一般會(huì)使用到nginx反向代理技術(shù)。
客戶(hù)端負(fù)載均衡:當(dāng)瀏覽器向后臺(tái)發(fā)出請(qǐng)求的時(shí)候,客戶(hù)端會(huì)向服務(wù)注冊(cè)器(例如:Eureka Server),拉取注冊(cè)到服務(wù)器的可用服務(wù)信息,然后根據(jù)負(fù)載均衡策略,直接命中哪臺(tái)服務(wù)器發(fā)送請(qǐng)求。這整個(gè)過(guò)程都是在客戶(hù)端完成的,并不需要反向代理服務(wù)器的參與。
一、啟動(dòng)Eureka Server
請(qǐng)參考該例:spring cloud中啟動(dòng)Eureka Server
二、啟動(dòng)微服務(wù),并注冊(cè)到Eureka Server上
spring cloud-將spring boot服務(wù)注冊(cè)到Eureka Server上
為了演示負(fù)載均衡的效果,再啟動(dòng)一個(gè)為服務(wù),注意需要將端口號(hào)改成不一致
三、添加Ribbon支持
1、添加Ribbon的依賴(lài)
2、添加負(fù)載均衡支持
package com.chhliu.springboot.restful; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; 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; @SpringBootApplication @EnableEurekaClient public class SpringbootRestTemplateApplication { @Autowired private RestTemplateBuilder builder; @Bean @LoadBalanced // 添加負(fù)載均衡支持,很簡(jiǎn)單,只需要在RestTemplate上添加@LoadBalanced注解,那么RestTemplate即具有負(fù)載均衡的功能,如果不加@LoadBalanced注解的話(huà),會(huì)報(bào)java.net.UnknownHostException:springboot-h2異常,此時(shí)無(wú)法通過(guò)注冊(cè)到Eureka Server上的服務(wù)名來(lái)調(diào)用服務(wù),因?yàn)镽estTemplate是無(wú)法從服務(wù)名映射到ip:port的,映射的功能是由LoadBalancerClient來(lái)實(shí)現(xiàn)的。 public RestTemplate restTemplate() { return builder.build(); } public static void main(String[] args) { SpringApplication.run(SpringbootRestTemplateApplication.class, args); } }
3、修改調(diào)用微服務(wù)的URL
package com.chhliu.springboot.restful.controller; 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; import com.chhliu.springboot.restful.vo.User; @RestController public class RestTemplateController { @Autowired private RestTemplate restTemplate; @GetMapping("/template/{id}") public User findById(@PathVariable Long id) {// 將原來(lái)的ip:port的形式,改成注冊(cè)到Eureka Server上的應(yīng)用名即可 User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class); System.out.println(u); return u; } }
四、查看Eureka Server狀態(tài)
五,在瀏覽器中,多次刷新http://localhost:7904/template/2地址
六、測(cè)試結(jié)果
7900端口服務(wù):
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
7901端口服務(wù):
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
7904端口服務(wù):
User [id=2, username=user2, name=李四, age=20, balance=100.00] 2017-01-23 09:58:05.682 INFO 7436 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: springboot-h2.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00]
從上面的測(cè)試結(jié)果可以看出,總共調(diào)了7904端口服務(wù)9次,其中7904端口服務(wù)調(diào)7900端口服務(wù)4次,調(diào)7901端口5次,剛好是9次
經(jīng)過(guò)上面的幾個(gè)步驟,就基本使用Ribbon實(shí)現(xiàn)了客戶(hù)端負(fù)載均衡的功能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud學(xué)習(xí)筆記之OpenFeign進(jìn)行服務(wù)調(diào)用
OpenFeign對(duì)feign進(jìn)行進(jìn)一步的封裝,添加了springmvc的一些功能,更加強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于SpringCloud學(xué)習(xí)筆記之OpenFeign進(jìn)行服務(wù)調(diào)用的相關(guān)資料,需要的朋友可以參考下2022-01-01java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要介紹了java中struts2實(shí)現(xiàn)文件上傳下載功能的方法,以實(shí)例形式較為詳細(xì)的分析了struts2實(shí)現(xiàn)文件上傳下載功能的具體實(shí)現(xiàn)技巧與相關(guān)問(wèn)題的解決方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01解決Tomcat啟動(dòng)報(bào)異常java.lang.ClassNotFoundException問(wèn)題
這篇文章主要介紹了解決Tomcat啟動(dòng)報(bào)異常java.lang.ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟
這篇文章主要介紹了springboot 啟動(dòng)時(shí)初始化數(shù)據(jù)庫(kù)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01Jmeter如何獲取jtl文件中所有的請(qǐng)求報(bào)文詳解
JMeter的可以創(chuàng)建一個(gè)包含測(cè)試運(yùn)行結(jié)果的文本文件,這些通常稱(chēng)為JTL文件,因?yàn)檫@是默認(rèn)擴(kuò)展名,但可以使用任何擴(kuò)展名,這篇文章主要給大家介紹了關(guān)于Jmeter如何獲取jtl文件中所有的請(qǐng)求報(bào)文的相關(guān)資料,需要的朋友可以參考下2021-09-09IntelliJ?IDEA?2022安裝注冊(cè)永久激活
java開(kāi)發(fā)工具IntelliJ?IDEA深受用戶(hù)喜愛(ài),很多朋友對(duì)這個(gè)idea開(kāi)發(fā)工具比較忠心,一旦有新版本發(fā)出,很多小伙伴就迫不及待的想更新,今天小編給大家?guī)?lái)了idea2022.1最新永久激活碼,親測(cè)有效,喜歡的朋友快來(lái)下載體驗(yàn)吧2022-08-08