This is the 18th day of my participation in Gwen Challenge

Sentinel

An overview,

https://sentinelguard.io/zh-cn/docs/introduction.html

https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

Same function as the Hystrix before.

Two, environment configuration

Download and install

  • Download address:https://github.com/alibaba/Sentinel/releases
  • Activation:Java jar sentinel - dashboard - 1.7.0. Jar
  • Access:http://localhost:8080, password sentinel

Initial Monitoring

  • New cloudalibaba sentinel — service8401

  • Rely on:

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
    <! -- Durable use -->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
    Copy the code
  • Configuration:

    server:
      port: 8401
    spring:
      application:
        name: cloudalibaba-sentinel-service
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848   Nacos configuration center address
        sentinel:
          transport:
            dashboard: localhost:8080
            The default port is 8719. If the port is occupied, a +1 scan is automatically performed starting from 8719 to find the port that is not occupied
            port: 8719
    management:   Expose the monitoring address
      endpoints:
        web:
          exposure:
            include: The '*'
    Copy the code
  • controller

    @RestController
    public class FlowLimitController {
        @GetMapping("/testA")
        public String testA(a) {
            return "testA";
        }
        @GetMapping("/testB")
        public String testB(a) {
            return "testB"; }}Copy the code
  • Because sentinel is a lazy loading mechanism, the service will not be displayed immediately after starting the service, but will be monitored only after being accessed once.

Sentinel flow control

An overview of the

https://sentinelguard.io/zh-cn/docs/flow-control.html

Add flow control

Configure flow control for specific requests on the Sentinel main screen

The threshold type

  • QPS (Number of requests per Second) Traffic limiting: The current configuration limits the traffic of /testA requests only for a specified number of accesses per second. If the number of accesses exceeds the threshold, the system displays a default message indicating that the access fails.
  • Number of threads flow limiting: Multiple requests can be received, but only a specified number of requests can be received at the same time. Failure messages are displayed for other requests.

Flow control mode

  • Direct: When the API reaches the traffic limiting condition, it directly limits traffic
  • Association: When the associated resource reaches the traffic limiting condition, the resource limits itself
  • Link: Only traffic on a specified link is recorded. (Specify the amount of traffic from the incoming resource and limit the traffic when it reaches the threshold.)

Effect of flow control

  • Fast failure: Fails directly and throws an exception.
  • Warm Up:
    • This method is mainly used when the system is at a low water level for a long time. When the flow increases suddenly, the system may be overwhelmed by pulling the system to a high water level. Through the “cold start”, the flow slowly increases and gradually increases to the upper limit of the threshold within a certain period of time, giving the cold system a time to warm up and avoiding the cold system being overwhelmed.
    • Formula: The threshold is divided by coldFactor (cold loading factor, default is 3). The threshold will be reached after a warm-up time.
    • Example: The current threshold is 10 and the preheating time is 5. Due to the presence of coldFactor, the threshold is 3 at the beginning and will reach 10 after the preheating time has passed.
  • Queuing: Queuing at a uniform speed and strictly controlling the interval for passing requests. The threshold must be set to QPS