Introduction?

  1. What is the Netflix Hystrix circuit breaker?

Netflix Hystrix is a tool/framework for providing service isolation, circuit breaker, degrade mechanisms in an SOA/ microservices architecture. Netflix Hystrix is an implementation of circuit breakers for availability of high micro service architectures and a weapon against an avalanche of services.

  1. Why circuit breakers are needed

In a distributed architecture, an application relies on multiple service is very common, if one of the dependence due to excessive delay happening, call this depend on the service thread will be blocked, if related business QPS is higher, it may produce a large number of block, which can lead to the application/service down due to server resources are exhausted.

In addition, faults can be passed between applications, which can lead to an avalanche of services if there are many upstream dependencies of failed services. In the same way that a data crash can cause an application that relies on that database to crash.

When an application relies on multiple external services and everything is fine, this is the case:


If one of the dependencies is delayed, the current request is blocked


After this happens, subsequent requests will continue to be blocked if there is no response


Each request consumes CPU, memory, and network resources. If the QPS of an application is high, all service resources of the application are quickly consumed until the application dies. If the offending Dependency I extends beyond one application, or if there are more dependencies at the top of the affected application, then the service avalanche effect we mentioned earlier can occur. Therefore, in order to deal with these problems, you need tools to support service isolation, circuit breakers, and so on.

Hystrix profile

  1. What are the capabilities/benefits of Hystrix?

    When a high delay or failure occurs through network dependent services, the protection and control for the system can help the system to quickly fail, shorten the delay waiting time, and quickly recover: When abnormal dependencies return to normal, threads occupied by failed requests will be quickly cleaned up without additional waiting to provide Fallback and relatively elegant service degradation mechanism to provide effective means of service fault tolerance monitoring, alarm and operation and maintenance control

  2. How does Hystrix solve cascading failures/prevent service avalanches?

  • Hystrix encapsulates the logic of the request, which is executed in a separate thread

  • Hystrix has an automatic timeout policy. If an external request exceeds a threshold, Hystrix will process it as a timeout

  • Hystrix maintains a thread pool for each dependency, and when threads are full, no thread queuing occurs and the operation is terminated

  • Hystrix has circuit breakers: manually or automatically disconnects services for a period of time when the dependent service failure rate exceeds a threshold

How Hystrix works

  1. Hystrix workflow


  1. Create HystrixCommand or HystrixObservableCommand objects
  2. Execute commands execute(), queue(), observe(), toObservable()
  3. If request result caching is enabled and the cache hits, the cached response is immediately returned as an Observable object
  4. Check the fuse status to determine if the requested line is open. If the requested line is open, Hystrix will not execute this command, but will execute getFallback directly
  5. If the thread pool and request queue are associated with the current command to execute, Hystrix will not execute this command and will execute getFallback directly
  6. Perform HystrixCommand. The run () or HystrixObservableCommand. The construct (), if the two method performs timeout or failure, is performed getFallback ()
  7. Hystrix reports successful, failed, rejected or timed out requests to the fuse, which maintains a number of counters for statistical purposes. The statistics generated by these counters enable the fuse to short-circuit subsequent requests for a dependent service at a specific time until the end of the recovery period, when the fuse determines that the line is still not healthy based on the statistics, the fuse will shut down the line again.
  8. Dependency isolation Hystrix uses bulkhead isolation mode to isolate dependencies between each other and limit concurrent access to either one.

One might wonder, why not rely on HTTP clients for fault tolerance (quick failures, fuses, etc.) instead of doing Hystrix with thread-pool isolation outside of access dependencies

Mainly in the following aspects:

  • Different dependencies execute differently and need to be treated separately
  • Different dependencies may require different Client tools/protocols to access, such as HTTP Client or Thrift Client.
  • Non-network exceptions may occur during Client execution, which should be isolated
  • The change of clients causes the change of circuit breakers

SpringCloud: Ribbon + Hystrix application

  1. Hystrix was introduced into the project

