Feign Ribbon Hystrix relationship among | the most complete history, the depth of analysis

The most complete history: Feign Ribbon Hystrix the relations between | depth analysis

Crazy maker culture circle Java [one hundred million concurrent high flow chat room] actual combat series of 15 [ blog park entrance total ]


Foreword

In the application of micro-services architecture, Feign, Hystrix, Ribbon three are essential, it can be said to have become a triangle.

Crazy maker culture circle (high concurrent learning community created by the author Nin), many small partners asked to Bennion, on Feign, Hystrix, the Ribbon relationship between the three, and three timeout configuration. Up to now, the whole network is no clear description of the article, so, a little Bennion close-up detailed article, analyze it.

Feign Introduction

Feign is written in the Java language HttpClient a binder for implementation declaration between the micro-micro service in Spring Cloud service invocation. Feign can define the interfaces to other service requests for service calls between micro, do not have to write http request, implemented in the client calls this interface as remote call any other service, when a request to implement the interface can be called an error class to return

Feign is a web service client a declarative, it makes writing web service clients easier. Creating interfaces, add annotations for the interface, you can use Feign. Feign Feign annotations may be used or JAX-RS annotations, also hot swappable the encoder and decoder. Spring Cloud Add to Feign a Spring MVC annotation support and integrated Ribbon and Eureka to provide load balancing when using Feign.

feign source github address:

https://github.com/OpenFeign/feign

Ribbon Introduction

Ribbon as load balancing, the client implementation, service segment can be started two different ports but the same service servername

Ribbon is Netflix released open source project, the main function is to provide the client software load balancing algorithm, an intermediate layer connecting the Netflix service together. Ribbon client components, such as a series of perfect CI connection timeout, retry the like. Simply put, is to list all the machines behind the Load Balancer in the configuration file, Ribbon will automatically help you to connect these machines based on certain rules (such as a simple polling, random connections, etc.). We are also very easy to use load balancing algorithm Ribbon customizable. Simply put, Ribbon is a client load balancer.

Ribbon is divided into two steps at work: The first step is to select Eureka Server, it preferred the same Zone and less loaded Server in; the second step and then based on user-specified policies, select Server from the registration list to get services an address. Ribbon which provides a variety of strategies, such as polling, random, weighted in accordance with the response time and the like.

ribbon github source address:

https://github.com/Netflix/ribbon

Hystrix Introduction

As Hystrix fuse flow control, the client implementations, the annotation process can be invoked when a request error annotation Returns

Hystrix fuse, fault management tool designed to control third-party library services and nodes by fusing mechanism, thereby providing a more robust fault tolerance for delay and failure. Achieve a series of service protection thread isolation, circuit breakers, etc. in Spring Cloud Hystrix in. It is also based on open source Netflix framework of Hystrix implemented, the framework aims by controlling access to those remote node systems, services and third-party libraries, providing a more robust fault tolerance for delay and failure. Hystrix have a service degradation, service fuse, thread isolation, request caching, request merging and service monitoring and other powerful features.

Hystrix source github address:

https://github.com/Netflix/hystrix

Focus: the relationship between the three

If the micro services plus a spring-cloud-starter-netflix-hystrix dependent, then, feign will automatically be packaged with all methods hystrix through a proxy mode.

In Spring Cloud micro-service system, between the micro-services call each other can be declared invocation by Feign, Feign will get micro-target service from the service registry service call in this process through the Ribbon server address list, after a network request Ribbon will process the request in a load-balanced way to hit on different instances of micro-services in order to achieve Spring Cloud micro-services architecture that is the most critical function of service discovery and client load balancing call.

On the other hand micro service during a call to each other in order to prevent the failure of a micro-services consume all resources connected micro services throughout the system, so in the implementation of micro-service call, we will ask the caller is calling for the implementation of micro fuse logic services. To achieve this logic micro scene in Spring Cloud service framework we are through Hystrix this framework to achieve.

The caller will call for service is called micro-set timeout, the timeout will enter fuse logic once, and this information will be returned to the fault indicator Hystrix components, Hystrix fuse assembly will be judged according to the situation is a fault tone micro services to open the fuse It is, after all the requests for the service will go directly to the micro-fuse logic, until the micro adjustment is restored service failure, MAMMALIA, closes the circuit breaker.

The relationship between the three, as follows:

Here Insert Picture Description

Feign typical configuration instructions

Feign HttpClient itself may support a variety of kits, and the Apache HttpClient OkHttp e.g., a typical configuration for the Apache HttpClient follows:


feign:
  #替换掉JDK默认HttpURLConnection实现的 Http Client
  httpclient:
    enabled: true
  hystrix:
    enabled: true
  client:
    config:
      default:
       #连接超时时间
        connectTimeout: 5000
       #读取超时时间
        readTimeout: 5000

Hystrix configuration instructions

Spring Cloud service in the micro-system Hystrix mainly be used to achieve implementation calls the failure of fuse, overload protection and resource isolation function between the micro-service network.

