First, practical instructions

1.1 Effect Description

  1. This section describes how to use Semaphore to limit traffic on a single interface
    • The A interface of system A mainly calls system B, and now we want to limit the flow of system B, for example, processing requests with A peak value of 100, and requests that exceed 100 fail quickly
    • The interface acts as a master gate and wants to limit all external access, such that a room can only have 100 players online at a time, and requests can only continue until the previous one has been processed
    • Other types of scenarios, where resources are fixed and need to be used in rotation, can use Semaphore
  2. Inapplicable Scenarios
    • Traffic limiting is implemented for the total entry. Therefore, traffic limiting cannot be performed based on IP addresses or tokens. The applicable scenarios are fixed
    • How to use other IP/Token level traffic limiting such as AOP+Redis+Lua in a future blog post
  3. The main advantages are
    • Compared to IP level traffic limiting, Semaphore is relatively simple to implement and can be implemented quickly in these scenarios
  4. Implementation and principle difficulty: ☆☆

1.2 Core Knowledge points

  1. The following technical points are mainly used
    • springboot
    • Semaphore in juc package (tryAcquire, release)
  2. This section describes Semaphore
    • Semaphore can acquire a license through tryAcquire and acquire (default), and release the license through release
    • The main difference between the two licenses is that the former is non-blocking and the latter is blocking, and we must use the non-blocking license acquisition method if we are to achieve the effect of fast failure
  3. Pay attention to the point
    • Make sure that the release method is called, such as in finally, otherwise the license will not be released and the interface will be completely occupied and unable to receive requests

Two, environment construction

  1. The environment was created using IDEA + Spring Initializr
  2. Create a New SpringBoot Web project

  1. Package Directory Description

  1. Create a SemaphoreController with the request path set to LIMIT
    • Note the code comments
package com.codecoord.semaphore.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Semaphore;

/** * Current limiting side test **@author [email protected]
 * @sinceThe 2020-08-06 * /
@RestController
public class SemaphoreController {
    /** * The maximum semaphore, such as 3 here. The generated environment can be made configurable and injected through injection */
    private static final int MAX_SEMAPHORE = 3;
    /** * Semaphore main flow limit */
    private static final Semaphore SEMAPHORE = new Semaphore(MAX_SEMAPHORE);

    @RequestMapping("/limit")
    public String limit(a) {
        // 01. Use a non-blocking tryAcquire. If tryAcquire is not available, a quick return fails
        if(! SEMAPHORE.tryAcquire()) {return "Request frequency exceeds limit:" + MAX_SEMAPHORE;
        }

        // 02. If you can enter here, you must have obtained a permit
        /// Todo possible argument verification, be sure to call the release method if the parameter verification does not pass
        /*if (valid(xxx)) { SEMAPHORE.release(); } * /

        try {
            // 03. Simulate business processing, if it takes 1 second
            Thread.sleep(1000);
            return "Business processing successful";
        } catch (InterruptedException e) {
            // Error handling
            return "Business processing failure";
        } finally {
            // 04. The interface must be released. Otherwise, the interface is suspended animation and cannot process the requestSEMAPHORE.release(); }}}Copy the code

Three, the current limiting demonstration

3.1 Concurrent request tool

  1. Concurrent requests will be tested based on Apache-JMeter-5.2.1. Jmeter is relatively simple to use, so readers are required to make their own decisions
  2. The number of threads in the thread group is 10. Adjust the size as required in the real environment

3.2 Effect sample diagram

  1. Starting the tests, you can see that only three of them were processed successfully, and the remaining seven all failed

The request is successful

The request failed