Spring Cloud 系列之負載均衡 Ribbon的示例代碼
1.1 簡介
1.1.1 概述
Ribbon 是 Netflix 發布的負載均衡器,它有助于控制 HTTP 和 TCP 客戶端的行為。為 Ribbon 配置服務提供者地址列表后,Ribbon 就可基于某種負載均衡算法,自動地幫助服務消費者去請求。Ribbon 默認為我們提供了很多的負載均衡算法,例如輪詢、隨機等。當然,我們也可為 Ribbon 實現自定義的負載均衡算法。Ribbon 現在已經進入維護狀態,但目前仍在大規模使用,Spring Cloud 準備使用 LoadBalancer 作為 Ribbon 的替換方案。
1.1.2 相關依賴
因為現在的注冊中心幫我們引入了 Ribbon 我們不需要再次引入就可以直接使用,當然也可以再引入一次沒有影響。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>
1.2 簡單使用
1.2.1 搭建項目
☞ eureka 項目
1.2.2 開啟負載均衡
開啟負載均衡非常簡單,只需要在 RestTemplate 的配置方法上加上 @LoadBalanced 注解即可使用最簡單的輪詢。
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description 消費者啟動類 */@SpringBootApplication@EnableDiscoveryClientpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}
1.2.3 使用負載均衡
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/29 * @description */@RestController@RequestMapping('/consumer')public class ConsumerController { @Autowired private DiscoveryClient discoveryClient; @Autowired private RestTemplate restTemplate; @GetMapping('/go') public void go() { List<ServiceInstance> providerServer = discoveryClient.getInstances('ProviderServer'); if (0 == providerServer.size()) { return; } ServiceInstance serviceInstance = providerServer.get(0); String url = serviceInstance.getUri() + '/provider/get'; System.out.println(url + ' --- '); String str = restTemplate.getForObject(url, String.class); System.out.println(str); }}
喔嚯!報錯了,明明可以訪問到為什么會報找不到實例呢?這是應為咱們使用 DiscoveryClient 直接獲取到了服務提供者集群中某一個的地址,然后讓 RestTemplate 去進行負載均衡。我們都已經拿到了準確地址再怎么進行負載均衡?所以我們這里不能使用 IP 要使用服務名稱。
@RestController@RequestMapping('/consumer')public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping('/go') public void go() { String url = 'http://ProviderServer/provider/get'; String str = restTemplate.getForObject(url, String.class); System.out.println(str); }}
修改完畢之后就可以看到服務提供者是輪換進行提供服務的
1.3 替換負載均衡策略
1.3.1 自帶負載均衡策略
策略名 策略描述 RoundRobinRule 輪詢選擇 server RandomRule 隨機選擇一個 serve RetryRule 先按輪詢策略獲取服務,如果獲取服務失敗則在指定時間內重試,獲取可用服務 BestAvailableRule 會先過濾由于多次訪問故障而處于斷路器跳閘狀態的服務,然后選擇一個并發量最小的服務 AvailabilityFilteringRule 會優先過濾故障實例,再選擇并發較小的實例 WeightedResponseTimeRule 對輪詢的擴展,響應速度越快的實例選擇比重權越大,越容易被選擇 ZoneAvoidanceRule 復合判斷 server 所在區域的性能和 server 的可用性選擇 server
1.3.2 使用配置類
官方文檔明確警告,這個自定義配置類不能放在 @ComponedtScan 所掃描的當前包下以及子包下,否則我們自定義的這個配置類就會被所有的 Ribbon 客戶端所共享。即在 Spring Boot 主程序掃描的包外定義配置類或在與 Spring Boot 主程序的同一級目錄但在排除掃描。配置類創建完畢后為 Spring Boo 主程序添加 @RibbonClient 注解引入配置類,用 name 屬性來指定調用的服務名稱,configuration 屬性指定自定義配置類
@Configurationpublic class MyRibbonRule { @Bean public IRule ribbonRule() { return new RandomRule(); }}
@SpringBootApplication@EnableDiscoveryClient// 用 name 來指定調用的服務名稱,configuration 指定自定義配置類@RibbonClient(name = 'ProviderServer', configuration = MyRibbonRule.class)public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}
1.3.3 配置文件
# 這種配置的優先級低于配置類# 想要調用的服務名稱,即服務提供者名稱ProviderServer: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
☞ 源碼
到此這篇關于Spring Cloud 系列之負載均衡 Ribbon的示例代碼的文章就介紹到這了,更多相關Spring Cloud 負載均衡 Ribbon內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: