


What are current limiting and fusing? Detailed explanation of Sentinel current limiting fuse degradation
This article is introduced to you by the golang tutorial column about Sentinel current limiting and circuit breaker downgrading. The content includes an introduction to current limiting and circuit breaker, as well as alibaba open source Sentinel, installation, and practical introduction. I hope it will be helpful to friends in need. !
Sentinel current limiting circuit breaker downgrade
What is current limiting\circuit break\downgrade
Current limiting: In our acquired system, if a large amount of traffic suddenly enters that day, our service can only handle 2k requests at the same time. Suddenly, 5k requests come
. This is Isn’t the pressure on the server terrible? This may directly cause the server to shut down and crash, causing the original 2K processing volume to be unable to be processed. At this time, we need to limit the current. The function of current limiting is When the number of visits reaches the highest level on the server, it is much better to not process redundant requests
than to directly hang up the server. For example, during Double Eleven, when we want to place an order, we will see something like "The request is busy, please try again later!".
Circuit breaker: I believe everyone is familiar with the circuit breaker. It is equivalent to a switch that can prevent traffic from passing through. For example, a fuse will blow when the current is too high, thus preventing damage to components.
Service circuit breaker refers to the caller accessing the service through a circuit breaker as a proxy. The circuit breaker will continue to observe the success and failure status returned by the service.
When the failure exceeds the set threshold When the circuit breaker is open, requests cannot actually access the service.
Usage scenariosWhen the service fails or is upgraded, let the client fail quickly
- Failure The processing logic is easy to define
- The response takes a long time, and the read timeout set by the client will be relatively long to prevent the client from retrying a large number of requests and causing the connection and thread resources to not be released
- * Downgrade*: Service downgrade is based on the load condition of the entire system. For some situations where the load is relatively high, in order to prevent certain functions (business
Scenario) When overload or slow response occurs, requests for some non-core interfaces and data are temporarily abandoned internally, and a
prepared fallback error handling information is directly returned. In this way, although it provides a lossy service, it ensures the stability and availability of the entire system.
Sentinel is an open source project of Alibaba. It provides multiple dimensions such as flow control, circuit breaker degradation, and system load protection to ensure the stability of services. sex.
Official website: github.com/alibaba/Sentinel/wiki
Sentinel was born in Alibaba in 2012, and its main goal is traffic control. From 2013 to 2017, Sentinel developed rapidly and became a basic component of all Alibaba microservices. It is used in more than 6000 applications, covering almost all core e-commerce scenarios. In 2018, Sentinel evolved into an open source project. In 2020, Sentinel Golang was released. Features:
Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba’s Double Eleven promotion traffic in the past 10 years, such as flash sales (i.e.
burst Traffic control within the range that the system capacity can bear), message peak shaving and valley filling, cluster traffic control, real-time fusing of downstream unavailable applications, etc.
Complete real-time monitoring: Sentinel also provides real-time monitoring functions. You can see the second-level data of a single machine connected to the application in the console, and even the summary operation status of a cluster with less than 500 machines.
* Broad Ecology*
History of SentinelIn 2012, Sentinel was born, its main function is 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 Service Mesh architecture. Solve the problem of multi-language current limit.
- In 2020, the Sentinel Go version was launched and continues to evolve in the direction of cloud native.
- In 2021, Sentinel is evolving towards the 2.0 cloud-native high-availability decision center component; at the same time, the Sentinel Rust native version 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 dyeing, flow control degradation, overload protection/instance removal, etc.; at the same time, the community will develop related standards for traffic management Extracted into the OpenSergo standard, Sentinel is implemented as a traffic management standard.
- Sentinel-go installation Sentinel-go open source address: https://github.com/alibaba/sentinel-golang
-
Official website documentation
Installation:
go get github.com/alibaba/sentinel-golang/api
Go current limiting practice
qps current limit
package main import ( "fmt" "log" sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/base" "github.com/alibaba/sentinel-golang/core/flow" ) func main() { //基于sentinel的qps限流 //必须初始化 err := sentinel.InitDefault() if err != nil { log.Fatalf("Unexpected error: %+v", err) } //配置限流规则:1秒内通过10次 _, err = flow.LoadRules([]*flow.Rule{ { Resource: "some_test", TokenCalculateStrategy: flow.Direct, ControlBehavior: flow.Reject, //超过直接拒绝 Threshold: 10, //请求次数 StatIntervalInMs: 1000, //允许时间内 }, }) if err != nil { log.Fatalf("Unexpected error: %+v", err) return } for i := 0; i < 12; i++ { e, b := sentinel.Entry("some_test", sentinel.WithTrafficType(base.Inbound)) if b != nil { fmt.Println("限流了") } else { fmt.Println("检查通过") e.Exit() } } }
Copy after loginPrint result:
检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 限流了 限流了
Copy after loginThrnotting
package main import ( "fmt" "log" "time" sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/base" "github.com/alibaba/sentinel-golang/core/flow" ) func main() { //基于sentinel的qps限流 //必须初始化 err := sentinel.InitDefault() if err != nil { log.Fatalf("Unexpected error: %+v", err) } //配置限流规则 _, err = flow.LoadRules([]*flow.Rule{ { Resource: "some_test", TokenCalculateStrategy: flow.Direct, ControlBehavior: flow.Throttling, //匀速通过 Threshold: 10, //请求次数 StatIntervalInMs: 1000, //允许时间内 }, }) if err != nil { log.Fatalf("Unexpected error: %+v", err) return } for i := 0; i < 12; i++ { e, b := sentinel.Entry("some_test", sentinel.WithTrafficType(base.Inbound)) if b != nil { fmt.Println("限流了") } else { fmt.Println("检查通过") e.Exit() } time.Sleep(time.Millisecond * 100) } }
Copy after login检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过 检查通过
Copy after loginWarrm_up
package main import ( "fmt" "log" "math/rand" "time" sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/base" "github.com/alibaba/sentinel-golang/core/flow" ) func main() { //先初始化sentinel err := sentinel.InitDefault() if err != nil { log.Fatalf("初始化sentinel 异常: %v", err) } var globalTotal int var passTotal int var blockTotal int ch := make(chan struct{}) //配置限流规则 _, err = flow.LoadRules([]*flow.Rule{ { Resource: "some-test", TokenCalculateStrategy: flow.WarmUp, //冷启动策略 ControlBehavior: flow.Reject, //直接拒绝 Threshold: 1000, WarmUpPeriodSec: 30, }, }) if err != nil { log.Fatalf("加载规则失败: %v", err) } //我会在每一秒统计一次,这一秒只能 你通过了多少,总共有多少, block了多少, 每一秒会产生很多的block for i := 0; i < 100; i++ { go func() { for { globalTotal++ e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound)) if b != nil { //fmt.Println("限流了") blockTotal++ time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) } else { passTotal++ time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) e.Exit() } } }() } go func() { var oldTotal int //过去1s总共有多少个 var oldPass int //过去1s总共pass多少个 var oldBlock int //过去1s总共block多少个 for { oneSecondTotal := globalTotal - oldTotal oldTotal = globalTotal oneSecondPass := passTotal - oldPass oldPass = passTotal oneSecondBlock := blockTotal - oldBlock oldBlock = blockTotal time.Sleep(time.Second) fmt.Printf("total:%d, pass:%d, block:%d\n", oneSecondTotal, oneSecondPass, oneSecondBlock) } }() <-ch }
Copy after loginPrint result: Gradually Reaching 1k, fluctuating up and down at the 1k position
total:11, pass:9, block:0 total:21966, pass:488, block:21420 total:21793, pass:339, block:21414 total:21699, pass:390, block:21255 total:21104, pass:393, block:20654 total:21363, pass:453, block:20831 total:21619, pass:491, block:21052 total:21986, pass:533, block:21415 total:21789, pass:594, block:21123 total:21561, pass:685, block:20820 total:21663, pass:873, block:20717 total:20904, pass:988, block:19831 total:21500, pass:996, block:20423 total:21769, pass:1014, block:20682 total:20893, pass:1019, block:19837 total:21561, pass:973, block:20524 total:21601, pass:1014, block:20517 total:21475, pass:993, block:20420 total:21457, pass:983, block:20418 total:21397, pass:1024, block:20320 total:21690, pass:996, block:20641 total:21526, pass:991, block:20457 total:21779, pass:1036, block:20677
Copy after loginGo circuit breaker practice
Here we introduce an incorrect number, view the detailed circuit breaker mechanism
error_count package main import ( "errors" "fmt" "log" "math/rand" "time" sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/circuitbreaker" "github.com/alibaba/sentinel-golang/core/config" "github.com/alibaba/sentinel-golang/logging" "github.com/alibaba/sentinel-golang/util" ) type stateChangeTestListener struct { } func (s *stateChangeTestListener) OnTransformToClosed(prev circuitbreaker.State, rule circuitbreaker.Rule) { fmt.Printf("rule.steategy: %+v, From %s to Closed, time: %d\n", rule.Strategy, prev.String(), util.CurrentTimeMillis()) } func (s *stateChangeTestListener) OnTransformToOpen(prev circuitbreaker.State, rule circuitbreaker.Rule, snapshot interface{}) { fmt.Printf("rule.steategy: %+v, From %s to Open, snapshot: %d, time: %d\n", rule.Strategy, prev.String(), snapshot, util.CurrentTimeMillis()) } func (s *stateChangeTestListener) OnTransformToHalfOpen(prev circuitbreaker.State, rule circuitbreaker.Rule) { fmt.Printf("rule.steategy: %+v, From %s to Half-Open, time: %d\n", rule.Strategy, prev.String(), util.CurrentTimeMillis()) } func main() { //基于连接数的降级模式 total := 0 totalPass := 0 totalBlock := 0 totalErr := 0 conf := config.NewDefaultConfig() // for testing, logging output to console conf.Sentinel.Log.Logger = logging.NewConsoleLogger() err := sentinel.InitWithConfig(conf) if err != nil { log.Fatal(err) } ch := make(chan struct{}) // Register a state change listener so that we could observer the state change of the internal circuit breaker. circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{}) _, err = circuitbreaker.LoadRules([]*circuitbreaker.Rule{ // Statistic time span=10s, recoveryTimeout=3s, maxErrorCount=50 { Resource: "abc", Strategy: circuitbreaker.ErrorCount, RetryTimeoutMs: 3000, //3s只有尝试回复 MinRequestAmount: 10, //静默数 StatIntervalMs: 5000, Threshold: 50, }, }) if err != nil { log.Fatal(err) } logging.Info("[CircuitBreaker ErrorCount] Sentinel Go circuit breaking demo is running. You may see the pass/block metric in the metric log.") go func() { for { total++ e, b := sentinel.Entry("abc") if b != nil { // g1 blocked totalBlock++ fmt.Println("协程熔断了") time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) } else { totalPass++ if rand.Uint64()%20 > 9 { totalErr++ // Record current invocation as error. sentinel.TraceError(e, errors.New("biz error")) } // g1 passed time.Sleep(time.Duration(rand.Uint64()%20+10) * time.Millisecond) e.Exit() } } }() go func() { for { total++ e, b := sentinel.Entry("abc") if b != nil { // g2 blocked totalBlock++ time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) } else { // g2 passed totalPass++ time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond) e.Exit() } } }() go func() { for { time.Sleep(time.Second) fmt.Println(totalErr) } }() <-ch }
Copy after login
The above is the detailed content of What are current limiting and fusing? Detailed explanation of Sentinel current limiting fuse degradation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

The Go framework stands out due to its high performance and concurrency advantages, but it also has some disadvantages, such as being relatively new, having a small developer ecosystem, and lacking some features. Additionally, rapid changes and learning curves can vary from framework to framework. The Gin framework is a popular choice for building RESTful APIs due to its efficient routing, built-in JSON support, and powerful error handling.

Best practices: Create custom errors using well-defined error types (errors package) Provide more details Log errors appropriately Propagate errors correctly and avoid hiding or suppressing Wrap errors as needed to add context

Common problems and solutions in Go framework dependency management: Dependency conflicts: Use dependency management tools, specify the accepted version range, and check for dependency conflicts. Vendor lock-in: Resolved by code duplication, GoModulesV2 file locking, or regular cleaning of the vendor directory. Security vulnerabilities: Use security auditing tools, choose reputable providers, monitor security bulletins and keep dependencies updated.

In Go framework development, common challenges and their solutions are: Error handling: Use the errors package for management, and use middleware to centrally handle errors. Authentication and authorization: Integrate third-party libraries and create custom middleware to check credentials. Concurrency processing: Use goroutines, mutexes, and channels to control resource access. Unit testing: Use gotest packages, mocks, and stubs for isolation, and code coverage tools to ensure sufficiency. Deployment and monitoring: Use Docker containers to package deployments, set up data backups, and track performance and errors with logging and monitoring tools.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.
