In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use the circuit breaker Hystrix in Spring Cloud". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, we start the service registry, then start the instances of two service providers with port numbers of 8080 and 8081, respectively, and then start a service consumer with a port number of 9000. After these are started successfully, we visit the http://localhost:9000/ribbon-consumer address and see the following results:
At this point, if we close any of the service providers and visit this address, we will see the following effect:
From the previous articles, you know that the default load balancing strategy adopted in Spring Cloud is polling, so when a service provider shuts down, service request success and request failure occur in pairs: when the service consumer requests the shut-down service provider, the request fails, and when the service consumer requests the normal service provider, it can get the desired results. Instead of showing the user such an ErrorPage when the request fails, it should be a controllable page, OK. Let's take a look at how to use circuit breakers to solve this problem.
Add circuit breakers to service consumers
First, we need to introduce hystrix into the service consumer, as follows:
Org.springframework.cloud spring-cloud-starter-hystrix modifies the service consumer startup entry class
After introducing hystrix, we need to enable the circuit breaker function through @ EnableCircuitBreaker on the entry class, as follows:
@ EnableCircuitBreaker@SpringBootApplication@EnableDiscoveryClientpublic class RibbonConsumerApplication {public static void main (String [] args) {SpringApplication.run (RibbonConsumerApplication.class, args);} @ LoadBalanced @ Bean RestTemplate restTemplate () {return new RestTemplate ();}}
We can also replace these three annotations with an annotation called @ SpringBootApplication, which is defined as follows:
@ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreakerpublic @ interface SpringCloudApplication {}
It is actually an integration of these three annotations.
Modify Controller
Then we create a HelloService class, as follows:
@ Servicepublic class HelloService {@ Autowired private RestTemplate restTemplate; @ HystrixCommand (fallbackMethod = "error") public String hello () {ResponseEntity responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/hello", String.class); return responseEntity.getBody ();} public String error () {return" error ";}}
I would like to make the following points about this HelloService class:
1.RestTemplate executes the network request operation and we put it in HelloService to complete.
The 2.error method is a callback method when a request fails.
3. Use the @ HystrixCommand annotation on the hello method to specify the callback method if the request fails.
OK, finally we modify the logic of ConsumerController to look like this:
RestControllerpublic class ConsumerController {@ Autowired private HelloService helloService; @ RequestMapping (value = "/ ribbon-consumer", method = RequestMethod.GET) public String helloController () {return helloService.hello ();}}
At this point, we turn on the circuit breaker function.
test
Let's first confirm the service registry. The port numbers of two service providers are 8080 and 8081, and the port number of one service consumer is 9000. A total of four instances are started successfully. After starting successfully, we shut down one service provider. The result of accessing http://localhost:9000/ribbon-consumer, is as follows:
This is the end of the content of "how to use the circuit breaker Hystrix in Spring Cloud". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.