(F) spring cloud micro Services Architecture b2b2c e-commerce platform - 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.

I. Introduction breaker

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.

. ---- taken from 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:

Hystrix-1.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.

Hystrix-2.png

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

Second, the use of circuit breakers in the ribbon

Code serice-ribbon renovation project, first started adding dependent spring-cloud-starter-netflix-hystrix in pox.xml file:

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

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

@SpringBootApplication
@EnableEurekaClient
@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!

Guess you like

Origin blog.csdn.net/vvx0206/article/details/92965133