1. 程式人生 > >SpringCloud之Ribbon

SpringCloud之Ribbon

max request .so fig 配置 edr resources app www.

一:Ribbon是什麽?

   Ribbon是Netflix發布的開源項目,主要功能是提供客戶端的軟件負載均衡算法,將Netflix的中間層服務連接在一起。Ribbon客戶端組件提供一系列完善的配置項如連接超時,重試等。簡單的說,就是在配置文件中列出Load Balancer(簡稱LB)後面所有的機器,Ribbon會自動的幫助你基於某種規則(如簡單輪詢,隨即連接等)去連接這些機器。我們也很容易使用Ribbon實現自定義的負載均衡算法。

二:LB方案分類  

  目前主流的LB方案可分成兩類:一種是集中式LB, 即在服務的消費方和提供方之間使用獨立的LB設施(可以是硬件,如F5, 也可以是軟件,如nginx), 由該設施負責把訪問請求通過某種策略轉發至服務的提供方;另一種是進程內LB,將LB邏輯集成到消費方,消費方從服務註冊中心獲知有哪些地址可用,然後自己再從這些地址中選擇出一個合適的服務器。Ribbon就屬於後者,它只是一個類庫,集成於消費方進程,消費方通過它來獲取到服務提供方的地址。

三:Ribbon的主要組件與工作流程

  Ribbon的核心組件(均為接口類型)有以下幾個:

  ServerList:用於獲取地址列表。它既可以是靜態的(提供一組固定的地址),也可以是動態的(從註冊中心中定期查詢地址列表)。

  ServerListFilter:僅當使用動態ServerList時使用,用於在原始的服務列表中使用一定策略過慮掉一部分地址。

  IRule:選擇一個最終的服務地址作為LB結果。選擇策略有輪詢、根據響應時間加權、斷路器(當Hystrix可用時)等。

  Ribbon在工作時首選會通過ServerList來獲取所有可用的服務列表,然後通過ServerListFilter過慮掉一部分地址,最後在剩下的地址中通過IRule選擇出一臺服務器作為最終結果。

四:Ribbon提供的主要負載均衡策略介紹

  1:簡單輪詢負載均衡(RoundRobin):以輪詢的方式依次將請求調度不同的服務器,即每次調度執行i = (i + 1) mod n,並選出第i臺服務器。

  2:隨機負載均衡 (Random): 隨機選擇狀態為UP的Server

  3:加權響應時間負載均衡 (WeightedResponseTime):根據相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。

  4:區域感知輪詢負載均衡(ZoneAvoidanceRule):復合判斷server所在區域的性能和server的可用性選擇server

Ribbon自帶負載均衡策略比較

五:Ribbon單獨使用

  創建一個maven工程,名稱是ribbon_client,pom的內容如下:

<dependencies>
        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-core</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-httpclient</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

sample-client.properties配置文件

# Max number of retries
sample-client.ribbon.MaxAutoRetries=1

# Max number of next servers to retry (excluding the first server)
sample-client.ribbon.MaxAutoRetriesNextServer=1

# Whether all operations can be retried for this client
sample-client.ribbon.OkToRetryOnAllOperations=true

# Interval to refresh the server list from the source
sample-client.ribbon.ServerListRefreshInterval=2000

# Connect timeout used by Apache HttpClient
sample-client.ribbon.ConnectTimeout=3000

# Read timeout used by Apache HttpClient
sample-client.ribbon.ReadTimeout=3000

# Initial list of servers, can be changed via Archaius dynamic property at runtime
sample-client.ribbon.listOfServers=www.sohu.com:80,www.163.com:80,www.sina.com.cn:80

sample-client.ribbon.EnablePrimeConnections=true

RibbonMain代碼

import java.net.URI;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient;

public class RibbonMain {
    public static void main( String[] args ) throws Exception {  
        ConfigurationManager.loadPropertiesFromResources("sample-client.properties");  
        System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));  
          
        RestClient client = (RestClient)ClientFactory.getNamedClient("sample-client");  
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();  
          
        for(int i = 0; i < 4; i ++) {  
            HttpResponse response = client.executeWithLoadBalancer(request);  
            System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());  
        }  
          
        ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();  
        System.out.println(lb.getLoadBalancerStats());  
          
        ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "ccblog.cn:80,www.linkedin.com:80");  
          
        System.out.println("changing servers ...");  
        Thread.sleep(3000);  
          
        for(int i = 0; i < 3; i ++) {  
            HttpResponse response = client.executeWithLoadBalancer(request);  
            System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());  
        }  
        System.out.println(lb.getLoadBalancerStats());  
    }  
}

代碼解析

  使用 Archaius ConfigurationManager 加載屬性;

  使用 ClientFactory 創建客戶端和負載均衡器;

  使用 builder 構建 http 請求。註意我們只支持 URI 的 "/" 部分的路徑,一旦服務器被負載均衡器選中,會由客戶端計算出完整的 URI;

  調用 API client.executeWithLoadBalancer(),不是 exeucte() API;

  動態修正配置中的服務器池;

  等待服務器列表刷新(配置文件中定義的刷新間隔是為 3 秒鐘);

  打印出負載均衡器記錄的服務器統計信息。

六:Ribbon結合eureka使用

  創建maven工程 eureka_ribbon_client 該工程啟動和相關配置依賴:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Brixton.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    </dependencies>
</dependencyManagement>

  在應用主類中,通過@EnableDiscoveryClient註解來添加發現服務能力。創建RestTemplate實例,並通過@LoadBalanced註解開啟均衡負載能力。

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }
}

  創建ConsumerController來消費service01的getuser服務。通過直接RestTemplate來調用服務

@RestController
public class ConsumerController {
 
    @Autowired
    RestTemplate restTemplate;
 
    @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
    public String add() {
        return restTemplate.getForEntity("http://biz-service-0/getuser", String.class).getBody();
    }
}

  Ribbon其實就是一個軟負載均衡的客戶端組件,他可以和其他所需請求的客戶端結合使用,和eureka結合只是其中的一個實例。

SpringCloud之Ribbon