java version of spring cloud + spring boot + redis multi-tenant social e-commerce platform (D) circuit breaker (Hystrix)

In the micro-service architecture, according to the service to be split into a number of service can call each other (RPC) services between a service and can be used RestTemplate + Ribbon Feign and call this Spring Cloud. In order to ensure availability, single service often cluster deployment. As the network reasons or for their own reasons, the service does not guarantee 100% available, if a single service issue arise, to call this service will be thread is blocked, at this time if the influx of a large number of requests, Servlet container thread resources will be consumed completed , resulting in paralysis of service. Dependencies between services and service failures will spread, will service the entire micro system have serious consequences disastrous, this is the "avalanche" effect service failure.

To solve this problem, the industry proposed circuit breaker model.

一、断路器简介
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.

- Excerpt from the official website

Netflix Hystrix open source components, to achieve the cut-out mode, SpringCloud of the components were integrated. In the micro-service architecture, a request need to call multiple services are very common, as shown below:

HystrixGraph.png

The deeper service If a fault occurs, it will lead to cascading failures. When the call is not available for a particular service reaches a threshold (Hystric 5 seconds 20 times) circuit breaker will be opened.

HystrixFallback.png

After breaking open, can be used to avoid cascading failure, fallback method may return directly to a fixed value.

Second, prepare for work
This article is based on an article in the engineering, construction started on the first article, start the eureka-server project; project start service-hi, the port is 8762.

Third, the use of circuit breakers in the ribbon
code for the transformation of serice-ribbon project, first started adding dependent spring-cloud-starter-hystrix in pox.xml file:

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

In the startup class program ServiceRibbonApplication plus @EnableHystrix comment open Hystrix:

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

Transformation HelloService class, on hiService method plus @HystrixCommand comment. The comment created a fuse function of the method and specify the method fallbackMethod fuse, fuse method returns a string, the string is "hi," + name +, the following code ", sorry, error!":

@Service
public class HelloService {
 
    @Autowired
    RestTemplate restTemplate;
 
    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }
 
    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}

Start: service-ribbon project, when we visit http: // localhost:? 8764 / hi name = forezp, the browser displays:

hi forezp,i am from port:8762

Now closed service-hi project, when we visit http: // localhost:? 8764 / hi name = forezp, the browser will display:

hi ,forezp,orry,error!

This shows that when the service-hi project is not available, when the service-ribbon service-hi call API interface performs fast failure to return a set of strings directly, rather than waiting for a response time out, which is good control of the vessel the thread blocks. JAVASpring Cloud needs of large enterprises distributed micro cloud services built B2B2C e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/91411079