首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于IP的速率限制在弹簧引导中的应用

基于IP的速率限制在弹簧引导中的应用
EN

Stack Overflow用户
提问于 2020-07-23 06:24:25
回答 2查看 1.9K关注 0票数 2

我想为我的项目申请基于IP (/get)服务的速率限制,例如,每个IP都可以访问特定的no的每个服务。时间,即5次/10秒。我使用过bucket4j速率限制,但它不起作用。有人知道这件事吗?

这是我的application.properties

代码语言:javascript
复制
spring.cache.jcache.config=classpath:ehcache.xml
logging.level.org.ehcache=info
bucket4j.enabled=true
bucket4j.filters[0].cache-name=buckets
bucket4j.filters[0].filter-method=servlet
bucket4j.filters[0].url=.* 
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=5
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=10
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=seconds
bucket4j.filters[0].rate-limits[0].expression="getRemoteAddress()"
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval=0
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval-unit=seconds

我是encache.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">6</ttl></expiry>
        <heap unit="entries">2000</heap>
        <jsr107:mbeans enable-statistics="true"/>
    </cache>
</config>

pom.xml

代码语言:javascript
复制
<dependency>
    <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
    <artifactId>bucket4j-spring-boot-starter</artifactId>
    <version>0.2.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>

<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
    <scope>runtime</scope>
</dependency>

服务器初始化器

代码语言:javascript
复制
@EnableCaching
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(EwseparationApplication.class);
    }

}
EN

回答 2

Stack Overflow用户

发布于 2022-02-21 07:08:27

我也有同样的问题。看起来,bucket4j spring启动github项目(链接)出现了一个问题。

我将我的application.properties转换为application.yml,就像在问题中报告的个人一样,我的利率限制也开始起作用。我不知道实际问题是什么(为什么application.yml工作,而application.properties不工作)。比起yml,我更喜欢属性格式。

票数 0
EN

Stack Overflow用户

发布于 2022-10-19 14:20:06

您可以使用.yaml,因为.properties在识别标记时仍然存在一些问题。

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

https://stackoverflow.com/questions/63048121

复制
相关文章

相似问题

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