How to set speed limit for each user in spring boot?

I am developing a Spring Boot Rest API that handles many inbound requests. My controller is something like below:

@RestController

public class ApiController {
    List<ApiObject>  apiDataList;   

    @RequestMapping(value="/data",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
    public ResponseEntity<List<ApiObject>> getData(){                                       
        List<ApiObject> apiDataList=getApiData();
        return new ResponseEntity<List<ApiObject>>(apiDataList,HttpStatus.OK);
    }
    @ResponseBody 
    @Async  
    public List<ApiObject>  getApiData(){
        List<ApiObject>  apiDataList3=new List<ApiObject> ();
        //do the processing
        return apiDataList3;
    }
}

So now I wanted to install ratelimit for each user. Say, each user can request only 5 requests per minute or something like that. How to set a speed limit for each user to make only 5 api calls per minute, and if the user requests more than I can send a 429 answer? Do we need an IP address?

Any help is appreciated.

+9
source share
2 answers

Spring.

  • . . . Token Bucket, .
  • , . API-, . Zuul
  • , Mulesoft ESB, API .
  • , , API-, , . MuleSoft, WSO2, 3Scale, Kong .. ( ,
+9

Spring .

bucket4j-spring-boot-starter, bucket4j token-bucket API REST. . IP- .

, 5 10 :

bucket4j:
  enabled: true
  filters:
  - cache-name: buckets
    url: .*
    rate-limits:
    - bandwidths:
      - capacity: 5
    time: 10
    unit: seconds

Netflix Zuul, Spring Cloud Zuul RateLimit, : Consul, Redis, Spring Data Bucket4j.

0

Source: https://habr.com/ru/post/1677437/


All Articles