SpringCloud integrated Sentinel + Feign implement the service fuse downgrade

Sentinal

1. Background

Sentinel Ali middleware team is open source, lightweight high availability for flow control components of a distributed services architecture, mainly in the flow as the starting point, the flow control, fuse downgrade, the system load in the dimension of protection to help users protect service stability. Here you may ask: Sentinel and common fuse before downgrading library Netflix Hystrix What similarities and differences do? Sentinel's official website there is a comparison and Hystrix migrate to the sentinel article excerpt here a summary table, you can compare the specific Click the link to view. 

Feature Comparison

 From the comparison table can clearly see, Sentinel than Hystrix in functionality but also powerful.

2. Functions

 Sentinel function is mainly reflected in three aspects

2.1 Flow Control

    For the system, at any time of the incoming request is often random uncontrollable, and the processing capacity of the system is limited. We need to control the flow of the processing capability of the system. 

Angle control as follows:

  • Call relations resources, such as the relationship between the call link resources, resources, and resources
  •  Performance indicators, such as QPS, thread pools, system load, etc.
  •  Effect control, such as a direct current limiting, cold start, queuing

2.2 fuse downgrade

        When a resource unstable performance call link is detected, such as request response time for a long time or abnormal increase in the proportion of the resources for this call will be limited, so that the request fail-fast, to avoid affecting other resources lead to cascading failures. Means as follows

  • Limited by the number of concurrent threads: When the number of threads to accumulate a certain number on a particular resource, the new resource request is rejected. Accumulation of thread starts continue to receive requests after the completion of the task.
  • Downgrade resources Response time: When the reliance of resources appears response time is too long, all access to the resource will be directly rejected, only to resume after until after the time window specified.

2.3 System Load Protection  

          Sentinel protection system while providing an adaptive dimension. Avalanche prevented, system protection is an important part. When a high system load time, if it continued to make requests to enter, may cause the system to crash, unable to respond. In a clustered environment, network load balancing will flow machine should be forwarded to another carrier machine up. If this time other machines also at the edge of a state when the increased traffic will lead to this machine also collapsed, leading to the entire cluster unusable.

          To address this situation, Sentinel provides protection corresponding to the load inlet flow and allow the system to achieve a balance system to ensure the system handle most requests within the sphere of competence.

3. Use

3.1 dependence

    Here I use sentinel is based gradle configuration, compatible spring clould alibaba, so add the following dependence

compile'com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel:2.1.0.RELEASE'
compile group: 'com.alibaba.csp', name: 'sentinel-transport-simple-http', version: '1.6.3'

3.2 Notes

      Sentinel provides the following configuration is used to define classes @SentinelResource annotation resource, and provides a means for automatically extended AspectJ defining resource, processing BlockException, of course, support the use of aop, Demo here aop manner, adding

@Configuration
public class SentinelAspectConfiguration {
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
}
}

 @SentinelResource Fallback exception handling and for defining resources, and provides an optional configuration item. @SentinelResource annotation contains the following properties

  • value: resource name, required items (can not be empty)
  • entryType: entry type, options EntryType.OUT / EntryType.IN (default EntryType.OUT), corresponding to the control inlet / outlet control
  • blockHandler / blockHandlerClass: blockHandler BlockException process corresponding to the function name.
  • fallback: fallback function name, options for providing fallback processing logic when throwing an exception. fallback function may be processed for all types of exceptions (which exclude exception types exceptionsToIgnore addition)
    • The return value type must be consistent with the original function return value type
    • fllback default function and requires the original method in the same class. If you want to use other types of functions, you can specify the Class object class fallbackClass for the corresponding function is necessary to pay attention to the corresponding static function, otherwise it can not be resolved.
  • defaultFallback (since 1.6.0): The default fallback function name option, commonly used generic fallback logic (i.e., a method may be used for many services). The default fallback function can be processed for all types of exceptions (exception types exceptionsToIgnore which exclude the addition). If you configure both fallback and defaultFallback, the only fallback takes effect. Function signature and consistent fallback
  • exceptionsToIgnore (since 1.6.0): used to specify which exceptions are excluded, not included in the statistical anomaly, and will not enter fallback logic, but as it will be thrown.

3.3 Example  

Service implementation class

@Service 
@ SLF4J 
public  class HelloProviderServiceImpl the implements HelloProviderService { 

  @Autowired 
  Private ConfigurableEnvironment configurableEnvironment; 

  // corresponding functions need to be in `handleException`` ExceptionUtil` class, and must function as a static 
  @Override 
  @SentinelResource (value = "Test", blockHandler = "handleException", blockHandlerClass = { 
      ExceptionUtil. class })
   public  void Test () { 
    log.info ( "the Test" ); 
  } 

