首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧引导(.2.3.0.RELEASE),bucket4j速率限制不起作用

弹簧引导(.2.3.0.RELEASE),bucket4j速率限制不起作用
EN

Stack Overflow用户
提问于 2021-11-01 11:21:05
回答 1查看 962关注 0票数 4

我试图在Spring应用程序中使用限值API,使用基于以下bucket4j的在线资源

请查找我的配置如下:

下面是添加到使用4Bucketj的maven依赖项:

代码语言:javascript
复制
...
    <dependency>
            <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
            <artifactId>bucket4j-spring-boot-starter</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
..

下面是我的application.yml配置:

代码语言:javascript
复制
spring:
  main:
    allow-bean-definition-overriding: true
  cache:
    jcache:
      config: classpath:ehcache.xml
bucket4j:
  enabled: true
  filters:
    - cache-name: buckets
      url: .*
      http-response-body: "{ \"status\": 429, \"error\": \"Too Many Requests\", \"message\": \"You have exhausted your API Request Quota\" }"
      rate-limits:
        - bandwidths:
            - capacity: 2
              time: 1
              unit: minutes

下面我在路径src/main/resources/中添加了ehcache.xml:

代码语言:javascript
复制
<config xmlns='http://www.ehcache.org/v3'
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
                            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
    <cache alias="buckets">
        <expiry>
            <ttl unit="seconds">3600</ttl>
        </expiry>
        <heap unit="entries">1000000</heap>
        <jsr107:mbeans enable-statistics="true"/>
    </cache>

最后,我在一个配置类中添加了@EnableCaching,这个配置类在春季开始时被加载。

根据application.yml文件中的配置,API不应在一分钟内接受超过2个请求,如果超过两个请求,则应按application.yml中所述(http-response-body)触发错误。

我已经部署在我的个人电脑上的应用程序和邮递员工具,我能够在一分钟内点击一个Rest超过10次,没有错误信息。

谁能告诉我为什么错误信息不会被触发,因为它应该只允许2个请求,而我正在发送10个请求?

EN

回答 1

Stack Overflow用户

发布于 2022-05-05 15:43:43

从第6点开始,我按照本指南操作,但是我发现与您的实现唯一不同的地方是缓存系统,所以我可以猜到问题就在那里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69796213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档