国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

瀏覽:104日期:2023-08-20 13:36:42

負(fù)載均衡

使用微服務(wù)后,為了能夠承擔(dān)高并發(fā)的壓力,同一個(gè)服務(wù)可能會(huì)啟動(dòng)多個(gè)實(shí)例。這時(shí)候消費(fèi)者就需要負(fù)載均衡,把請(qǐng)求分散到各個(gè)實(shí)例。負(fù)載均衡主要有兩種設(shè)計(jì):

服務(wù)端負(fù)載均衡客戶端負(fù)載均衡

對(duì)于傳統(tǒng)的分布式服務(wù)來說,大多使用服務(wù)端負(fù)載均衡。一般會(huì)使用Nginx或者ELB等工具作為負(fù)載均衡器,如下圖:

Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

傳統(tǒng)負(fù)載均衡

而在Spring Cloud中,使用的是「客戶端負(fù)載均衡」的方式,使用「Ribbon」組件來實(shí)現(xiàn)客戶端的負(fù)載均衡。只要引入了微服務(wù)注冊(cè)中心依賴,就會(huì)自動(dòng)引入Ribbon依賴。客戶端負(fù)載均衡原理如下圖:

Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

客戶端負(fù)載均衡

Ribbon的原理

Ribbon利用了RestTemplate的攔截器(接口是ClientHttpRequestInterceptor)機(jī)制,在攔截器中實(shí)現(xiàn)的負(fù)載均衡。負(fù)載均衡的基本實(shí)現(xiàn)就是利用從「服務(wù)注冊(cè)中心」獲取可用的服務(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-1service-user-2

代碼:

@RestController@RequestMappingpublic class HelloController { @Value('${spring.cloud.consul.discovery.instanceId}') private String instanceId; @GetMapping('hello') public String hello() { return String.format('hello, this is %s', instanceId); }}

然后對(duì)于消費(fèi)者「service-order」,可以使用Java聲明式注解的方式來使用Ribbon,只需要在消費(fèi)者給RestTemplate類型的Bean配上一個(gè)@LoadBalanced就可以了。然后再配置一下負(fù)載均衡策略:

@Configurationpublic class RibbonConfig { @Bean @LoadBalanced public RestTemplate ribbonRestTemplate(){ return new RestTemplate(); } @Bean public IRule ribbonRule() { return new RandomRule(); // 隨機(jī)負(fù)載均衡 }}

然后就可以直接使用自動(dòng)注入的RestTemplate類型的Bean了:

@RestController@RequestMappingpublic 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-2hello, this is service-user-1

負(fù)載均衡策略

查看IRule接口的實(shí)現(xiàn)類,可以看到Ribbon的所有負(fù)載均衡策略,查看各實(shí)現(xiàn)類頂部的注釋可以看到它的具體策略:

Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解

負(fù)載均衡器

RandomRule:隨機(jī)選擇; RoundRobinRule:輪詢; WeightedResponseTimeRule:根據(jù)每個(gè)服務(wù)的響應(yīng)時(shí)間設(shè)置權(quán)重,響應(yīng)時(shí)間越長(zhǎng),所占權(quán)重越少; AvailabilityFilteringRule:過濾掉那些因?yàn)橐恢边B接失敗的被標(biāo)記為circuit tripped的后端server,并過濾掉那些高并發(fā)的的后端server(active connections 超過配置的閾值); ZoneAvoidanceRule:使用CompositePredicate根據(jù)區(qū)域和可用性過濾服務(wù)器的規(guī)則; BestAvailableRule:選擇一個(gè)最小的并發(fā)請(qǐng)求的server; RetryRule:在現(xiàn)有的策略基礎(chǔ)上,添加重試機(jī)制,因?yàn)镮Rule支持「級(jí)聯(lián)」。

遠(yuǎn)程調(diào)用

Spring Cloud提供了OpenFeign組件(以前叫Feign)來進(jìn)行遠(yuǎn)程的HTTP調(diào)用。它是對(duì)RestTemplate的一個(gè)封裝。

Feign是一個(gè)聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡(jiǎn)單。Spring Cloud對(duì)Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與微服務(wù)注冊(cè)中心和Ribbon組合使用以支持負(fù)載均衡。

使用OpenFeign

第一步,引入依賴:

implementation ’org.springframework.cloud:spring-cloud-starter-openfeign’

第二步,啟動(dòng)配置,在啟動(dòng)類上添加@EnableFeignClients注解:

@SpringBootApplicationbr/>@EnableFeignClientspublic 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請(qǐng)求的時(shí)候有一個(gè)小坑,詳情參考:SpringCloud Feign Post表單請(qǐng)求。

@FeignClient('service-user')public interface UserClient { @GetMapping('hello') String getUserHello();}

第四步:使用,注解使用@Autowired注解注入就可以了:

@RestController@RequestMappingpublic 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'; }}

是不是看起來非常簡(jiǎn)單?

OpenFeign集成Ribbon

前面我們提到,OpenFeign底層是對(duì)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ù)載均衡。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 高阳县| 留坝县| 老河口市| 昌都县| 福贡县| 敦化市| 平南县| 谢通门县| 和龙市| 林甸县| 北川| 昂仁县| 子洲县| 青川县| 黄山市| 靖西县| 永清县| 胶南市| 乌兰浩特市| 湖南省| 中山市| 德格县| 仪陇县| 奎屯市| 安康市| 云浮市| 阳信县| 汨罗市| 美姑县| 乌拉特中旗| 怀集县| 福清市| 交城县| 南昌县| 龙江县| 黑河市| 泰兴市| 英德市| 阳西县| 肇东市| 铁岭县|