  @Override 
  @SentinelResource (value= "sayHi", blockHandler = "exceptionHandler", fallback = "helloFallback")
  public String sayHi(long time) {
    if (time < 0) {
      throw new IllegalArgumentException("invalid arg");
    }
    try {
      Thread.sleep(time);
    } catch (InterruptedException e) {
      throw new IllegalArgumentException("inter arg");
    }
    return String.format("Hello time %d", time);
  @SentinelResource (value
  @Override  // Here commonly known resources buried point, when the current limit is set according to this policy will be buried point to control
  }

= "HelloAnother", defaultFallback = "defaultFallback" , exceptionsToIgnore = {IllegalStateException. Class }) public String helloAnother (String name) { IF (name == null || "Bad" .equals (name)) { the throw new new an IllegalArgumentException ( " oops " ); } IF (" foo " .equals (name)) { the throw new new IllegalStateException (" oops " ); } return " the Hello, "+ name; } // Fallback function, consistent with the original signature of the function or adding a function Throwable types of parameters. publicHelloFallback String ( Long S, the Throwable EX) { log.error ( "fallbackHandler:" + S); return "the Oops fallbackHandler, AT error occurred" + S; } // default fallback function name public String defaultFallback () { log. info ( "Go to default fallback" ); return "default_fallback" ; } // Block exception handlers, last one more parameter BlockException, consistent with the rest of the original function. public String from exceptionHandler ( Long S, BlockException EX) { // the Do some here Wallpaper log. return "Oops,exceptionHandler, error occurred at " + s; } }

Service Interface

public interface HelloProviderService {
    public String sayHi(long t) throws InterruptedException;
    String helloAnother(String name);
    void test();
}
ExceptionUtil class
@Slf4j
public final class ExceptionUtil {
  public static void handleException(BlockException ex) {
     log.info("Oops: " + ex.getClass().getCanonicalName());
  }
}

controller class

@RestController
@Slf4j
public class HelloProviderController {

  @Autowired
  HelloProviderServiceImpl helloServiceProviderService;

  @GetMapping("/sayHi")
  public String sayHi(@RequestParam(required = false) Long time) throws Exception {
    if (time == null) {
      time = 300L;
    }
    helloServiceProviderService.test();
    
return helloServiceProviderService.sayHi(time); } @GetMapping("baz/{name}") public String apiBaz(@PathVariable("name") String name) { return helloServiceProviderService.helloAnother(name); } }

3.4 Sentinel console

A lightweight open source console that provides discovery and machine health management, monitoring (single and cluster), and push rules management capabilities. The main limiting can be dynamically configured to push Buried server resources provided by the console, so the flexibility to set policies limiting died without writing code

  • Provides a web interface, visual resources, and traffic monitoring, configuring resource Buried
  • DETAILED installation is relatively simple, it is not mentioned herein, can refer to the link 

3.5  Downgrade Policy

  • The average response time (DEGRADE_GRADE_RT): When the 1s continuously into the five requests, the average response time (in seconds) corresponding to the time average exceeds a threshold value (COUNT, in ms), then the time window took the timeWindow (DegradeRule in , in s) within the call to this method will automatically fuse (throw DegradeException). Note Sentinel default statistics RT upper limit is 4900 ms, exceeding this threshold will be counted as 4900 ms, if you need to change this limit can be configured by the startup configuration items -Dcsp.sentinel.statistic.max.rt = xxx.

  • The proportion of abnormal (DEGRADE_GRADE_EXCEPTION_RATIO): When the amount of the second resource request> = 5, and after accounting for the total number exceeds the second abnormality threshold (count DegradeRule), the resource enters degraded state by the ratio of the amount, i.e. the time window took ( the DegradeRule timeWindow, the in s), the invocation of this method automatically returned. Abnormal ratio threshold range [0.0, 1.0], representing 0% --100%.

  • Abnormal number (DEGRADE_GRADE_EXCEPTION_COUNT): After almost a minute of abnormal number of resources exceeds the threshold value will be blown. Note that due to the statistical time window is a minute level, if timeWindow less than 60s, after the end of the blown fuse state can still re-entered the state.

  • Sentinel console can be enabled, configured fuse downgrade rule directly on the console.

    • Open the console interface, click the cluster point link, select the program in the resources buried point, click Downgrade
    • Configuration downgrade rules
    • RT configuration mode test, the console window and enter time RT
      • url: ip:? port / sayHi time = delayTime, within 1s when entering sustained an average of five requests delayTime> RT enter downgrade
    • Abnormal allocation ratio, the proportion of abnormal console input
      • url: ip: port / baz / bad, when the amount of the second resource request> = 5, and the ratio of the total number of second abnormality abnormality accounts set by the ratio of the amount of degraded service will enter in the next window time setting

Feign

  1. Background 

    Feign is open source Netflix, lightweight Load Balancing HTTP client, using Feign API calls as local calls the same method, while avoiding the tedious micro-call service goals, the need to constantly parsing / json data package . Spring Cloud Feign introduced and integrated Ribbon implement client load balancing call. Popular thing about: you can call a remote method invocation services like local method the same. 
Of course, there are many pit tread.

   2. Use

