Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解
負(fù)載均衡
使用微服務(wù)后,為了能夠承擔(dān)高并發(fā)的壓力,同一個(gè)服務(wù)可能會(huì)啟動(dòng)多個(gè)實(shí)例。這時(shí)候消費(fèi)者就需要負(fù)載均衡,把請求分散到各個(gè)實(shí)例。負(fù)載均衡主要有兩種設(shè)計(jì):
服務(wù)端負(fù)載均衡客戶端負(fù)載均衡
對于傳統(tǒng)的分布式服務(wù)來說,大多使用服務(wù)端負(fù)載均衡。一般會(huì)使用Nginx或者ELB等工具作為負(fù)載均衡器,如下圖:
傳統(tǒng)負(fù)載均衡
而在Spring Cloud中,使用的是「客戶端負(fù)載均衡」的方式,使用「Ribbon」組件來實(shí)現(xiàn)客戶端的負(fù)載均衡。只要引入了微服務(wù)注冊中心依賴,就會(huì)自動(dòng)引入Ribbon依賴??蛻舳素?fù)載均衡原理如下圖:
客戶端負(fù)載均衡
Ribbon的原理
Ribbon利用了RestTemplate的攔截器(接口是ClientHttpRequestInterceptor)機(jī)制,在攔截器中實(shí)現(xiàn)的負(fù)載均衡。負(fù)載均衡的基本實(shí)現(xiàn)就是利用從「服務(wù)注冊中心」獲取可用的服務(wù)地址列表,然后通過一定算法負(fù)載,決定使用哪一個(gè)服務(wù)地址來進(jìn)行HTTP調(diào)用。
詳情可以查看LoadBalancerInterceptor這個(gè)類,位于org.springframework.cloud.client.loadbalancer包下。
使用Ribbon
首先定義一下生產(chǎn)者「service-user」,我這里使用容器啟動(dòng)了多個(gè)生產(chǎn)者實(shí)例,每個(gè)實(shí)例具有不同的id,我的示例代碼里面啟動(dòng)了兩個(gè)實(shí)例:
service-user-1
service-user-2
代碼:
@RestController @RequestMapping public class HelloController { @Value("${spring.cloud.consul.discovery.instanceId}") private String instanceId; @GetMapping("hello") public String hello() { return String.format("hello, this is %s", instanceId); } }
然后對于消費(fèi)者「service-order」,可以使用Java聲明式注解的方式來使用Ribbon,只需要在消費(fèi)者給RestTemplate類型的Bean配上一個(gè)@LoadBalanced就可以了。然后再配置一下負(fù)載均衡策略:
@Configuration public class RibbonConfig { @Bean @LoadBalanced public RestTemplate ribbonRestTemplate(){ return new RestTemplate(); } @Bean public IRule ribbonRule() { return new RandomRule(); // 隨機(jī)負(fù)載均衡 } }
然后就可以直接使用自動(dòng)注入的RestTemplate類型的Bean了:
@RestController @RequestMapping public class HelloController { @Autowired RestTemplate restTemplate; @GetMapping("/ribbon/service-user") public String ribbonService(){ return restTemplate.getForObject("http://service-user/hello", String.class); } }
這個(gè)時(shí)候訪問消費(fèi)者的/ribbon/service-user,刷新幾次,就會(huì)看到下面兩個(gè)隨機(jī)的響應(yīng):
hello, this is service-user-2
hello, this is service-user-1
負(fù)載均衡策略
查看IRule接口的實(shí)現(xiàn)類,可以看到Ribbon的所有負(fù)載均衡策略,查看各實(shí)現(xiàn)類頂部的注釋可以看到它的具體策略:
負(fù)載均衡器
- RandomRule:隨機(jī)選擇;
- RoundRobinRule:輪詢;
- WeightedResponseTimeRule:根據(jù)每個(gè)服務(wù)的響應(yīng)時(shí)間設(shè)置權(quán)重,響應(yīng)時(shí)間越長,所占權(quán)重越少;
- AvailabilityFilteringRule:過濾掉那些因?yàn)橐恢边B接失敗的被標(biāo)記為circuit tripped的后端server,并過濾掉那些高并發(fā)的的后端server(active connections 超過配置的閾值);
- ZoneAvoidanceRule:使用CompositePredicate根據(jù)區(qū)域和可用性過濾服務(wù)器的規(guī)則;
- BestAvailableRule:選擇一個(gè)最小的并發(fā)請求的server;
- RetryRule:在現(xiàn)有的策略基礎(chǔ)上,添加重試機(jī)制,因?yàn)镮Rule支持「級聯(lián)」。
遠(yuǎn)程調(diào)用
Spring Cloud提供了OpenFeign組件(以前叫Feign)來進(jìn)行遠(yuǎn)程的HTTP調(diào)用。它是對RestTemplate的一個(gè)封裝。
Feign是一個(gè)聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單。Spring Cloud對Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與微服務(wù)注冊中心和Ribbon組合使用以支持負(fù)載均衡。
使用OpenFeign
第一步,引入依賴:
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
第二步,啟動(dòng)配置,在啟動(dòng)類上添加@EnableFeignClients注解:
@SpringBootApplicationbr/>@EnableFeignClients public class ServiceOrderApplication { public static void main(String[] args) { SpringApplication.run(ServiceOrderApplication.class, args); } }
第三步:聲明調(diào)用接口,我這里案例是service-order調(diào)用service-user的/hello接口,這里的注解就跟Spring MVC的注解一致:
注意Feign在做POST請求的時(shí)候有一個(gè)小坑,詳情參考:SpringCloud Feign Post表單請求。
@FeignClient("service-user") public interface UserClient { @GetMapping("hello") String getUserHello(); }
第四步:使用,注解使用@Autowired注解注入就可以了:
@RestController @RequestMapping public class HelloController { @Autowired UserClient userClient; @GetMapping("hello") public String hello() { return "hello, this is order service"; } @GetMapping("userHello") public String user() { return userClient.getUserHello() + ", this is order-service"; } }
是不是看起來非常簡單?
OpenFeign集成Ribbon
前面我們提到,OpenFeign底層是對RestTemplate的一個(gè)封裝,而Ribbon是通過給RestTemplate添加過濾器來實(shí)現(xiàn)的,所以O(shè)penFeign天生就自動(dòng)集成了Ribbon,我們不需要任何額外的配置。
在上述代碼中,啟動(dòng)后,可以訪問service-order的/userHello端口,不斷刷新,發(fā)現(xiàn)已經(jīng)實(shí)現(xiàn)了負(fù)載均衡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡功能
- SpringCloud客戶端的負(fù)載均衡Ribbon的實(shí)現(xiàn)
- Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的方法
- 詳解Spring Cloud負(fù)載均衡重要組件Ribbon中重要類的用法
- Spring Cloud 負(fù)載均衡器 Ribbon原理及實(shí)現(xiàn)
- Spring Cloud Ribbon負(fù)載均衡器處理方法
- Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的示例
- 詳解spring cloud中使用Ribbon實(shí)現(xiàn)客戶端的軟負(fù)載均衡
- spring cloud 之 客戶端負(fù)載均衡Ribbon深入理解
相關(guān)文章
Java實(shí)現(xiàn)利用廣度優(yōu)先遍歷(BFS)計(jì)算最短路徑的方法
這篇文章主要介紹了Java實(shí)現(xiàn)利用廣度優(yōu)先遍歷(BFS)計(jì)算最短路徑的方法,實(shí)例分析了廣度優(yōu)先遍歷算法的原理與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04Java中String類(字符串操作)的10個(gè)常見問題和解決方法
這篇文章主要介紹了Java中String類(字符串)操作的10個(gè)常見問題,需要的朋友可以參考下2014-04-04MyBatis動(dòng)態(tài)SQL之<choose><when><o(jì)therwise>標(biāo)簽的使用
MyBatis中動(dòng)態(tài)語句choose-when-otherwise 類似于Java中的switch-case-default語句,本文就來介紹一下MyBatis動(dòng)態(tài)SQL之<choose><when><o(jì)therwise>標(biāo)簽的使用,感興趣的可以了解一下2023-09-09java實(shí)現(xiàn)文件切片上傳百度云+斷點(diǎn)續(xù)傳的方法
文件續(xù)傳在很多地方都可以用的到,本文主要介紹了java實(shí)現(xiàn)文件切片上傳百度云+斷點(diǎn)續(xù)傳的方法,?文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(7)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07