Breaker hystrix use of springcloud

  Previous article we mentioned recently opened a new project to the new business into new projects, old projects maintained separately, then gradually migrate old projects to the new project. At the front end of the time the production environment has undergone a accident, the accident began exception is our business found the front page h5 handle very slow, this server ran three provinces to conduct business, colleagues looking for an old problem when almost spit blood out, all the projects hit a log file, which is the degree of chaos can not describe, because of too busy, and this thing is a network security nothing bad title, but the most irritating is the week Friday night after work, more and more business reaction, we did not bring the equipment, can only guess at the group analyzed a variety of reasons, we came to the company new tactics on Saturday forced the issue, in recent days saw logs found items in a statistical interface is called frequently cause our cpu with memory running full, put aside the question sql program efficiency and the efficiency was found to be the same ip keep asking us one day to brush more than 80,000 requests, this is not continue Tucao. Program did not make any fault tolerant measures, will not speak of limiting it to cases interface call timeout, we can use what we are saying hystrix. His code hosted on github:

  From : https://github.com/Netflix/Hystrix/wiki

  Simply put, he can help us complete the process of program fuse downgrade in case of an error or delay, suppose we have the old project of such a mechanism, we run the case with cpu memory full, I can fuse, the main logic downgrade, by our auxiliary logic to quickly return to the front, so that the end user experience c sense is not so bad, we will not be so awkward to find the problem. springcloud has helped us integrate well, we just need to add a small amount of the notes with the configuration can be used.

  First, the use

  Finally, on a piece of paper we feign to build a good project, add pom configuration file:

  

  Reconstruction written before controller:

  

  We request to send 20, 15 and the like before transmission to complete the rest of us 6 seconds, the first 15 we show the request and opens the fuse degradation process, where the server 5 when the normal presentation of the fuse is closed:

  Continue to transform service, feign integrated hystrix, hystrix is ​​to use logic to complete the downgrade by the method, by way of using feign call interface, so the need to use

@FeignClient notes add parameters: 
  
  fallback class marked downgrade method to create downgraded class
  
  configuration file to add configuration
  feign integrated hystrix In earlier versions, spring cloud is enabled by default, if you use feign enabled by default on the circuit breaker, so the later version breaker feature is off by default,
Here are several important parameters: 
  
1.hystrix.command.default.circuitBreaker.requestVolumeThreshold: setting request to the offline snapshot time window, default is 20, a snapshot in
time of receiving the request does not reach the window that even if the number of failed fuse is not triggered
  2.hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds: it is used to set the time of half-open state circuit breaker (circuit breaker in the half-open state
allowing transmitting a request, if the request is successful breaker oFF , disconnection failure continues waiting for the next half-open state) by default to 5000.
  3.
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: to set the request time
  4.
hystrix.command.default.circuitBreaker.errorThresholdPercentage: error rate is used to set the default value of 50, within a time window exceeds a snapshot
request offline, failure rate exceeds 50%, the circuit breaker to open the
  write global default configured as above configuration, a priority from low to high They are: with the global default values, configured global attributes, default values instance, Example configuration properties, citing a quarantine policy configuration

  
instance attribute configuration requires you to change the default on it, in addition to a lot of configuration and share a link: https://www.cnblogs.com/520playboy/p/ 8074347.html , the Spring micro Cloud services have combat this bookMore detailed configuration in detail : For all requests retry operations : handover retry instance : the current instance number of retries  
   ribbon.OkToRetryOnAllOperations
  ribbon.MaxAutoRetriesNextServer
  ribbon.MaxAutoRetries
  In addition to the above configuration we need to add a configuration, the aim is to close feign retry mechanism, because the interface did not want to trigger a timeout retry mechanism. feign ribbon is accomplished by the integration of load balancing, the default retry the test five times, can be seen under feign package Retryer class, which 
the default method. Continue, we
modify the method of service providers on the article:

  Code Completion: Let's access interface: HTTP: // localhost: 8886 / the Add
  client console:
  
  The server console: 
  
  You can see the information by printing out across a total of 20 times before the call, after waiting 15 calls 6 seconds continue to call five times, 15 times the server receives a request from the reference can be seen on the 11th to 15 no request to the server, this process will demonstrate the whole process fuse, we pass the configuration file
setting request a lower limit of 10, but 10 times before the server requests will call a timeout, so 10 request will trigger the downgrade before the client method, the final blow, making our requests quickly return, 16 to 20 times when the rest because the thread 6 seconds, then half-open circuit breaker time already reached, the
order to 16th will release request when hystrix a request is found not timed out, turn off the fuse, the anti-16-20 on subsequent requests will normally result, the program logic and back to the primary.
  Presentation about ribbon use hystrix, using the project created on an article, add pom file, create a service logic into the service in:
  
  
  在需要熔断的方法添加
@HystrixCommand注解,标注降级方法,修改controller方法,访问:http://localhost:8884/add
  
  二、工作流程
  通过上面的例子我们简单分析一下hystrix流程,我们先引用一张HystrixCircuitBreaker官方的流程图:
  

   第一步:HystrixCommand调用allowRequest方法判断请求是否允许通过,如果熔断器强制打开不允许放行,如果熔断器强制关闭允许放行,circuitBreaker.forceOpen配置优先级比circuitBreaker.forceClosed优先级高

  第二步:调用isopen方法判断熔断器是否打开,如果打开则判断circuitBreaker.sleepWindowInMilliseconds进入半开模式的时间,如果距离上一次打开时间超过设置时间,将进入半开模式,放行一个请求,走第一步

  第三步:熔断器未打开,判断一个请求窗内的请求下线circuitBreaker.requestVolumeThreshold,小于放行,否则判断一个时间窗内的错误率小于circuitBreaker.errorThresholdPercentage,则放行,大于则打开熔断器,走第二部

  三、浅谈源码
  我们从feign包下找到HystrixFeign类,里面有几个方法,第一个创建HystrixInvocationHandler:

  第二个方法初始化HystrixCommand的setterFactory工厂

  

  HystrixInvocationHandler类里调用invoke(太长就不截图了)方法重写了降级方法,通过代理模式将hystrix包装feign。通过toSetters方法给方法设置配置项

  setterFactory里通过create方法生成配置项

  

  groupKey是接口名称,还有一个commandKey,继续往下:

  

  通过接口名跟方法名属性名拼接生成commandKey,文章上面写到几个属性的配置以hystrix.command.default.circuitBreaker.requestVolumeThreshold为例,我们这样配置是设置了全局默认属性配置,通过上面的分析我们可以改成这样配置hystrix.command.AddService.circuitBreaker.requestVolumeThreshold,这就是上面提到的事例默认值配置,对应我们刚刚写的类:

  

  我们再来看看hystrix的配置类HystrixCommandProperties,它包含了大量配:

  通过片段能看出来传入一个前缀,然后拼接key加上后面的字符串然后为属性赋值,这个key就是我们前面讲到的configKey,没有配置采用默认key。

Guess you like

Origin www.cnblogs.com/HuuuWnnn/p/11414645.html