亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳細介紹SpringCloud之Ribbon

 更新時間:2018年01月16日 16:38:13   作者:Bob_F  
本篇文章主要介紹了SpringCloud之Ribbon,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一:Ribbon是什么?

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

二:LB方案分類

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

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

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

ServerList

用于獲取地址列表。它既可以是靜態(tài)的(提供一組固定的地址),也可以是動態(tài)的(從注冊中心中定期查詢地址列表)。

ServerListFilter

僅當使用動態(tài)ServerList時使用,用于在原始的服務列表中使用一定策略過慮掉一部分地址。

IRule

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

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

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

1:簡單輪詢負載均衡(RoundRobin)

以輪詢的方式依次將請求調(diào)度不同的服務器,即每次調(diào)度執(zhí)行i = (i + 1) mod n,并選出第i臺服務器。

2:隨機負載均衡 (Random)

隨機選擇狀態(tài)為UP的Server

3:加權響應時間負載均衡 (WeightedResponseTime)

根據(jù)相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。

4:區(qū)域感知輪詢負載均衡(ZoneAvoidanceRule)

復合判斷server所在區(qū)域的性能和server的可用性選擇server

Ribbon自帶負載均衡策略比較

策略名 策略聲明 策略描述 實現(xiàn)說明
BestAvailableRule public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule 選擇一個最小的并發(fā)請求的server 逐個考察Server,如果Server被tripped了,則忽略,在選擇其中ActiveRequestsCount最小的server
AvailabilityFilteringRule public class AvailabilityFilteringRule extends PredicateBasedRule 過濾掉那些因為一直連接失敗的被標記為circuit tripped的后端server,并過濾掉那些高并發(fā)的的后端server(active connections 超過配置的閾值) 使用一個AvailabilityPredicate來包含過濾server的邏輯,其實就就是檢查status里記錄的各個server的運行狀態(tài)
WeightedResponseTimeRule public class WeightedResponseTimeRule extends RoundRobinRule 根據(jù)相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。 一 個后臺線程定期的從status里面讀取評價響應時間,為每個server計算一個weight。Weight的計算也比較簡單responsetime 減去每個server自己平均的responsetime是server的權重。當剛開始運行,沒有形成statas時,使用roubine策略選擇 server。
RetryRule public class RetryRule extends AbstractLoadBalancerRule 對選定的負載均衡策略機上重試機制。 在一個配置時間段內(nèi)當選擇server不成功,則一直嘗試使用subRule的方式選擇一個可用的server
RoundRobinRule public class RoundRobinRule extends AbstractLoadBalancerRule roundRobin方式輪詢選擇server 輪詢index,選擇index對應位置的server
RandomRule public class RandomRule extends AbstractLoadBalancerRule 隨機選擇一個server 在index上隨機,選擇index對應位置的server
ZoneAvoidanceRule public class ZoneAvoidanceRule extends PredicateBasedRule 復合判斷server所在區(qū)域的性能和server的可用性選擇server 使 用ZoneAvoidancePredicate和AvailabilityPredicate來判斷是否選擇某個server,前一個判斷判定一個 zone的運行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于過濾掉連接數(shù)過多的 Server。

五:Ribbon單獨使用

創(chuàng)建一個maven工程 名稱 ribbon_client

pom內(nèi)容

    <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 創(chuàng)建客戶端和負載均衡器;

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

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

動態(tài)修正配置中的服務器池;

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

打印出負載均衡器記錄的服務器統(tǒng)計信息。

六:Ribbon結合eureka使用

先要啟動eureka_register_service工程(注冊中心)和biz-service-0工程(服務生產(chǎn)者)

創(chuàng)建maven工程 eureka_ribbon_client 該工程啟動和相關配置依賴eureka_register_service和biz-service-0

pom加入

<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注解來添加發(fā)現(xiàn)服務能力。創(chuàng)建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);
  }
} 

創(chuàng)建ConsumerController來消費biz-service-0的getuser服務。通過直接RestTemplate來調(diào)用服務

@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();
  }

} 

application.properties中配置eureka服務注冊中心

spring.application.name=ribbon-consumer
server.port=8003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

完成后可以打開http://localhost:8003/getuserinfo 可以看到結果

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

代碼地址:https://github.com/zhp8341/SpringCloudDemo

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 通過實例了解java TransferQueue

    通過實例了解java TransferQueue

    這篇文章主要介紹了TransferQueue實例,下面小編和大家一起來學習一下
    2019-05-05
  • Hibernatede 一對多映射配置方法(分享)

    Hibernatede 一對多映射配置方法(分享)

    下面小編就為大家?guī)硪黄狧ibernatede 一對多映射配置方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 淺析JVM垃圾回收的過程

    淺析JVM垃圾回收的過程

    這篇文章主要介紹了JVM垃圾回收的過程,幫助大家更好的理解和學習Java中的垃圾回收機制,感興趣的朋友可以了解下
    2020-09-09
  • JWT原理與java操作jwt驗證詳解

    JWT原理與java操作jwt驗證詳解

    這篇文章主要介紹了JWT原理與java操作jwt驗證,詳細分析了JWT的基本概念、原理與java基于JWT進行token驗證的相關操作技巧,需要的朋友可以參考下
    2023-06-06
  • Java中的轉換流InputStreamReader解讀

    Java中的轉換流InputStreamReader解讀

    InputStreamReader是Java.io包中的一個類,用于將字節(jié)輸入流轉換為字符輸入流,它繼承自java.io.Reader類,提供了兩種構造方法,可以使用默認或指定字符集創(chuàng)建實例,常用方法包括讀取字符、判斷是否準備好讀取數(shù)據(jù)和關閉流
    2024-09-09
  • Java定時調(diào)用.ktr文件的示例代碼(解決方案)

    Java定時調(diào)用.ktr文件的示例代碼(解決方案)

    這篇文章主要介紹了Java定時調(diào)用.ktr文件的示例代碼,本文給大家分享遇到問題及解決方法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • SpringBoot內(nèi)置tomcat調(diào)優(yōu)測試優(yōu)化

    SpringBoot內(nèi)置tomcat調(diào)優(yōu)測試優(yōu)化

    這篇文章主要介紹了SpringBoot內(nèi)置tomcat調(diào)優(yōu)測試優(yōu)化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • SpringBoot中實現(xiàn)接收文件和對象

    SpringBoot中實現(xiàn)接收文件和對象

    這篇文章主要介紹了SpringBoot實現(xiàn)接收文件和對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Gateway如何實現(xiàn)全局跨域

    Gateway如何實現(xiàn)全局跨域

    這篇文章主要介紹了Gateway如何實現(xiàn)全局跨域問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • SpringBoot3整合SpringDoc實現(xiàn)在線接口文檔的詳細過程

    SpringBoot3整合SpringDoc實現(xiàn)在線接口文檔的詳細過程

    這篇文章主要介紹了SpringBoot3整合SpringDoc實現(xiàn)在線接口文檔的詳細過程,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-06-06

最新評論