spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.55k stars 3.33k forks source link

Spring Cloud Gateway + IntelliJ - cannot autowire: circuitBreakerConfig: Consumer<SpringCloudCircuitBreakerFilterFactory.Config> #3601

Open dreamstar-enterprises opened 5 days ago

dreamstar-enterprises commented 5 days ago

In IntelliJ Idea I keep getting a cannot autowire bean error.

The exact error is:

enter image description here

Spring Cloud Gateway

Here is my routelocator

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

    /**
     * Creates route definitions with multiple resilience patterns for the Resource Server.
     * Implements a chain of filters in the following order:
     * 1. Token relay for authentication
     * 2. Bulkhead pattern to limit concurrent requests
     * 3. Rate limiting to prevent overload
     * 4. Time limiting for request timeouts
     * 5. Circuit breaker for fault tolerance
     * 6. Retry pattern for transient failures
     * 7. Request header cleanup
     *
     * @param builder RouteLocatorBuilder for creating route definitions
     * @param tokenRelayGatewayFilterFactory Filter factory for OAuth2 token relay
     * @param requestRateLimiterConfig Configuration for request rate limiting
     * @param timeLimiterFilterFactory Filter factory for request timeouts
     * @param bulkheadFilterFactory Filter factory for concurrent request limiting
     * @param retryConfig Configuration for retry behavior
     * @param retryFilterFactory Filter factory for retry mechanism
     * @param circuitBreakerConfig Configuration for circuit breaker behavior
     * @return RouteLocator with configured routes and filters
     */
    @Bean
    fun routeLocator(
        builder: RouteLocatorBuilder,
        tokenRelayGatewayFilterFactory: TokenRelayGatewayFilterFactory,
        requestRateLimiterConfig: RequestRateLimiterConfig,
        timeLimiterFilterFactory: TimeLimiterFilterFactory,
        bulkheadFilterFactory: BulkheadFilterFactory,
        retryConfig: Consumer<RetryGatewayFilterFactory.RetryConfig>,
        retryFilterFactory: RetryFilterFactory,
        circuitBreakerConfig: Consumer<SpringCloudCircuitBreakerFilterFactory.Config>
    ): 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 bulkhead limiter
                        f.filter(bulkheadFilterFactory.apply())

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

                        // 4. Request time limiter
                        f.filter(timeLimiterFilterFactory.apply())

                        // 5. Circuit Breaker for fault tolerance
                        f.circuitBreaker(circuitBreakerConfig)

                        // 6. Retry filter for transient failures (will ignore above exceptions)
                        f.retry(retryConfig)
                        f.filter(retryFilterFactory.apply())

                        // 7. Basic request cleanup
                        f.removeRequestHeader("Cookie")
                    }
                    .uri(serverProperties.resourceServerUri)
            }
            .build()
    }

}

Circuit Breaker Bean

And the bean is clearly there - this doesn't show any errors

/**
 * Configuration for Spring Cloud Gateway circuit breaker functionality.
 * Provides both Resilience4J circuit breaker core configuration and gateway filter configuration.
 *
 * @property circuitBreakerProperties Properties for configuring Resilience4J circuit breaker behavior
 */
@Configuration
internal class CircuitBreakerConfig(
    private val circuitBreakerProperties: CircuitBreakerResilience4JProperties
) {

    /**
     * Creates a gateway circuit breaker configuration consumer.
     * Configures:
     * - Fallback route for failed requests
     * - HTTP 5xx series error codes that trigger the circuit breaker
     *
     * @return Consumer function to configure circuit breaker gateway filter
     */
    @Bean
    fun circuitBreakerConfig(): Consumer<SpringCloudCircuitBreakerFilterFactory.Config> = Consumer { config ->
        config.setName("resourceServerCircuitBreaker")
        config.setFallbackUri("forward:/fallback")
        config.setStatusCodes(
            setOf(
                HttpStatus.INTERNAL_SERVER_ERROR.value().toString(),            // 500
                HttpStatus.NOT_IMPLEMENTED.value().toString(),                  // 501
                HttpStatus.BAD_GATEWAY.value().toString(),                      // 502
                HttpStatus.SERVICE_UNAVAILABLE.value().toString(),              // 503
                HttpStatus.GATEWAY_TIMEOUT.value().toString(),                  // 504
                HttpStatus.HTTP_VERSION_NOT_SUPPORTED.value().toString(),       // 505
                HttpStatus.VARIANT_ALSO_NEGOTIATES.value().toString(),          // 506
                HttpStatus.INSUFFICIENT_STORAGE.value().toString(),             // 507
                HttpStatus.LOOP_DETECTED.value().toString(),                    // 508
                HttpStatus.BANDWIDTH_LIMIT_EXCEEDED.value().toString(),         // 509
                HttpStatus.NOT_EXTENDED.value().toString(),                     // 510
                HttpStatus.NETWORK_AUTHENTICATION_REQUIRED.value().toString()   // 511
            )
        )
    }
}

Can someone help?

I believe it must be something to do with the Spring Component Scanning setup, since it quite quickly shows it as a bean that cannot be autowired.