SpringBoot project integration Hystrix

Hystrix

 

Hystrix open source Netflix is ​​a service component isolation, service isolation through reliance to avoid delays, abnormal, causing depletion of resources system unusable solutions.

 

 

1. What is the service fuse

    Service fuse is blown execution of the service call, the corresponding subsequent requests, not continue to invoke the target service, but directly returned, so you can quickly free up resources,

   Or service failure, the failure message will be returned to the client. Local sacrifice, to preserve the overall measure is called blown.

 

2, fuse meaning

 

  Essentially to protect the system, so that the system remains stable;

 

  Reducing a loss in performance;

 

  Timely response;

 

  Fuse function: exception handling, logging, the operation test fails, manual reset, concurrent, acceleration disconnection, retry failed request.

3, the three states Hystrix

  1. blown off state (Closed)

    When the service is not a failure, the fuse is located, without any restrictions on the call of the caller.

  2. blown open state (Open)

    Within a fixed time window (MAMMALIA, default is 10 seconds), the interface call error rate reaches a threshold value (MAMMALIA, default 50%), blown enters the ON state.

    After entering the blown state, subsequent calls to the service through the network interface no longer direct the implementation of local fallback method.

  3. The semi-blown state (Half-Open)

    After entering the blow open state for a period of time (default MAMMALIA, 5 seconds), it will enter the semi-fuse-blown state. The so-called half-fuse is to try to restore service call,

    Allowing limited traffic invoke the service and monitor call success rate. If the success rate is expected, then the service has been restored into blown off; if the success rate is still very low, then re-enter blown off.

Transforming relationship of three states as shown below:

 

 

 

4, SpringBoot project integration Hystrix fuse technology

Add Hystrix dependence  

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency> 

添加yml配置

# Hystrix settings
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
          thread:
            # 线程超时15秒,调用Fallback方法
            timeoutInMilliseconds: 15000
      metrics:
        rollingStats:
          timeInMilliseconds: 15000
      circuitBreaker:
        # 10秒内出现3个以上请求(已临近阀值),并且出错率在50%以上,开启断路器.断开服务,调用Fallback方法
        requestVolumeThreshold: 3
        sleepWindowInMilliseconds: 10000

添加熔断配置类

package com.lmq.configuration;

import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 熔断配置
 */
@Configuration
public class HystrixConfiguration {

    @Bean
    public HystrixCommandAspect hystrixAspect() {
        return new HystrixCommandAspect();
    }

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

至此就算是把Hystrix集成进项目了,下面看具体使用

@RestController
@RequestMapping(value = "/webservice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@DefaultProperties(defaultFallback = "timeOutError")
public class WebServiceController {

    /**
     * 该方法是对接口调用超时的处理方法
     */
    public String timeOutError() {
        return "time out";
    }

    @GetMapping(value = "test") 
   public String getDate() {
    
return "success";
  }

只需要在Controller层添加@DefaultProperties注解,defaultFallback为服务失败后默认调用的方法,这里直接返回time out,具体可根据业务需求返回指定数据实现服务降级。

 

Guess you like

Origin www.cnblogs.com/lmqblogs/p/11984057.html