How to implement flow control and circuit breaker degradation?

Sentinel

Sentinel is a high availability and flow control distributed system open sourced by Alibaba. It was originally developed to solve the flow control and circuit breaker degradation problems in Alibaba's internal microservice architecture. Sentinel is designed to provide real-time flow control, circuit breaker degradation, system load protection and other functions to ensure high availability and stability of applications. The following is a detailed introduction to Sentinel:

Features and Benefits

  1. Flow control:  Sentinel allows you to define flow control rules for different resources (such as interfaces, methods, etc.), including QPS (requests per second), number of threads, concurrency, etc. This allows you to limit the access rate of different resources and prevent system crashes due to traffic surges.
  2. Real-time monitoring:  Sentinel provides real-time monitoring and statistical information, including resource traffic, response time, error rate, etc. You can use the visual monitoring interface to view this data to help you quickly identify and solve problems.
  3. Circuit breaker degradation:  Sentinel supports circuit breaker degradation mechanism. When the error rate or response time of a resource exceeds the threshold, circuit breaker can be automatically triggered to stop requests for the resource and avoid cascading failures of dependent resources.
  4. System load protection:  Sentinel can dynamically adjust flow control rules based on system load conditions to protect the system from excessive load pressure.
  5. Hotspot parameter flow control:  Sentinel allows you to define parameter-based flow control rules, which means you can restrict access to certain requests based on the parameters in the request.
  6. Supports multiple application scenarios:  Sentinel is not only suitable for flow control in microservice architecture, but also for any scenario that needs to protect and stabilize applications, including distributed systems, message queues, database access, etc.

Sentinel Architecture

Sentinel consists of the following core components:

  1. Resource:  Resource is the basic unit in Sentinel and can be any monitorable and controllable object in the application, such as interfaces, methods, etc.
  2. Flow Control Rules:  Flow control rules define how to control access to resources. Rules can set limits such as QPS, number of threads, and concurrency, and can be dynamically adjusted as needed.
  3. Metrics:  Sentinel collects real-time performance data about resources, such as traffic, response time, error rate, etc. This data is used for monitoring and statistics purposes.
  4. Rule Manager:  The Rule Manager is responsible for managing and maintaining traffic control rules. It can dynamically add, delete or modify rules as needed.
  5. Controller:  The controller is the core component of Sentinel and is responsible for implementing traffic control and circuit breaker degradation strategies. When the request for a resource reaches a threshold, the controller will reject the request or trigger a circuit breaker.
  6. Dashboard:  Sentinel provides a visual dashboard for real-time monitoring and management of traffic control rules, resource performance, etc. Dashboards make it easier for users to understand the status of the system.

Sentinel workflow

  1. Rule configuration:  Users configure traffic control rules, define which resources require traffic control, and the specific parameters and thresholds of the control rules.
  2. Resource monitoring:  Sentinel monitors performance data such as traffic, response time, error rate, etc. of each resource. This data is displayed visually in real-time on the dashboard.
  3. Flow control:  The controller decides whether to reject the request or trigger a circuit breaker based on rules and resource performance data. This ensures that access rates to resources are controlled.
  4. Alerts and notifications:  Sentinel supports alerting functionality. When the performance data of a resource exceeds a predefined threshold, an alert can be triggered and relevant personnel notified.
  5. Dynamic adjustment:  Based on monitoring data, the rule manager can dynamically adjust traffic control rules to adapt to system load changes.

Sentinel usage scenarios