 Sentinel adaptation of Fegin components. If you want to use, in addition to the introduction of  spring-cloud-starter-alibaba-sentinel outside-dependent required two steps:

  • Profile Open Sentinel Feign support of:feign.sentinel.enabled=true

  • Join  openfeign starter rely on to make  sentinel starter the automated configuration class effect:
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.1.3.RELEASE'

2.1 Example

    Add EchoService interface class, the interface corresponding to the interface service01 service bound by @FeignClient (name = "service-provider") annotations

@FeignClient(name = "nacos-provider-sentianel1", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {

  @GetMapping(value = "/sayHi")
  String sayHi(@RequestParam(value = "time", required = false) Long time);

  @RequestMapping("/api/{name}")
  String apiBaz(@PathVariable("name") String name);
}

    @FeignClient in which the value of the name of the party to provide the name of a service, the service needs to configure the current interface call interface nacos-provider-sentianel1 services provided. nacos-provider-sentianel1 registered with the registration service, I used here is Nacos.

   Service configuration is as follows

  nacos-provider-sentianel1 The controller is like this, where you can see and EchoService in the method signature is consistent

@RestController
public class HelloProviderController2 {

  @GetMapping("/echo")
  public String helloConsumer(@RequestParam(required = false) Long time) {
    return "echo";
  }

  @GetMapping("/api/{name}") public String apiBaz(@PathVariable("name") String name) { return "another provider " + name; } }

Add EchoServiceFallback, here is fegin of Fallback机制,主要用来做容错处理。因为在网络请求时,可能会出现异常请求,如果还想再异常情况下使系统可用,那么就需要容错处理。

@Component。
public class EchoServiceFallback implements EchoService {

  @Override
  public String sayHi(Long time) {
    return "sayHi fallback";
  }

  @Override
  public String apiBaz(String name) {
    return "apiBaz fallback";
  }
}

Add FeignConfiguration 

@Configuration
public  class FeignConfiguration { 
  @Bean 
  public EchoServiceFallback echoServiceFallback () {
     return  new EchoServiceFallback (); 
  } 
}

  Add EchoService call on the basis of the above-HelloProviderServiceImpl

@Service 
@ SLF4J 
public  class HelloProviderServiceImpl the implements HelloProviderService { 

  @Autowired 
  Private ConfigurableEnvironment configurableEnvironment; 

  @Autowired 
  the EchoService the echoService; 

  // corresponding functions need to be in `handleException`` ExceptionUtil` class, and must function as a static 
  @Override 
  @SentinelResource (value = " Test ", blockHandler =" handleException ", blockHandlerClass = { 
      . ExceptionUtil class })
   public  void Test () { 
    log.info ( " the Test " ); 
  } 

  @Override 
  @SentinelResource (value= "sayHi", blockHandler = "exceptionHandler", fallback = "helloFallback")
  public String sayHi(long time) {
    if (time < 0) {
      throw new IllegalArgumentException("invalid arg");
    }
    try {
      Thread.sleep(time);
    } catch (InterruptedException e) {
      throw new IllegalArgumentException("inter arg");
    }
    return String.format("Hello time %d", time);
  }

  @Override
  @SentinelResource(value = "HelloAnother", defaultFallback = "defaultFallback" , 
      exceptionsToIgnore = {IllegalStateException. Class })
   public String helloAnother (String name) {
     IF (name == null || "Bad" .equals (name)) {
       the throw  new new an IllegalArgumentException ( " oops " ); 
    } 
    IF (" foo " .equals (name)) {
       the throw  new new IllegalStateException (" oops " ); 
    } 
    return " the Hello, "+ name; 
  } 

  // Fallback function, consistent with the original signature of the function or adding a function Throwable types of parameters.
  publicHelloFallback String ( Long S, the Throwable EX) { 
    log.error ( "fallbackHandler:" + S); 

    return "the Oops fallbackHandler, AT error occurred" + S; 
  } 

  // default fallback function name 
  public String defaultFallback () { 
    log. info ( "Go to default fallback" );
     return echoService.apiBaz ( "Bad" );
     // return "default_fallback"; 
  } 

  // . Block exception handler, the parameters last more than a BlockException, remaining consistent with the original function of 
  public String exceptionHandler ( Long S, BlockException EX) {
     // the Do some log here Wallpaper.
    return "Oops,exceptionHandler, error occurred at " + s;
  }
}

  Here we use echoService.apiBaz ( "bad") in defaultFallback to call the apiBaz method of nacos-provider-sentianel1

  Configuring downgrade rules helloAnother in sentinel console, after the downgrade is triggered, it will call apiBaz method acos-provider-sentianel1 services, returns the result.

to sum up

      Use sentinel system flow control, when the system stream exceed acceptable range when the current service can be invoked by Feign downgrade, such could constitute one of the most basic of demotion fuse module, of course, also integrated FeignRibbon,可以通过配置实现客户端负载均衡调用。

Guess you like

Origin www.cnblogs.com/NathanYang/p/11819881.html
Recommended