自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

神器!API接口限流就是這么簡單

開發(fā) 前端
訪問速率限制是一種API訪問限制的策略。它限制客戶端在一定時間內(nèi)調(diào)用 API 的次數(shù)。這有助于保護應(yīng)用程序接口,防止無意或惡意的過度使用。

環(huán)境:SpringBoot3.2.5

1. 簡介

訪問速率限制是一種API訪問限制的策略。它限制客戶端在一定時間內(nèi)調(diào)用 API 的次數(shù)。這有助于保護應(yīng)用程序接口,防止無意或惡意的過度使用。

速率限制通常是通過跟蹤 IP 地址或更具體的業(yè)務(wù)方式(如 API 密鑰或訪問令牌等方式)來應(yīng)用于 API 的。作為 API 開發(fā)人員,當客戶端達到限制時,我們有幾種選擇:

  • 請求排隊,直到剩余時間結(jié)束(這也是最常用的方式)
  • 拒絕請求(HTTP 429 請求過多)

本篇文章將介紹一款開源的組件Bucket4j,該組件提供了強大的限流功能。基于基于令牌桶算法。既可用于獨立的 JVM 應(yīng)用程序,也可用于集群環(huán)境。它還通過 JCache(JSR107)規(guī)范支持內(nèi)存或分布式緩存。

令牌桶算法

假設(shè)我們有一個 "桶",其容量被定義為可容納的令牌數(shù)量。每當消費者想要訪問 API 端點時,就必須從桶中獲取一個令牌。如果有令牌,我們就會從數(shù)據(jù)桶中移除令牌,并接受請求。反之,如果程序桶中沒有令牌,我們就會拒絕請求。

在請求消耗令牌(token)的同時,我們也在以某種固定的速度補充令牌。

考慮一個速率限制為每分鐘 100 個請求的應(yīng)用程序接口。我們可以創(chuàng)建一個容量為 100 的水桶,每分鐘填充 100 個令牌。如果我們在一分鐘內(nèi)收到 70 個請求,少于可用令牌的數(shù)量,那么在下一分鐘開始時,我們只需再添加 30 個令牌,就能使水桶達到容量。另一方面,如果我們在 40 秒內(nèi)用完了所有令牌,我們將等待 20 秒來重新裝滿令牌桶。

接下來將詳細介紹在Spring Boot中如何使用Bucket4j實現(xiàn)限流。

2. 實戰(zhàn)案例

2.1 環(huán)境準備

引入依賴

<dependency>
  <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
  <artifactId>bucket4j-spring-boot-starter</artifactId>
  <version>0.12.7</version>
</dependency>
<dependency>
  <groupId>com.bucket4j</groupId>
  <artifactId>bucket4j-redis</artifactId>
  <version>8.10.1</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-core</artifactId>
</dependency>

接下來的案例是基于redis的,所以引入了jedis。你也可以是lettuce或者是redisson但是這2個貌似需要是webflux環(huán)境。

jedis配置

@Bean
public JedisPool jedisPool(
    @Value("${spring.data.redis.port}") Integer port,
    @Value("${spring.data.redis.host}") String host,
    @Value("${spring.data.redis.password}") String password,
    @Value("${spring.data.redis.database}") Integer database
  ) {
  // buildPoolConfig方法自己進行配置吧
  final JedisPoolConfig poolConfig = buildPoolConfig();
  return new JedisPool(poolConfig, host, port, 60000, password, database);
}

以上基礎(chǔ)環(huán)境就準備好了,接下來就可以進行規(guī)則配置。而規(guī)則的配置可以基于2中方式,基于配置文件和基于注解(AOP)。

定義接口

@RestController
@RequestMapping("/products")
public class ProductController {


  @GetMapping("/{id}")
  public Product getProduct(@PathVariable Integer id) {
    return new Product(id, "商品 - " + id, BigDecimal.valueOf(new Random().nextDouble(1000))) ;
  }
}

接下來我將基于上面的接口進行限流的配置。

2.2 基于配置文件

基于配置文件的規(guī)則配置底層實現(xiàn)是通過Filter。

bucket4j:
  cache-to-use: redis-jedis
  filter-config-caching-enabled: true
  filters:
  - cache-name: product_cache_name
    id: product_filter
    # 配置請求url的規(guī)則;這里底層是通過正則表達式進行匹配的
    url: /products/.*
    rate-limits:
    - 
      #這里的cache-key非常關(guān)鍵;用于區(qū)分不同請求的情況;
      #比如,這里我會根據(jù)不同的請求id來現(xiàn)在訪問速率
      #這里可以寫spel表達式,這里調(diào)用的是HttpServletRequest#getParameter方法
      cache-key: getParameter("id")
      bandwidths:
      #配置桶的容量
      - capacity: 2
        # 時間
        time: 30
        # 單位
        unit: seconds
        # 填充速度;這會每隔30秒進行填充
        refill-speed: interval

