Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringCloud distributed Micro Service Cloud Architecture part 4: circuit Breaker (Hystrix) (Finchley version)

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In the micro-service architecture, services are split into services according to the business. Services and services can be called to each other (RPC). In Spring Cloud, services can be used

RestTemplate+Ribbon and Feign to call. To ensure its high availability, a single service is usually deployed in a cluster. Due to network reasons or one's own origin

Because the service does not guarantee 100% availability, if there is a problem with a single service, there will be thread blocking when calling the service, and if there are a large number of requests pouring in

The thread resources of the Servlet container will be consumed, resulting in service paralysis. Service-to-service dependencies, failures propagate, understand springcloud architecture

You can add: three, six, two, four, two, two, five, nine, three, three, six, two, four, two, two, five, three, six, two, four, two, five, six, two, four, two, five, three, six, two, four, two, five, three, six, two, four, two, two, five, three, six, two, four, two,

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

Brief introduction of circuit 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. -excerpt from the official website

Netflix has opened up the Hystrix component and implemented the circuit breaker model, which has been integrated by SpringCloud. In a micro-service architecture, a request requires

It is very common to invoke multiple services, as shown in the following figure:

If the lower-level services fail, it will lead to cascading failures. When the unavailability of calls to a particular service reaches a threshold (Hystric is 5 seconds 20 times)

The circuit breaker will be opened.

II. Preparatory work

This article is based on the project of the previous article, first start the project of the previous article, start the eureka-server project; start the service-hi project

Its port is 8762.

Third, use circuit breakers in ribbon

To transform the code of the serice-ribbon project, first of all, add the initial dependence of spring-cloud-starter-netflix-hystrix in the pox.xml file:

Org.springframework.cloud spring-cloud-starter-netflix-hystrix

Add @ EnableHystrix annotation to the startup class ServiceRibbonApplication of the program to open Hystrix:

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

Modify the HelloService class by annotating the hiService method with @ HystrixCommand. This annotation creates the function of a fuse for this method, and specifies the fallbackMethod fuse method. The fuse method directly returns a string of "hi,"+ name+", sorry,error! ", with the following code:

@ Servicepublic 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 the http://localhost:8764/hi?name=forezp, browser to display:

Hi forezp,i am from port:8762

Close the service-hi project at this time, and when we visit http://localhost:8764/hi?name=forezp again, the browser will display:

Hi, forezp,orry,error!

This means that when the service-hi project is not available, when service-ribbon calls the API interface of service-hi, it will execute a quick failure and directly return a

Group string instead of waiting for a response timeout, which controls the thread blocking of the container.

Using circuit breakers in Feign

Feign comes with a circuit breaker, and after the D version of Spring Cloud, it is not turned on by default. You need to configure it in the configuration file to open it, and add

The following code:

Feign.hystrix.enabled=true

To carry out the transformation based on the service-feign project, you only need to add the specified class of fallback to the comments of the SchedualServiceHi interface of FeignClient:

FeignClient (value = "service-hi", fallback = SchedualServiceHiHystric.class) public interface SchedualServiceHi {@ RequestMapping (value = "/ hi", method = RequestMethod.GET) String sayHiFromClientOne (@ RequestParam (value = "name") String name);}

SchedualServiceHiHystric needs to implement the SchedualServiceHi interface and inject it into the Ioc container. The code is as follows:

@ Componentpublic class SchedualServiceHiHystric implements SchedualServiceHi {@ Override public String sayHiFromClientOne (String name) {return "sorry" + name;}}

Start the four Servcie-feign project, and the browser opens http://localhost:8765/hi?name=forezp,. Note that the service-hi project is not started at this time.

The web page shows:

Sorry forezp

Open the service-hi project and visit it again. The browser displays:

Hi forezp,i am from port:8762

This proves that the circuit breaker works.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report