SpringCloud?客戶端Ribbon負載均衡的實現(xiàn)方法
Ribbon 介紹
Ribbon 是 Netflix 提供的一個基于 Http 和 TCP 的客戶端負載均衡工具,且已集成在 Eureka 依賴中。
實現(xiàn)原理:SpringCloud Ribbon 的底層采用了一個攔截器,攔截了 RestTemplate 發(fā)出的請求,對地址做了修改。
開啟客戶端負載均衡,簡化 RestTemplate 調(diào)用
1)在服務(wù)調(diào)用者的 RestTemplate 配置類上添加注解:
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced // 開啟客戶端負載均衡(默認輪詢策略) public RestTemplate restTemplate(){ return new RestTemplate(); } }
2)在調(diào)用時指定服務(wù)名:
package com.controller; import com.domain.Goods; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * 服務(wù)調(diào)用方 */ @RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/goods/{id}") public Goods findOrderByGoodsId(@PathVariable("id") int id) { String url = String.format("http://eureka-provider/goods/findOne/%d", id); Goods goods = restTemplate.getForObject(url, Goods.class); return goods; } }
負載均衡策略
負載均衡策略:
- 輪詢(默認)
- 隨機
- 最小并發(fā)
- 過濾
- 響應(yīng)時間
- 輪詢重試
- 性能可用性
使用負載均衡:
方式一:使用 bean 的方式
- 在消費者端配置負載均衡策略 Bean:
package com.config; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.context.annotation.Bean; public class MyRule { @Bean public IRule rule() { return new RandomRule(); // 隨機策略 } }
- 在啟動類添加注解:
package com; import com.config.MyRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; @EnableDiscoveryClient // 激活DiscoveryClient @EnableEurekaClient @SpringBootApplication @RibbonClient(name="eureka-provider", configuration= MyRule.class) // 指定服務(wù)提供方并配置負載均衡策略 public class ConsumerApp { public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
方式二:使用配置文件
server: port: 9000 eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:8761/eureka spring: application: name: eureka-consumer # 設(shè)置 Ribbon 的負載均衡策略:隨機策略 EUREKA-PROVIDER: ribbon: NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule.RandomRule
饑餓加載
Ribbon 默認是采用懶加載,即第一次訪問時才會去創(chuàng)建 LoadBalanceClient,請求時間會很長。而饑餓加載則會在項目啟動時創(chuàng)建,達到降低第一次訪問的耗時。
可以通過下面配置開啟饑餓加載:
ribbon: eager-load: enabled: true clients: userservice
到此這篇關(guān)于SpringCloud 客戶端Ribbon負載均衡的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringCloud Ribbon負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java通過XPath獲取XML文件中符合特定條件的節(jié)點
今天小編就為大家分享一篇關(guān)于Java通過XPath獲取XML文件中符合特定條件的節(jié)點,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01Java實現(xiàn)帶有權(quán)重隨機算法的示例詳解
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)帶有權(quán)重隨機算法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10Java Collections集合繼承結(jié)構(gòu)圖_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java Collections集合繼承結(jié)構(gòu)圖_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下2017-04-04