Spring Cloud项目中使用Bucket4j 0.10.3与Hazelcast实现限流时Bucket未初始化导致功能失效的问题求助
Spring Cloud项目中使用Bucket4j 0.10.3与Hazelcast实现限流时Bucket未初始化导致功能失效的问题求助
大家好,我正在Spring Cloud项目里尝试用com.giffing.bucket4j.spring.boot.starter配合Hazelcast实现限流功能,但遇到了一个头疼的问题:Bucket4j在应用启动时完全没有初始化,导致限流规则根本不起作用。
我的项目有版本限制,必须使用Spring Boot 3.1.2,根据Bucket4j Giffing的官方文档,我特意搭配了0.10.3版本的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>
备注:内容来源于stack exchange,提问作者spyder
相关产品推荐
相关产品推荐

