In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1. Concept
Fuses, fault-tolerant management tools, are designed to control the nodes of services and third-party libraries through fuse mechanisms, thus providing stronger fault tolerance for delays and failures. In addition to the function of circuit breaker, there are powerful functions such as service degradation, thread and signal isolation, request caching, request merging and service monitoring.
2. Integration
A. Introduce spring-cloud-starter-hystrix dependency into the dependency node of the pom.xml of the project:
Org.springframework.cloud
Spring-cloud-starter-hystrix
B. start the fuse by annotating @ EnableCircuitBreaker in the startup class
/ * *
* turn on the circuit breaker function using the @ EnableCircuitBreaker annotation
* @ author eacdy
, /
@ SpringBootApplication
@ EnableDiscoveryClient
@ EnableCircuitBreaker
Public class HystrixApplication {
/ * *
* instantiate RestTemplate and enable load balancing capacity through @ LoadBalanced annotation.
* @ return restTemplate
, /
@ Bean
@ LoadBalanced
Public RestTemplate restTemplate () {
Return new RestTemplate ()
}
Public static void main (String [] args) {
SpringApplication.run (HystrixApplication.class, args)
}
}
C. Write a service class, write a fallback method, and specify the fallback method if an exception occurs through the command mode.
@ Service
Public class HystrixService {
@ Autowired
Private RestTemplate restTemplate
Private static final Logger LOGGER = LoggerFactory.getLogger (RibbonHystrixService.class)
@ HystrixCommand (fallbackMethod = "fallback")
Public User findById (Long id) {
Return this.restTemplate.getForObject ("http://microservice-provider-user/" + id, User.class)
}
Public User fallback (Long id) {
User user = new User ()
User.setId (- 1L)
User.setUsername ("default username")
User.setAge (0)
Return user
}
}
D. Write the controller class and invoke the service class
@ RestController
Public class HystrixController {
@ Autowired
Private HystrixService hystrixService
@ GetMapping ("/ ribbon/ {id}")
Public User findById (@ PathVariable Long id) {
Return this.hystrixService.findById (id)
}
}
E. Write application.yml
Server:
Port: 9413
Spring:
Application:
Name: microservice-hystrix
Eureka:
Client:
ServiceUrl:
DefaultZone: http://discovery:8761/eureka/
Instance:
Hostname: hystrix
3. Start the application and launch two identical applications at the same time, only with different ports. If you access http://hystrix:9413/hystrix/1, the content will be displayed normally. When the service is set to time out, the fallback method will be called.
4. Summary
The fuse effectively solves the service failure and request backlog, which leads to the service crash and improves the user experience at the same time. The above integration uses the command mode of fuses. If feign is used in the project, it comes with a fallback mechanism. However, it is recommended to use the command mode. The parameter setting is flexible, and you can set the number of concurrency, thread execution timeout, maximum number of concurrent execution threads, thread survival time, and whether thread execution timeout is interrupted. Api is more powerful.
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.