Spring Cloud Practical Combat | How to use distributed system flow control and circuit breaker degradation component Sentinel

A collection of columns that you can save for emergencies

Spring Cloud practical column:https://blog.csdn.net/superdangbo/category_9270827.html

Python practical column:https://blog.csdn.net/superdangbo/category_9271194.html

Logback detailed explanation column:https://blog.csdn.net/superdangbo/category_9271502.html

tensorflow专栏:https://blog.csdn.net/superdangbo/category_8691332.html

Redis专栏:https://blog.csdn.net/superdangbo/category_9950790.html

AI machine learning practice:

AI machine learning practice | Sentiment analysis using Python and scikit-learn library

AI machine learning | Speech recognition based on the librosa library and using classifiers in the scikit-learn library

Python practice:

Python Practical | Use Python and TensorFlow to build a convolutional neural network (CNN) for face recognition

Spring Cloud actual combat:

Spring Cloud Practical Combat | Decrypting the underlying principles of Feign, including practical source code

Spring Cloud Practical Combat | Decrypting the underlying principles of load balancing Ribbon, including practical source code

1024 Programmers Day special article:

1024 Programmers Carnival Special | ELK+ collaborative filtering algorithm builds a personalized recommendation engine to intelligently realize "thousands of people, thousands of faces"

1024 Programmer's Day Special | Deciphering Spring Cloud Hystrix Meltdown to Improve System Availability and Fault Tolerance

1024 Programmer's Day Special | ELK+ uses user portraits to build a personalized recommendation engine to intelligently realize "thousands of people, thousands of faces"

1024 Programmer's Day Special | OKR VS KPI, who is more suitable?

1024 Programmers Day Special | Spring Boot Practical MongoDB Sharding or Replica Set Operation

Spring practical series of articles:

Spring Practical | Spring AOP Core Tips - Sunflower Collection

Spring Practice | The secret that Spring IOC cannot tell?

National Day and Mid-Autumn Festival special series of articles:

National Day and Mid-Autumn Festival Special (8) How to use JPA in Spring Boot projects

National Day and Mid-Autumn Festival Special (7) 20 Common Programming Interview Questions for Java Software Engineers

National Day and Mid-Autumn Festival Special (6) 30 Common Treasure Programming Interview Questions for College Students

National Day and Mid-Autumn Festival Special (5) How to performance tune MySQL? Next article

National Day and Mid-Autumn Festival Special (4) How to performance tune MySQL? Previous article

National Day and Mid-Autumn Festival Special (3) Use Generative Adversarial Network (GAN) to generate paintings with a festive atmosphere, implemented by the deep learning frameworks TensorFlow and Keras

National Day and Mid-Autumn Festival Special (2) Romantic Blessings Using Generative Adversarial Networks (GAN) to generate paintings with a festive atmosphere

National Day and Mid-Autumn Festival Special (1) Romantic Blessing Method Use Recurrent Neural Network (RNN) or Long Short-Term Memory Network (LSTM) to generate blessing poems

1. Sentinel development history

Sentinel is a flow control, circuit breaker and degradation component for distributed systems, developed by Alibaba Group. Its development history can be traced back to 2012. It was initially used internally by Alibaba and was later gradually open sourced and developed into an independent project. Sentinel is committed to providing high-performance, scalable, and secure traffic control solutions to meet the needs of modern distributed systems.
In 2012, Sentinel was born, with its main function being inlet flow control.
From 2013 to 2017, Sentinel developed rapidly within Alibaba Group and became a basic technology module, covering all core scenarios. Sentinel has thus accumulated a large number of traffic consolidation scenarios and production practices.
In 2018, Sentinel was open sourced and continues to evolve.
In 2019, Sentinel continued to explore the direction of multi-language expansion and launched a C++ native version. At the same time, it also launched Envoy cluster flow control support for Service Mesh scenarios to solve the problem of multi-language expansion under the Service Mesh architecture. Current limiting issue.
In 2020, the Sentinel Go version was launched and continues to evolve towards cloud native.
In 2021, Sentinel is evolving towards a 2.0 cloud-native high-availability decision center component; at the same time, a native version of Sentinel Rust is launched. At the same time, we also explored scenarios such as Envoy WASM extension and eBPF extension in the Rust community.
In 2022, the Sentinel brand will be upgraded to traffic management, covering traffic routing/scheduling, traffic coloring, flow control degradation, overload protection/instance removal, etc.; at the same time, the community will extract traffic management related standards to OpenSergo In the standard, Sentinel is implemented as a traffic management standard.
The main application scenarios of Sentinel are as follows:

  1. Flow control
    In a distributed system, calls between services may generate a large number of requests, causing the system to be overloaded. Sentinel can limit requests by setting traffic control policies to ensure that the system can run stably in high-concurrency scenarios.
  2. Circuit breaker degradation
    In a distributed system, dependencies between services may cause the failure of a certain service to affect the entire system. Sentinel supports a circuit breaker mechanism. When a service fails, it can automatically cut off requests to avoid the spread of failures. At the same time, Sentinel also supports downgrade strategies to provide downgrade processing for faulty services, such as returning to default values, logging, etc.
  3. Resource Isolation
    Sentinel can isolate access to different resources, such as HTTP requests, database connections, etc. In this way, when a problem occurs with a certain resource, its impact on other resources can be limited and the stability of the system can be improved.
  4. System Monitoring
    Sentinel provides a real-time monitoring interface to view system traffic, circuit breakers, degradation and other statuses. This helps developers detect system problems in time and optimize them accordingly.
  5. Application Extensions
    Sentinel provides a wealth of extension points, such as custom traffic control policies, degradation policies, etc. This allows developers to flexibly customize and extend Sentinel's functionality based on business needs.
    In Spring Cloud projects, Sentinel can be seamlessly integrated with Spring Cloud components, such as Spring Cloud Gateway, Spring Cloud Alibaba Nacos Discovery, Spring Cloud Alibaba Sentinel, etc. This makes it easier for developers to use Sentinel in Spring Cloud projects, improving the stability, reliability, and scalability of the system.
    In short, Sentinel, as a distributed system component with a long history of development, has been recognized by the industry for its rich functions and powerful performance. Using Sentinel in Spring Cloud projects can effectively solve the problems of high concurrency and fault diffusion faced by distributed systems, and improve the stability, reliability and scalability of the system.

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

