Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring cloud (7): the Application of Hystrix

Shulou Source: shulou.com Published: 2022-06-03 06:01:23 10月03日 Update

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.

Tags: Services threads fuses powerful functional commands methods patterns annotations applications at the same time failures time mechanisms capabilities nodes fault tolerance control difference maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Microsoft Huawei macOS