In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the use of fuse Hystrix in Spring Cloud", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "what is the use of fuse Hystrix in Spring Cloud".
I. 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.
. --From official website
Netflix open-sourced the Hystrix component to implement the breaker pattern, and SpringCloud integrated this component. In microservices architecture, it is very common for a request to invoke multiple services, as shown in the following figure:
Failure of lower level services can lead to cascading failures. When the unavailability of calls to a particular service reaches a threshold (Hystral is 5 seconds 20 times) the circuit breaker will be opened.
After the open circuit is opened, it can be used to avoid cascading failures, and the fallback method can directly return a fixed value.
II. Preparations
This article is based on the project of the previous article. Start the project of the previous article first, start the eureka-server project; start the service-hi project, which has port 8762.
Third, the use of circuit breakers in ribbon
To modify the code of the serice-ribbon project, first add the starting dependency of spring-cloud-starter-hystrix to the box.xml file:
org.springframework.cloud spring-cloud-starter-hystrix
Add @EnableHystrix annotation to the startup class ServiceRibbonApplication of the program to open Hystrix:
@SpringBootApplication@EnableDiscoveryClient@EnableHystrixpublic class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); }}
Modify HelloService class by adding @HystrixCommand annotation to hiService method. This annotation creates the fuse function for this method and specifies the fallbackMethod fuse method, which directly returns a string of "hi,"+name+",sorry,error! The code is as follows:
@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 http://localhost:8764/hi? name=forezp, browser displays:
hi forezp,i am from port:8762
At this point close service-hi project, when we visit http://localhost:8764/hi? name=forezp, the browser will display:
hi ,forezp,orry,error!
This means that when the service-hi project is unavailable, the service-ribbon calls the API interface of service-hi and performs a quick failure, directly returning a set of strings instead of waiting for a response timeout, which is a good control of the container thread blocking.
IV. Circuit breaker used in Feign
Feign comes with a circuit breaker, and in version D 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 to the configuration file:
feign.hystrix.enabled=true
Based on the service-feedback project, you only need to add the specified class of fallback to the annotation of FeigClient's Scheduler ServiceHi interface:
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name);}
Scheduled ServiceHiHystic needs to implement the Scheduled ServiceHi 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 servcie-feign project and open the browser http://localhost:8765/hi? name=forezp, note that the service-hi project is not started at this time, and the webpage shows:
sorry forezp
Open service-hi project, visit again, browser displays:
hi forezp,i am from port:8762
This proves that the circuit breaker worked.
V. Hystrix Dashboard
Based on service-ribbon remodeling, Feign's remodeling is the same as this.
Preferred starting dependency for introducing spring-cloud-starter-hystrix-dashboard in pom.xml:
org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-hystrix-dashboard
Add the @EnableHystrixDashboard annotation to the main program startup class to open hystrixDashboard:
@SpringBootApplication@EnableDiscoveryClient@EnableHystrix@EnableHystrixDashboardpublic class ServiceRibbonApplication {
..// Code omitted}
Open the browser: visit http://localhost:8764/hystrix, the interface is as follows:
Click monitor stream to enter the next interface, visit: http://localhost:8764/hi? name=forezp
The monitoring interface will appear:
The above is "Spring Cloud fuse Hystrix what to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.