Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Sentinel to realize current limit in SpringCloud

2025-03-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how to use Sentinel to achieve current limit in SpringCloud, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will have something to gain after reading this SpringCloud article on how to use Sentinel to achieve current limit. Let's take a look.

Sentinel is a lightweight flow control framework for micro services, which protects the stability of services from many dimensions, such as flow control, circuit breaker degradation, system load protection and so on.

Sentinel can be used as a substitute for Hystrix to provide service breaker and service degradation for the system.

Service fuse: according to the principle that when the call to the target service times out and fails, the call to the service should be fused to release resources quickly, and all calls to it are returned quickly during this period of time to ensure the stability of the overall service system.

Service degradation: in view of the sharp increase in the pressure on core business services, other non-core services can be downgraded according to the current business scenarios and traffic, such as current limit and quick return, to release resources to ensure the normal operation of core tasks.

Current limiting principle of Sentinel

Sentinel records the total number of requests, total exceptions, total time spent and other metrics in a time window in Bucket (buckets).

A Bucket can record data within one second or within 10 milliseconds. We call this time window the statistical unit of Bucket, which is defined by the user.

So Sentinel is based on sliding window algorithm.

Set threshold metrics for Sentinel:

Thread count mode

The mode of thread count uses signal isolation to prevent the thread pool from being occupied.

There are generally two ways to prevent thread pools from being occupied:

Thread pool isolation: in order to deal with the situation that too many threads are occupied, isolation schemes are used in the industry, for example, different business logic uses different thread pools to isolate the resource contention between businesses themselves. Although this isolation scheme is better, the cost is that there are too many threads and a large overhead for thread context switching, especially for calls with low latency.

Signal isolation: sentinel adopts the scheme of signal isolation, which simply counts the number of threads in the current request context (the number of calls in progress). If the threshold is exceeded, new requests will be rejected immediately.

So the business processing is multithreaded in the case of using thread count mode.

Sentinel adopts the way of signal isolation, through the concurrent thread number mode, combined with the fuse degradation mode based on response time, it can automatically downgrade when the unstable average corresponding time is relatively high, prevent too many slow calls from occupying full concurrency, affect the whole system, and avoid the phenomenon of dependence avalanche caused by slow calls.

QPS mode

QPS, the query rate per second, is a measure of how much traffic is processed by a particular query server within a specified period of time.

QPS mode is suitable for single-reading thread situations (such as servlet requests), which provides three more precise flow control methods:

Direct rejection: direct failure

Warm Up: that is, the request QPS starts from threshold / 3 and gradually rises to the set QPS threshold after preheating, which is usually used in the second kill system.

Uniform queuing: set a waiting time to process requests at a uniform speed to ensure the uniformity of the service, and cannot handle scenarios with QPS > 1000.

There are two modes for Sentinel to calculate the threshold:

Overall cluster mode: limit the overall qps of a resource in the entire cluster to no more than this threshold.

Stand-alone sharing mode: the threshold configured in the stand-alone sharing mode is equal to the limit that the stand-alone machine can bear. Token server will calculate the total threshold based on the number of connections (for example, if 3 client are connected to the token server in the stand-alone mode, and then the split threshold is 10, the total number of clusters calculated is 30), which is limited according to the calculated total threshold. This method calculates the total threshold in real time based on the current number of connections, which is very suitable for environments where machines change frequently.

To be clear here: qps is the number of queries per second, tps is the number of transactions per second, and pv is the number of times the page has been viewed.

Sentinel flow control mode

Direct mode: when the interface reaches the current limit condition, the current limit is enabled

Association mode: when the associated resource reaches the current limit condition, the current limit is enabled

Link mode: when the resource from an interface reaches the current limit condition, the current limit is enabled.

The way of limiting current in Sentinel

Set current limit according to url in Sentinel console

Custom current limit through annotations, which can be divided into: custom url current limit and custom resource current limit.

Step 1: deploy sentinel-dashboard

Sentinel-dashboard (Click to download jar package) is a separate application that starts through spring-boot and mainly provides a lightweight console that provides machine discovery, real-time monitoring of stand-alone resources, cluster resource summarization, and rule management.

This can be understood as the sentinel service governance center.

Java-Dserver.port=18080-jar sentinel-dashboard.jar

Step 2: integrate sentinel into the project

Injection dependency

Com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.0.RELEASE pom import com.alibaba.cloud spring-cloud-starter-alibaba-sentinel 2.2.1.RELEASE

Related configurations in application.properties

Spring.application.name=imagerepairserver.port=8080spring.cloud.sentinel.transport.dashboard=127.0.0.1:18080spring.cloud.sentinel.eager=true

Controller layer

@ RestControllerpublic class UserController {@ Autowired UserService userService; @ RequestMapping ("/ hello") public String hello () {return userService.sayHello ()}

Service

Servicepublic class UserService {@ SentinelResource (value = "sayHello", fallback = "sayHellofail") public String sayHello () {return "Hello,World";} public String sayHellofail () {return "I'am sorry";}}

Set current limit and fast access, thus triggering service degradation

Set up resources for Sentinel

When the request exceeds the set threshold, start the current limit and downgrade, as shown below:

This is the end of the article on "how to use Sentinel to limit current in SpringCloud". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Sentinel to achieve current limitation in SpringCloud". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report