如何配置后,訪問/products/xxx接口,結(jié)果如下:

圖片圖片

圖片圖片

分別請求id為1,2時,如果超過了配置桶的容量,則默認顯示上面的錯誤消息,同時Redis中會分別記錄這2個key

圖片圖片

只有等到30s后才能繼續(xù)訪問。

修改默認的限流提示

bucket4j:
  filters:
  - cache-name: product_cache_name
    http-content-type: 'application/json;charset=utf-8'
    http-response-body: '{"code": -1, "message": "請求太快了"}'

注意:你必須同時要設(shè)置content-type設(shè)置字符編碼,否則會亂碼。

條件放行

你也可以通過如下屬性進行有條件的放行;

bucket4j:
  filters:
  - cache-name: product_cache_name
    rate-limits:
    - 
      skip-condition: 'getParameter("id").equals("6")'

當請求id的值為6時則跳過規(guī)則,直接方向。

以上是基于配置文件規(guī)則的應(yīng)用,它還有很多其它的配置屬性,詳細查看官方文檔

https://github.com/MarcGiffing/bucket4j-spring-boot-starter

接下來介紹基于注解的方式。

2.3 基于注解

通過"@RateLimiting"注解,AOP 可以攔截目標方法。這樣,你就可以全面訪問方法參數(shù),輕松定義速率限制鍵或有條件地跳過速率限制。

配置文件中配置規(guī)則

bucket4j:
  methods:
  - name: storage_rate #在代碼中會通過該名稱引用
    cache-name: storage_cache_name
    rate-limit:
      bandwidths:
      - capacity: 2
        time: 30
        unit: seconds
        refill-speed: interval

接口注解,配置限流

@RateLimiting(
    name = "storage_rate", 
    cacheKey = "'storage-' + #id",
    skipCondition = "#name eq 'admin'",
    ratePerMethod = true,
    fallbackMethodName = "getStorageFallback"
  )
@GetMapping("/{id}")
public R<Storage> getStorage(@PathVariable Integer id, String name) {
  return R.success(new Storage(id, "SP001 - " + id, new Random().nextInt(10000))) ;
}
// 回退方法的簽名必須與業(yè)務(wù)方法一致
public R<Storage> getStorageFallback(Integer id, String name) {
  return R.failure(String.format("請求id=%d,name=%s被限流", id, name)) ;
}

skipCondition:該屬性定義了如果請求的name的值為admin則跳過,不限流。

圖片圖片

圖片圖片

@RateLimiting注解還可以應(yīng)用到類中,這樣該類中的所有方法都會被限流,如下示例:

@Service
@RateLimiting(
    name = "storage_rate", 
    cacheKey = "getName",
    ratePerMethod = false
  )
public class StorageService {


  public Storage queryStorageById(Integer id) {
    return new Storage(id, "SP001 - " + id, new Random().nextInt(10000)) ;
  }
  
  @IgnoreRateLimiting
  public List<Storage> queryStorages() {
    return List.of(
        new Storage(1, "SP001 - " + 1, new Random().nextInt(10000)),
        new Storage(2, "SP002 - " + 2, new Random().nextInt(10000)),
        new Storage(3, "SP003 - " + 3, new Random().nextInt(10000))
      ) ;
  }
}

上面代碼queryStorageById會被限流,而queryStorages方法被@IgnoreRateLimiting注解標準,所以不會被限流。

關(guān)于Bucket4j就介紹這么多,它還有非常多的其它功能,你可以參考官方文檔進行查看。

責任編輯:武曉燕 來源: Spring全家桶實戰(zhàn)案例源碼
相關(guān)推薦

2024-09-09 11:35:35

2024-12-03 08:43:49

2023-08-21 08:01:03

2017-11-28 15:29:04

iPhone X網(wǎng)頁適配

2021-05-24 10:50:10

Git命令Linux

2020-06-16 10:57:20

搭建

2016-07-22 15:12:12

Win10技巧重裝

2019-05-27 14:03:48

開發(fā)技能代碼

2023-08-26 21:42:08

零拷貝I/O操作

2020-04-20 10:47:57

Redis數(shù)據(jù)開發(fā)

2021-12-27 07:31:37

JavaNeo4J數(shù)據(jù)庫

2021-02-26 10:21:35

比特幣投資金融

2023-07-27 08:26:36

零拷貝I/O操作

2021-10-28 19:23:27

界面嵌入式 UI

2019-05-13 08:24:58

數(shù)據(jù)庫MySQLInnoDB

2024-02-27 08:14:51

Nginx跨域服務(wù)

2013-06-09 10:34:24

華為網(wǎng)絡(luò)規(guī)劃企業(yè)ICT

2020-07-27 07:00:00

超文本鏈接Word文檔網(wǎng)絡(luò)

2017-11-06 16:30:33

開源

2020-06-17 11:42:50

異常解析器Spring MVC
點贊
收藏

51CTO技術(shù)棧公眾號