当前位置:网站首页>Implementation of service fusing

Implementation of service fusing

2022-04-23 22:00:00 Leon_ Jinhai_ Sun

# 0. How to realize service fusing 
-  introduce hystrix rely on , And turn on the fuse ( Circuit breaker )
-  Simulation degradation method 
-  Call test 
# 1. Introduced in the project hystrix rely on  
<!-- introduce hystrix-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
# 2. Open the circuit breaker 
@SpringBootApplication
@EnableCircuitBreaker  // Used to open the circuit breaker 
public class Products9998Application {
    public static void main(String[] args) {
        SpringApplication.run(Products9998Application.class, args);
    }
}
# 3. Use HystrixCommand Annotation implementation break 
// Service failure 
@GetMapping("/product/break")
@HystrixCommand(fallbackMethod = "testBreakFall" )
public String testBreak(int id){
  log.info(" Goods received id by : "+ id);
  if(id<=0){
    throw new RuntimeException(" Illegal data !!!");
  }
  return " Currently receiving goods id: "+id;
}

public String testBreakFall(int id){
  return " The current data is illegal : "+id;
}

# 4. Access test 
-  Normal parameter access 
-  Error parameter access 

# 5. summary
- From the above demonstration process, we can find that if a certain condition is triggered, the circuit breaker will automatically open , After a while, it'll shut down again after it's normal . So what is the opening condition of the circuit breaker ?

# 6. Circuit breaker opening conditions
- Official website : https://cloud.spring.io/spring-cloud-netflix/2.2.x/reference/html/#circuit-breaker-spring-cloud-circuit-breaker-with-hystrix

A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service exceed circuitBreaker.requestVolumeThreshold (default: 20 requests) and the failure percentage is greater than circuitBreaker.errorThresholdPercentage (default: >50%) in a rolling window defined by metrics.rollingStats.timeInMilliseconds (default: 10 seconds), the circuit opens and the call is not made. In cases of error and an open circuit, a fallback can be provided by the developer. -- From the official

# After the translation of the original text , Summarize the conditions for opening and closing :
- 1、   When a certain threshold is met ( Default 10 More than in seconds 20 Number of requests )
- 2、   When the failure rate reaches a certain level ( Default 10 More than in seconds 50% Request failed for )
- 3、   Reaching the above threshold , The circuit breaker will open
- 4、   When it's turned on , All requests are not forwarded
- 5、   After a while ( The default is 5 second ), At this time, the circuit breaker is half open , One of the requests will be forwarded . If it works , The circuit breaker will close , If it fails , Keep opening . repeat 4 and 5.

# Interview key questions : Circuit breaker process

# 7. The default service FallBack processing method
- If you develop a downgrade for each service method , For us , There may be a lot of code redundancy , Not conducive to maintenance , At this time, you need to add the default service degradation processing method

@GetMapping("/product/hystrix")
@HystrixCommand(fallbackMethod = "testHystrixFallBack") // adopt HystrixCommand Degraded processing   Specify the method of error 
public String testHystrix(String name) {
  log.info(" The receiving name is : " + name);
  int n = 1/0;
  return " service [" + port + "] Response successful , The current receiving name is :" + name;
}
// Service degradation processing 
public String testHystrixFallBack(String name) {
  return port + " The current service has been degraded !!!, The receiving name is : "+name;
}

版权声明
本文为[Leon_ Jinhai_ Sun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232157392813.html