我试图在Spring应用程序中使用限值API,使用基于以下bucket4j的在线资源
请查找我的配置如下:
下面是添加到使用4Bucketj的maven依赖项:
...
<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配置:
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:
<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个请求?
发布于 2022-05-05 15:43:43
从第6点开始,我按照本指南操作,但是我发现与您的实现唯一不同的地方是缓存系统,所以我可以猜到问题就在那里。
https://stackoverflow.com/questions/69796213
复制相似问题