最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Spring Cloud Gateway - how to apply timeout limiter to specific endpoint, using Kotlin - Stack Overflow

matteradmin5PV0评论

How do I apply a timeout limiter in Spring to a specific endpoint?

E.g. I have, but I am stuck on step 3:

RouteLocator

@Configuration
internal class RoutingConfig(
    private val serverProperties: ServerProperties,
    private val ignoreFilter: IgnoreFilterConfig,
) {

    private val logger = LoggerFactory.getLogger(RoutingConfig::class.java)

    @Bean
    fun routeLocator(
        builder: RouteLocatorBuilder,
        tokenRelayGatewayFilterFactory: TokenRelayGatewayFilterFactory,
        requestRateLimiterConfig: RequestRateLimiterConfig,
        timeLimiterRegistry: TimeLimiterRegistry,
    ): RouteLocator {
        return builder.routes()

            // routing for Resource Server
            .route("resource-server") { r ->
                r.path("${serverProperties.resourceServerPrefix}/**")
                    .filters { f ->

                        // 1. Token relay filter first for authentication
                        f.filter(tokenRelayGatewayFilterFactory.apply())

                        // 2. Request rate limiter
                        f.filter(requestRateLimiterConfig.apply(RequestRateLimiterConfig.Config()))

                        // 3. Time limiter
                        val timeLimiter = timeLimiterRegistry.timeLimiter("resourceServerTimeLimiter")
                        }

And then here is my actual timeout configuration class

Timeout Limiter Class

@Configuration
internal class TimeLimiterConfig(
    private val timeOutLimiterProperties: TimeOutLimiterProperties
) {

    // default configuraiton
    @Bean
    fun customTimeLimiterConfig(): TimeLimiterConfig {
        return TimeLimiterConfig.custom()
            .timeoutDuration(timeOutLimiterProperties.timeOutDuration)
            .cancelRunningFuture(timeOutLimiterProperties.cancelRunningFuture)
            .build()
    }

    @Bean
    fun customTimeLimiterRegistry(
        customTimeLimiterConfig: TimeLimiterConfig
    ): TimeLimiterRegistry {
        return TimeLimiterRegistry.of(customTimeLimiterConfig)
    }

    @Bean
    fun resourceServerTimeLimiter(
        timeLimiterRegistry: TimeLimiterRegistry,
        customTimeLimiterConfig: TimeLimiterConfig
    ): TimeLimiter {
        return timeLimiterRegistry.timeLimiter(
            "resourceServerTimeLimiter",
            customTimeLimiterConfig
        )
    }
}

The most I could find in terms of guidance were:

/

Post a comment

comment list (0)

  1. No comments so far