Use SpringCloud Hystrix for service meltdown and service downgrade

Keywords: xml Spring

Avalanche effect
(1) The system of a microservice architecture usually consists of multiple service layers, each focusing on its own business logic and providing corresponding interfaces to the outside world, and each service depends on each other through service registration and subscription.If there is a request to call Service A, but there is a problem with Service A, the request will be blocked. As long as the request to call Service A is blocked, as more and more requests are blocked, more and more computer resources are occupied.When a service has a problem, it can cause all requests to be unavailable and the entire distributed system to be unavailable, which is the avalanche effect.

(2) Causes of avalanche effect:
Hardware failure: such as server host crash, power outage in computer room, etc.
Flow surge: such as abnormal traffic, user retries causing system load to increase.
Cache refresh: Assume A is client side and B is Server side. Assume A system requests flow to B system, requests exceed B system's capacity, which will cause B system to crash.
There are bugs in the program: logic problems in code loop calls, memory leaks caused by unreleased resources, etc.

2. Service Fusion and Service Degradation
(1) Service melt down
When a large number of timeouts or failures occur for requests and calls to the target service, all calls to the service should be broken, and subsequent calls should be returned directly, freeing up resources quickly and ensuring that all calls to the target service are returned immediately and not blocked during the period when the target service is unavailable.In the SpringCloud framework, the fuse mechanism is implemented through Hystrix.Hystrix monitors the status of calls between microservices, and when a failed call reaches a certain threshold, the default is 20 calls in five seconds that fail, the fuse mechanism starts.
(2) Service downgrade
Service downgrades generally take into account overall load. When server pressure increases dramatically, some services and pages are strategically downgraded based on current business conditions and traffic.The service will no longer be invoked, and the client can prepare a local fallback callback to return a default value on its own.This frees up server resources to ensure the normal or efficient operation of the core business.

3. Implement service fault tolerance using SpringCloud Hystrix
(1) Add the following dependencies to pom.xml.

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

(2) Add the @EnableCircuitBreaker or @EnableHystrix annotation to the startup class.

package com.example.hystrixdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
public class HystrixDemoApplication {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(HystrixDemoApplication.class, args);
    }

}

(3) CompanyController controller code.

package com.example.hystrixdemo.controller;

import com.example.hystrixdemo.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CompanyController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getByIdFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
            @HystrixProperty(name = "metrics.rollingStats.thread.timeInMilliseconds", value = "5000")
            }, threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "1")
    })
    @GetMapping("/user/{id}")
    public User getById(@PathVariable Integer id){
        return restTemplate.getForObject("http://localhost:8001/"+id,User.class);
    }

    public User getByIdFallback(@PathVariable Integer id){
        User user=new User();
        user.setId(0);
        user.setName("Default Employee");
        return user;
    }
}

Posted by leagal4ever on Sun, 22 Sep 2019 19:49:57 -0700