Play Turn Springcloud (f version) three. Circuit breakers (Hystrix) Resttemplate+ribbon and feign two different ways

Source: Internet
Author: User
This article is based on:

Play Turn Springcloud one. Registration and discovery of services (Eureka)

Play Turn Springcloud two. Service consumer (1) ribbon+resttemplate

Turn Springcloud two. Service consumer (2) feign

Three circuit breakers (hystrix)

In a microservices architecture, the service is split into services according to the business, and services can be called with each other ( RPC), which can be used in spring cloud to invoke. To ensure their high availability, a single service is typically deployed in a cluster. Due to network reasons or their own reasons, the service does not guarantee that 100% is available, if a single service problem, call the service will be a thread blocking, if there is a large number of requests, the servlet container thread resources will be exhausted, resulting in service paralysis. The dependence between service and service, the failure will spread , will have disastrous serious consequence to the whole micro-service system, this is the "avalanche" effect of service failure.

In order to solve this problem, the industry put forward the circuit breaker model.

One Circuit Breaker Introduction

Netflix opens up the Hystrix component, implements the circuit breaker mode, and springcloud the component. In a microservices architecture, it is very common for a request to invoke multiple services, such as:

Lower -level services can cause cascading failures if they fail. When a call to a particular service is not available to reach a threshold (Hystric is 5 seconds 20 times) The circuit breaker will be opened.

The fallback method can return a fixed value directly after the open circuit is opened to avoid cascading failures .

Two preparatory work

start the Demo1 project , start the Demo2 project, and its port is 8763.

Three using a circuit breaker in the ribbon

based on Topsy Springcloud two. Service consumer (1) ribbon+resttemplate

Project structure:

retrofit demo3  pox.xml file:

 <  dependency  >  <  groupid  >  org.springframework.cloud</  GroupId  >  <  artifactid  >  spring-cloud-starter-netflix-hystrix</ artifactid  >  </ dependency  >  

in the startup class of the program Serviceribbonapplication plus @enablehystrix annotations Open Hystrix:

@EnableEurekaClient @enablediscoveryclient@springbootapplication@EnableHystrix   Public class demo3application {   publicstaticvoid  main (string[] args) {      Springapplication.run (demo3application. class , args);   }   @Bean   @LoadBalanced   resttemplate resttemplate () {      returnnew  Resttemplate ();   }}

Annotation parsing:

@EnableHystrix

1. turn on the fuse mechanism

Transform the HelloService class and add @hystrixcommand annotations to the Hiservice method. The note creates a fuse function for the method and specifies the Fallbackmethod fuse method, which directly returns a string with the string "Hi," "+name+", sorry,error! "

@Service  Public class HelloService {    @Autowired    resttemplate resttemplate;     = "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!" ;    }}

Annotation parsing:

@HystrixCommand

1. specify the method to execute after the break, through which the service is invoked to implement the fallback method

2. implement the Hystrix component in Spring Cloud:

Start:Service-ribbon Project, when we visit HTTP://LOCALHOST:8764/HI?NAME=FSDM, the browser displays:

at this point the Service-hi project is closed, and when we revisit HTTP://LOCALHOST:8764/HI?NAME=FSDM, the browser will show:

This means that when the Demo2 project is unavailable,Demo3 calls the API interface of Demo2, performs a quick failure, returns a set of strings directly instead of waiting for a response timeout, which Very good control of the container's thread blocking .

Four using circuit breakers in feign

Based on Topsy Springcloud two. Service consumer (2) feign

Project structure:

Reconstruction based on Demo4 engineering

The feign comes with a circuit breaker , which is not opened by default after the D version of Spring Cloud. You need to configure it in the configuration file to open it and join in the last line of yml :

Feign.hystrix.enabled:true

based on the Demo4 project, simply Add the fallback specified class to the annotations of the Feignclient Schedualservicehi interface:

@FeignClient (value = "Service-hi", fallback = schedualservicehihystric.  Class)publicinterface  Schedualservicehi {    = "/hi", method =  Requestmethod.get)    = "name") String name);

The schedualservicehihystric needs to implement the Schedualservicehi interface and inject it into the IOC container,

@Component  Public class Implements Schedualservicehi {    @Override    public  string Sayhifromclientone (string name) {         return "Sorry" +name;}    }

Annotation parsing:

@Component

1. Instantiate the normal pojo into the spring container

2. Refer to various components, that is, when our class is not in the various categories (not @Controller, @Services, etc.), we can use @component to label this class.

Errors that may occur:

That's because

you must write the. Class of the implementation class here .

start four Demo4 project, browser open HTTP://LOCALHOST:8765/HI?NAME=FSDM, notice at this time Demo2 project has Start, Web page display:

Close Demo2 project, re-visit HTTP://LOCALHOST:8765/HI?NAME=FSDM

This proves that the circuit breaker is playing a role.

                      Not finished, to be continued ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.