hystrix:
  propagate:
    request-attribute:
      enabled: true
  command:
    #全局默认配置
    default:
      #线程隔离相关
      execution:
        timeout:
          #是否给方法执行设置超时时间,默认为true。一般我们不要改。
          enabled: true
        isolation:
          #配置请求隔离的方式,这里是默认的线程池方式。还有一种信号量的方式semaphore,使用比较少。
          strategy: threadPool
          thread:
            #方式执行的超时时间,默认为1000毫秒,在实际场景中需要根据情况设置
            timeoutInMilliseconds: 10000
            #发生超时时是否中断方法的执行,默认值为true。不要改。
            interruptOnTimeout: true
            #是否在方法执行被取消时中断方法,默认值为false。没有实际意义,默认就好!
            interruptOnCancel: false
  circuitBreaker:   #熔断器相关配置
    enabled: true   #是否启动熔断器,默认为true,false表示不要引入Hystrix。
    requestVolumeThreshold: 20     #启用熔断器功能窗口时间内的最小请求数,假设我们设置的窗口时间为10秒,
    sleepWindowInMilliseconds: 5000    #所以此配置的作用是指定熔断器打开后多长时间内允许一次请求尝试执行,官方默认配置为5秒。
    errorThresholdPercentage: 50   #窗口时间内超过50%的请求失败后就会打开熔断器将后续请求快速失败掉,默认配置为50

Ribbon configuration instructions

Ribbon in Spring Cloud support for communication between the micro-service plays a crucial role, its main functions include load balancer and a client for the intermediate layer communications client. Whether in open micro-services-based communication Feign in Hystrix, Ribbon are essential, Ribbon configuration parameters are mainly as follows:

ribbon:
  eager-load:
    enabled: true
  #说明:同一台实例的最大自动重试次数,默认为1次,不包括首次
  MaxAutoRetries: 1
  #说明:要重试的下一个实例的最大数量,默认为1,不包括第一次被调用的实例
  MaxAutoRetriesNextServer: 1
  #说明:是否所有的操作都重试,默认为true
  OkToRetryOnAllOperations: true
  #说明:从注册中心刷新服务器列表信息的时间间隔,默认为2000毫秒,即2秒
  ServerListRefreshInterval: 2000
  #说明:使用Apache HttpClient连接超时时间,单位为毫秒
  ConnectTimeout: 3000
  #说明:使用Apache HttpClient读取的超时时间,单位为毫秒
  ReadTimeout: 3000

As shown above, in the Spring Cloud service call Feign finely divided into two layers: Hystrix calls and calls the Ribbon, Feign its configuration will be overwritten.

If turned Hystrix, so Ribbon timeout configuration Hystrix timeout configure the dependencies, because it involves a retry mechanism Ribbon, so under normal circumstances are less than Ribbon timeout timeout Hystrix, otherwise they would the following error occurred:

2019-10-12 21:56:20,208   111231 [http-nio-8084-exec-2] WARN    o.s.c.n.z.f.r.s.AbstractRibbonCommand - The Hystrix timeout of 10000ms   for the command operation is set lower than the combination of the Ribbon   read and connect timeout, 24000ms. 

Relations Ribbon and Hystrix timeout configuration

So what Ribbon and Hystrix timeout configuration specific relationship is it? as follows:

Hystrix的超时时间=Ribbon的重试次数(包含首次) * (ribbon.ReadTimeout + ribbon.ConnectTimeout)

Ribbon and retries is calculated as:

Ribbon重试次数(包含首次)= 1 + ribbon.MaxAutoRetries  +  ribbon.MaxAutoRetriesNextServer  +  (ribbon.MaxAutoRetries * ribbon.MaxAutoRetriesNextServer)

Ribbon arranged above the figure as an example, the number of retries Ribbon = 1 + (1 + 1 + 1) = 4, should be arranged so Hystrix timeout> = 4 * (3000 + 3000) = 24,000 milliseconds. In the Ribbon timeout but Hystrix no timeout situation, Ribbon will take retry mechanism; and during the retry If the time exceeds the Hystrix timeout configuration is immediately blown (fallback).

If you do not configure the number of retries Ribbon, the Ribbon by default will try again, with the first call Ribbon, the total number of retries is 2 times to the above configuration parameters, for example, Hystrix timeout configured as a 2 * 6000 = 12,000, due in many cases, you will not usually configured number of retries Ribbon, it should be noted here next! To stress the value of more than just a demonstration configured timeout, timeout configuration a little big not fit the actual online scene, you can set up according to the actual situation!

Note, if you do not enable the timeout Hystrix, Feign is a Ribbon timeout, Feign own configuration will also be covered .

Finally, tell us about the crazy maker culture circle: Crazy maker culture circle, a highly concurrent Java learning community [ blog park entrance total ]

Crazy maker culture circle, a commitment to delivering: interview + interview must-must-must-interview + base + actual principle books " Netty Zookeeper Redis combat high concurrency "

img


Crazy maker culture circles Java Sike series

  • Java (Netty) chat program [traffic] one hundred million real open source project combat

  • Netty source, principle, JAVA NIO principle
  • Java questions face clean sweep
  • Crazy maker culture circle [blog park entrance total]


Guess you like

Origin www.cnblogs.com/crazymakercircle/p/11664812.html