带有存储桶 4j 和 Hazelcast 的 Spring 云速率限制

问题描述 投票:0回答:1

我正在使用“com.giffing.bucket4j.spring.boot.starter”在我的 Spring Cloud 项目中使用 hazelcast 来实现速率限制。

bucket4j 在应用程序启动时未初始化。因此,速率限制不起作用。

我的项目限制使用 3.1.2 spring boot 版本,根据bucket4j giffing 文档,我使用 0.10.3 版本的bucket4j starter。但速率限制不起作用。

请帮忙。

完整的代码库:

GatewaySampleApplication.java

package com.oardic.springbootbucket4j;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableCaching
public class GatewaySampleApplication {


    public static void main(String[] args) {
        SpringApplication.run(GatewaySampleApplication.class, args);
    }
    
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        //@formatter:off
        return builder.routes()
                 .route(p -> p
                            .path("/get")
                            .filters(f -> f.addRequestHeader("Hello", "World"))
                            .uri("http://httpbin.org:80"))
                .build();
        //@formatter:on
    }

}

MyController.java

package com.oardic.springbootbucket4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Mono;

@RestController
@RequestMapping
public class MyController {

    @GetMapping("/hello")
    public Mono<String> hello(
        @RequestParam(defaultValue = "World") String name) {
        return Mono.just("Hello")
            .flatMap(s -> Mono
                .just(s + ", " + name + "!\n")
            );
    }
    
    @GetMapping("/world")
    public Mono<String> world(
        @RequestParam(defaultValue = "World") String name) {
        return Mono.just("Hello")
            .flatMap(s -> Mono
                .just(s + ", " + name + "!\n")
            );
    }
    
}

application.yml

spring:
  cache:
    type: jcache
    jcache:
      provider: com.hazelcast.cache.impl.HazelcastServerCachingProvider
      config: classpath:hazelcast.xml
management:
  endpoints:
    web:
      exposure:
        include: "*"
bucket4j:
    cache-to-use: hazelcast
    enabled: true
    filters:
    -   cache-name: buckets
        rate-limits:
        -   bandwidths:
            -   capacity: 1
                time: 5
                unit: seconds
            cache-key: '@securityService.getClientIP(#this)'
        url: /myservice/endpoint/*
debug: true
logging:
  file:
    name: temp.log
    path: D://temp.log

hazelcast.xml

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           http://www.hazelcast.com/schema/config/hazelcast-config-4.1.xsd">

    <map name="buckets">
        <time-to-live-seconds>120</time-to-live-seconds>
        <in-memory-format>BINARY</in-memory-format>
        <metadata-policy>CREATE_ON_UPDATE</metadata-policy>
        <statistics-enabled>true</statistics-enabled>
    </map>

    <cache name="buckets">
    </cache>

</hazelcast>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath />
    </parent>
    
    <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
    <artifactId>bucket4j-spring-boot-starter-example-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
            <artifactId>bucket4j-spring-boot-starter</artifactId>
            <version>0.10.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.vladimir-bukhtoyarov</groupId>
            <artifactId>bucket4j-hazelcast</artifactId>
            <version>7.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
        </dependency>
        <dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
java spring spring-cloud spring-cloud-gateway bucket4j
1个回答
0
投票

通过添加更新的 Spring Boot 启动器 Bucket4j 解决了问题,该启动器具有异步解析器,并且还添加了 buket4j 相关的 jar。 bucket4j-hazelcast:com.bucket4j:8.10.1 和 griffing 0.12.8 jars

也可使用以下道具。

bucket4j.filters[1].filter-method=WEBFLUX
bucket4j.filters[1].cache-name=buckets
bucket4j.filters[1].url=^(/gateway/service).*
bucket4j.filters[1].rate-limits[0].bandwidths[0].capacity=10
bucket4j.filters[1].rate-limits[0].bandwidths[0].time=60
bucket4j.filters[1].rate-limits[0].bandwidths[0].unit=seconds
bucket4j.filters[1].rate-limits[0].bandwidths[0].refill-speed=interval

spring.cache.type=jcache
spring.cache.jcache.provider=com.hazelcast.client.cache.HazelcastClientCachingProvider
© www.soinside.com 2019 - 2024. All rights reserved.