Insert image description here

2. Sentinel specific parameters

To use Sentinel in a Spring Cloud project, you mainly need to configure the following parameters:

  1. Traffic control policy
    Sentinel provides a variety of traffic control policies, such as traffic limit, burst traffic limit, request session limit, etc. The default traffic control policy and specific parameters can be set in the application.yml configuration file.
    Case:
sentinel:
  flow:
    defaultFlowId: 1
    control:
      default:
        limitType: flow
        flowConfig:
          maxFlow: 1000
          burstCapacity: 500

In this case, we set the default flow control policy to flow, the maximum flow rate is 1000, and the burst capacity is 500.
2. Downgrade strategy
Sentinel supports downgrade strategies based on HTTP status codes, exception types and custom rules. The downgrade strategy and specific parameters can be set in the application.yml configuration file.
Case:

sentinel:
  degrade:
    enabled: true
    selectors: default
    rules:
      - type: http-status
        status: 500
      - type: exception
        exception: java.lang.ArithmeticException

In this case, we set up a downgrade when the HTTP status code is 500, and a downgrade when an exception ArithmeticException occurs.
Insert image description here

  1. Resource Wrapper
    Sentinel provides resource wrappers that can intercept requests and measure resource usage. The parameters of the resource wrapper can be set in the application.yml configuration file.
    Case:
sentinel:
  resource:
    watermark:
      enabled: true
      interval: 60000
      statInterval: 1000

In this case, we set the water mark to be on, the statistics interval to 10 seconds, and the water mark interval to 60 seconds.
4. System parameters
Sentinel provides many system parameters, such as daemon mode, log level, etc. These system parameters can be set in the application.yml configuration file.
Case:

sentinel:
  defender:
    mode: standalone
  log:
    level: debug

In this case, we set the daemon mode to independent mode and the log level to debug.
5. Application parameters
Sentinel supports setting independent parameters for each application, such as traffic control policy, degradation policy, etc. Application parameters can be set in the application.yml configuration file.
Case:

application:
  name: my-application
  sentinel:
    flow:
      control:
        default:
          limitType: flow
          flowConfig:
            maxFlow: 1000
            burstCapacity: 500
  degrade:
    rules:
      - type: exception
        exception: java.lang.ArithmeticException

In this case, we set up a separate traffic control policy and degradation rules for the application named my-application.
The above is a detailed introduction on how to use Sentinel and configure it in the Spring Cloud project. In actual applications, you can make more detailed configurations according to your needs to meet different traffic control, degradation, and monitoring needs.

3. Use Sentinel in actual Spring Cloud projects

Using Sentinel in a Spring Cloud project is mainly completed through the following steps: introducing dependencies, initializing Sentinel, configuring Sentinel, using Sentinel for flow control, circuit breaker and downgrade, and monitoring Sentinel. The following will introduce it in detail and give specific examples at each step.

  1. Introduce dependencies
    Add Sentinel dependencies in the project's pom.xml file:
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.2</version>
</dependency>
  1. Initialize Sentinel
    Add @EnableSentinel annotation to the main class of the project to start the Sentinel daemon process:
@SpringBootApplication
@EnableSentinel
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }
}
  1. Configure Sentinel
    Configure Sentinel in a application.properties or application.yml file:
sentinel:
  degrade:
    enabled: true
    selectors: default
  flow:
    defaultFlowId: 1
    control:
      default:
        limitType: flow
        flowConfig:
          maxFlow: 1000
          burstCapacity: 500

The downgrade function is configured here and the default traffic control policy is set.
4. Use Sentinel for flow control
Add @Block annotation on the method that needs to control the flow:
Case:

@RestController
public class MyController {
    
    
    @GetMapping("/controlFlow")
    public String controlFlow() {
    
    
        // 业务逻辑
        return "Hello, Sentinel!";
    }
}
  1. Use Sentinel for fusing
    Add @Breakpoint annotation on the method that needs fusing:
    Case: a>
@RestController
public class MyController {
    
    
    @GetMapping("/breakpoint")
    public String breakpoint() {
    
    
        // 业务逻辑
        return "Hello, Sentinel!";
    }
}
  1. Use Sentinel for downgrade
    Add @Degrade annotation on the method that needs to be downgraded:
    Case: a>
@RestController
public class MyController {
    
    
    @Degrade
    @GetMapping("/degrade")
    public String degrade() {
    
    
        // 业务逻辑
        return "Hello, Sentinel!";
    }
}
  1. Monitor Sentinel
    Launch the Sentinel monitoring page to view real-time flow control, circuit breaker and degradation status:
http://localhost:8080/console

The above is a detailed introduction on how to use Sentinel in Spring Cloud projects. By using Sentinel, we can better manage service traffic and ensure system stability and reliability. In actual projects, more complex configurations can be made according to needs to meet different flow control needs.

Guess you like

Origin blog.csdn.net/superdangbo/article/details/134543637