directory
- preface
- What is flow control
- Introduction to flow control
- Basic flow control rules
- Flow control test
- conclusion
preface
SpringCloud Alibaba into a pit (three) Sentinel entry
The last article introduced the basic usage of Sentinel. In this chapter, we will start to learn the key point: Sentinel flow control rules
What is flow control
Flow control is flow control, so why flow control?
Large Internet applications have a large amount of user traffic. When they encounter some special time nodes, such as “Double Eleven” and “Spring Festival”, a large amount of traffic suddenly floods into the system, far exceeding the processing capacity of the server. The server may directly break down, causing the whole system to become unavailable.
So for this kind of problem, we need to limit the flow. For the flow exceeding the limit, we can solve it by means of circuit breaker, downgrade, queuing and so on.
Introduction to flow control
Now let’s explain the basic concepts of flow control by talking and practicing.
Let’s start by implementing the API from the previous article:
Then we enter the Sentinel background, in the cluster point link, you can see/Hello, and you can see the button to add flow control on the right side of the list
Basic flow control rules
Hit the flow control button, you can see
In the flow control rules form, you can see many options, so what are these?
We highlight the following three:
-
The resource name is the name of the API interface
-
Threshold Type A threshold is the maximum value. For example, if the boiling point of water is 100 degrees, the temperature threshold of water is 100 degrees. Then the threshold type is the different type of maximum, and there are two:
- Query Per Seconds (QPS) Number of requests Per second, the number of resource requests within one second. When the number exceeds the threshold, traffic limiting is enabled
- Number of threads This is the number of threads accessing resources. If the number exceeds the threshold, traffic limiting is enabled
-
Single-machine threshold This is the maximum value set based on the type selected above
Flow control test
Just reading the text may not be easy to understand, so let’s try to do it.
QPS: Select QPS as the threshold type, and then set the single-machine threshold to 1, that is, if the number of requests to/Hello interface exceeds 1 within 1 second, the flow limiting will be started. Finally, click Add, and the added flow control rule appears below:
Click on /hello slowly and it’s fine. Click on it quickly (multiple times per second) and the following text will appear, meaning Sentinel has restricted the stream.
Then we edit the flow control rule, change the QPS to the number of threads, set the threshold to 3, and click Save.
Then our browser tests the interface and we can see that no matter how fast the interface is clicked, the interface is accessible without limiting traffic.
So how do you limit the number of threads?
We can use JMeter to test, the basic use of JMeter is not introduced here, you can baidu.
- Adding a thread Group
- Set the number of threads to 5 and loop forever, which means that five threads are started at a time to access the interface above
- Add sampler
Configure interface access options
- You can add a listener to see the result
- Finally, click the launch test button above
The result: limiting current
conclusion
In this article, we introduce the basic flow control rules of Sentinel. There are two types of flow control rules: QPS and thread count. As to which one to use, it depends on whether our specific business and server resources are sensitive to requests or threads.
- The text that appears on the stream limit is Sentinel’s own text. Can we make it our own, or use a better way to inform users?
- There is also an advanced option in the flow control rules, what does it do?
I will introduce it in a later blog post, thank you for reading it, please give a thumbs up if you find it useful 🙂
If you need to learn more about Java, click here for a more detailed summary of Java knowledge