Modify pom.xml to introduce Spring Cloud Netflix Hystrix

  <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

  </dependency>

Copy the code
  1. Configure the Hystrix startup class

Modify the startup class application. Java to add the @enablehystrix annotation to EnableHystrix

@EnableHystrix

@EnableDiscoveryClient

@SpringBootApplication

public class Application {



    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }



    @Bean

    @LoadBalanced

    public RestTemplate restTemplate() {

        return new RestTemplate();

    }

}

Copy the code
  1. Circuit breaker processing implementation

Modified testService. Java to add circuit breaker function

  • Add the @hystrixCommand annotation to the index method and specify the method to execute after the break with the fallbackMethod parameter
  • Defines the disconnection handling method to return the prompt after service/operation disconnection
@Service

public class TestService {



    @Autowired

    private RestTemplate restTemplate;



    @HystrixCommand(fallbackMethod = "indexError")

    public Object index() {

        return restTemplate.getForObject("http://testservice", String.class);

    }



    public Object plus(int numA, int numB) {

        String url = String.format("http://testservice/plus? numA=%s&numB=%s", numA, numB);

        return restTemplate.getForObject(url, String.class);

    }



    public Object indexError() {

        return "{\"code\ : 999,\"message\ : \" Service interruption \"}";

    }

}

Copy the code
  1. Disconnect processing test

Start the Application project, visit: http://localhost:8604/ti, and you’ll see

{

  "code": 0.

  "message""hello".

  "content": null,

  "serviceName""testservice".

  "host""localhost:8602"

}

Copy the code

Close testService and visit http://localhost:8604/ti again to see

{

  "code": 999,

  "message""Service interruption"

}

Copy the code

This completes the Ribbon+Hystrix fuse.

SpringCloud: Feign + Hystrix applications

  1. Enable Hystrix Modify Application. yml, enable Hystrix
feign:

  hystrix:

    enabled: true

Copy the code
  1. Circuit breaker processing implementation

Create testServicehystrix. Java as the TestService disconnect implementation



@Component

public class TestServiceHystrix implements TestService {



    @Override

    public String indexService() {

        return "{\"code\ : 999,\"message\ : \" Service interruption \"}";

    }



    @Override

    public Result plusService(int numA, int numB) {

        Result result = new Result();

        result.setCode(999);

        result.setMessage("Service interruption");

        return new Result();

    }



    @Override

    public Result plusabService(Plus plus) {

        Result result = new Result();

        result.setCode(999);

        result.setMessage("Service interruption");

        return new Result();

    }



    @Override

    public Result plus2Service(Plus plus) {

        Result result = new Result();

        result.setCode(999);

        result.setMessage("Service interruption");

        return new Result();

    }

}

Copy the code
  1. Modify TestService to specify fallback class
@FeignClient(value = "testservice", fallback = TestServiceHystrix.class)

public interface TestService {



    @RequestMapping(value = "/", method = RequestMethod.GET)

    String indexService();



    @RequestMapping(value = "/plus", method = RequestMethod.GET)

    Result plusService(@RequestParam(name = "numA") int numA, @RequestParam(name = "numB") int numB);



    @RequestMapping(value = "/plus", method = RequestMethod.POST, consumes = "application/json")

    Result plusabService(Plus plus);



    @RequestMapping(value = "/plus2", method = RequestMethod.POST)

    Result plus2Service(@RequestBody Plus plus);



}

Copy the code
  1. Start the Feign Application project
  • Visit: http://localhost:8605/ti to see
{

  "code": 0.

  "message""hello".

  "content": null,

  "serviceName""testservice".

  "host""localhost:8602"

}

Copy the code
  • Close testService and visit http://localhost:8605/ti again to see
{

  "code": 999,

  "message""Service interruption"

}

Copy the code

The fusing of Feign+Hystrix is completed.

The end of the

Qq group: 869215239 Search: coding on the way to focus on the public number!