1. 程式人生 > >spring cloud中Ribbon自定義負載均衡策略

spring cloud中Ribbon自定義負載均衡策略

一、Ribbon中的負載均衡策略

1、Ribbon中支援的負載均衡策略

AvailabilityFilteringRule:過濾掉那些因為一直連線失敗的被標記為circuit tripped的後端server,並過濾掉那些高併發的的後端server(active connections 超過配置的閾值) | 使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status裡記錄的各個server的執行狀態

RandomRule:隨機選擇一個server

BestAvailabl:選擇一個最小的併發請求的server,逐個考察Server,如果Server被tripped了,則忽略

RoundRobinRule:roundRobin方式輪詢選擇, 輪詢index,選擇index對應位置的server

WeightedResponseTimeRule:根據響應時間分配一個weight(權重),響應時間越長,weight越小,被選中的可能性越低

RetryRule:對選定的負載均衡策略機上重試機制,在一個配置時間段內當選擇server不成功,則一直嘗試使用subRule的方式選擇一個可用的server

ZoneAvoidanceRule:複合判斷server所在區域的效能和server的可用性選擇server

ResponseTimeWeightedRule:作用同WeightedResponseTimeRule,二者作用是一樣的,ResponseTimeWeightedRule

後來改名為WeightedResponseTimeRule

二、驗證

1、自定義負載均衡策略

# 自定義負載均衡策略
springboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // 自定義使用隨機策略,springboot-h2是服務應用名
2、修改呼叫程式碼
package com.chhliu.springboot.restful.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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;
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	
	@GetMapping("/template/{id}")
	public User findById(@PathVariable Long id) {
		ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");
		System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
				+ serviceInstance.getPort());// 列印當前呼叫服務的資訊
		User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class);
		System.out.println(u);
		return u;
	}
}
3、測試

服務呼叫關係如下:


測試結果如下:

===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7902
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
===:springboot-h2:127.0.0.1:7901
User [id=2, username=user2, name=李四, age=20, balance=100.00]
發現選擇7901埠服務和7902埠服務確實是隨機的!