Sentinel is a flow control and circuit breaker degradation library that can be applied to a variety of different usage scenarios to protect your application from sudden traffic surges, resource contention, increased error rates, and other issues. The following are some of Sentinel's main usage scenarios:

  1. API current limiting and anti-brushing:  Sentinel can be used to limit the current flow of public APIs or interfaces to prevent abuse and malicious attacks. You can set traffic control rules based on the access frequency of the interface and the request source.
  2. Service protection:  In a microservice architecture, when a service is frequently accessed or abnormal requests cause an increase in error rates, Sentinel can be used to restrict access to the service to protect it from excessive requests and errors.
  3. Circuit breaker degradation:  Sentinel provides circuit breaker degradation function, which can automatically stop sending requests to the service when the service is unstable or abnormal to prevent the spread of errors, and gradually resume requests after the service returns to normal.
  4. Load balancing:  You can use Sentinel to achieve load balancing among multiple instances or services. When the load on an instance is high, traffic can be directed to other available instances to spread the load and increase availability.
  5. Slow request degradation:  Sentinel can detect requests that take too long to process and automatically degrade or deny them to ensure fast responses to other requests.
  6. Resource pool management:  When multiple requests compete for limited resources (such as database connections or thread pools), Sentinel can help you control the allocation and use of resources to prevent resource exhaustion and congestion.
  7. Real-time monitoring and statistics:  Sentinel provides real-time monitoring and statistics functions. You can view the application's performance indicators, error rate, traffic and other information through the Sentinel console, so that problems can be discovered and adjusted in a timely manner.
  8. Dynamic configuration:  Sentinel allows you to dynamically modify traffic control rules and circuit breaker policies at runtime without restarting the application, allowing you to adjust and optimize based on actual conditions.
  9. Defense against vulnerability attacks:  Sentinel can be used to detect and defend against some common vulnerability attacks, such as brute force cracking, SQL injection, etc.
  10. Limit backend service calls:  In a microservice architecture, you can use Sentinel to control the frequency of calls to backend services to prevent excessive requests from causing instability in the backend service.

The above are just some common usage scenarios of Sentinel. It is a flexible library that can be customized and extended according to your application needs. Whether protecting applications from unstable external influences or optimizing resource utilization and performance, Sentinel is a useful tool.

Using Sentinel in golang

Using Sentinel for flow control and circuit breaker degradation in Go is relatively simple. Here are the detailed steps:

Step 1: Install Sentinel

First, you need to install the Sentinel Go client library. You can use Go Modules for dependency management by adding the following import statements to your Go project:

 
 
import (
"github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/config"
"github.com/alibaba/sentinel-golang/logging"
)

Then use  go get the command to obtain the Sentinel library:

 
 
go get github.com/alibaba/sentinel-golang@latest

Step 2: Initialize Sentinel

Sentinel needs to be initialized when your application starts. This is usually  main done within a function. You can set some global configurations of Sentinel, such as log levels, custom error pages, etc.:

 
 
func main() {
// 初始化 Sentinel
err := sentinel.InitWithConfig(&config.Config{
// 配置日志级别
LoggingLevel: logging.Info,
// 配置 Sentinel 控制台地址(可选)
ConsoleServer: "127.0.0.1:8181",
})
if err != nil {
// 处理初始化错误
panic(err)
}
// 启动你的应用程序
// ...
}

Step 3: Define resources

In Sentinel, resources are objects you want to protect, which can be interfaces, methods, HTTP routes, etc. You need to define traffic control rules for your resources. Let's say you want to protect a function  myFunction:

 
 
func myFunction() {
// 你的业务逻辑
}

You can define a Sentinel resource as follows:

 
 
resourceName := "myFunction"

Step 4: Define traffic control rules

You can define flow control rules in the initialization code to control the access rate of resources. For example, you can set the maximum number of requests allowed per second:

 
 
// 定义流量控制规则
_, err := api.LoadRules([]*base.Rule{
{
Resource: resourceName,
MetricType: base.QPS,
Count: 100, // 允许的最大 QPS
ControlBehavior: base.Reject, // 超出限制后的处理行为
},
})
if err != nil {
// 处理规则加载错误
panic(err)
}

Step 5: Use Sentinel for flow control

You can now  myFunction use Sentinel for flow control within your functions. At the entry of the function, you need to create a Sentinel context and check whether execution is allowed to continue:

 
 
func myFunction() {
// 创建 Sentinel 上下文
entry, err := api.Entry(resourceName)
if err != nil {
// 处理创建上下文错误
panic(err)
}
// 在函数返回时离开 Sentinel 上下文
defer entry.Exit()
// 如果流量控制规则限制了请求,则执行相应的处理
if entry != nil && entry.Status == base.Block {
// 请求被阻止,可以返回自定义响应或执行其他操作
fmt.Println("请求被阻止")
return
}
// 正常处理逻辑
// ...
}

In this way, when  myFunction a function is called frequently and exceeds the QPS limit defined in the rule, Sentinel will automatically reject a portion of the requests to protect your application from excessive traffic pressure.

This is just the basic usage of Sentinel. You can also define more complex traffic control rules and circuit breaker degradation strategies as needed. In addition, Sentinel also provides real-time monitoring and statistical functions, which can be viewed and managed through the Sentinel console. This gives you a better understanding of the performance and health of your application.

Guess you like

Origin blog.csdn.net/qq_41221596/article/details/133000612