In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the use of Hystrix in Spring Cloud, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
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.
Netflix creates a library called Hystrix, which implements the circuit breaker mode. In a micro-service architecture, there are usually multiple service invocation layers.
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.
After the open circuit is opened, cascading failures can be avoided, and the fallback method can directly return a fixed value.
Preparatory work
On the basis of the previous project, start eureka-server with port 9090 and start eureka-client with port 8040.
Using Circuit Breaker in Ribbon
To transform the code of the rebbon-service 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
Annotate @ EnableHystrix on the project startup class to enable the circuit breaker capability:
@ EnableEurekaClient@SpringBootApplication@EnableHystrixpublic class RibbonServiceApplication {public static void main (String [] args) {SpringApplication.run (RibbonServiceApplication.class, args);} @ Bean @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();}}
Modify the HelloController class by adding @ HystrixCommand annotations to the hello method. This annotation gives the method the ability to lowpass the fuse, specifies the fallbackMethod fuse method, and executes the fuse method when the remote service is invoked:
@ RestControllerpublic class HelloController {private final RestTemplate restTemplate; @ Autowired public HelloController (RestTemplate restTemplate) {this.restTemplate = restTemplate;} @ HystrixCommand (fallbackMethod = "helloError") @ GetMapping ("/ hello") public String hello (@ RequestParam ("name") String name) {return restTemplate.getForObject ("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class) } public String helloError (String name) {return String.format ("Hello, s! Access remote service fail! ", name);}}
Start the ribbon-service project and visit http://localhost:8050/hello?name=Mars in the browser:
Hello, My name is Mars, I'm from port: 8040
At this point, we close the eureka-service project and visit http://localhost:8050/hello?name=Mars again:
Hello, Mars! Access remote service fail!
This means that when the eureka-service service is unreachable, the ribbon-service invocation interface will fail quickly and directly call the circuit breaker method instead of waiting for the response timeout, which well controls the thread blocking of the container.
Using Circuit Breaker in Feign
Feign has integrated the circuit breaker and is modified based on the feign-service project. You only need to add the specified class of fallback to the @ FeignClient annotation:
FeignClient (value = "hello-eureka-client", fallback = FeignServiceHystrix.class) public interface FeignService {@ GetMapping (value = "/ hello") String hello (@ RequestParam (value = "name") String name);}
FeignServiceHystrix needs to implement the FeignService interface and inject it into the Ioc container:
@ Componentpublic class FeignServiceHystrix implements FeignService {@ Override public String hello (String name) {return String.format ("Hello, s! Access remote service fail!", name);}}
Start the eureka-client and eureka-server projects first, and then start the feign-service project, and visit http://localhost:8080/hello?name=Mars in the browser:
Hello, My name is Mars, I'm from port: 8040
At this point, we close the eureka-service project and visit http://localhost:8080/hello?name=Mars again:
Hello, Mars! Access remote service fail! The above is the use of Hystrix in Spring Cloud. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow 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.