spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.18k stars 758 forks source link

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration] #973

Closed SirZiWang closed 5 months ago

SirZiWang commented 5 months ago

Describe the bug Describe the bug spring-boot 3.0.5 spring-cloud 2022.0.0 spring-cloud-alibaba 2022.0.0.0-RC1 openfeign 3.8.0

error stack

[pool-5-thread-1] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration]
Exception in thread "pool-5-thread-1" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:178)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:398)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:283)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:747)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565)
        at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:138)
        at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:122)
        at org.springframework.cloud.context.named.NamedContextFactory.getInstances(NamedContextFactory.java:236)
        at org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient.lambda$execute$2(RetryableFeignBlockingLoadBalancerClient.java:140)
        at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
        at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:225)
        at org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient.execute(RetryableFeignBlockingLoadBalancerClient.java:135)
        at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:102)
        at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:72)
        at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:98)
        at org.springframework.cloud.openfeign.FeignCachingInvocationHandlerFactory$1.proceed(FeignCachingInvocationHandlerFactory.java:66)
        at org.springframework.cache.interceptor.CacheInterceptor.lambda$invoke$0(CacheInterceptor.java:54)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:351)
        at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:64)
        at org.springframework.cloud.openfeign.FeignCachingInvocationHandlerFactory.lambda$create$1(FeignCachingInvocationHandlerFactory.java:53)
        at jdk.proxy2/jdk.proxy2.$Proxy190.deviceControl(Unknown Source)
        at com.lenovo.starworld.task.strategy.impl.CameraStrategyImpl.lambda$cameraCtrl$4(CameraStrategyImpl.java:239)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at com.xx.xx.impl.CameraStrategyImpl.cameraCtrl(CameraStrategyImpl.java:205)
        at com.xx.xx.impl.impl.CameraStrategyImpl.taskExec(CameraStrategyImpl.java:141)
        at com.xx.xx.impl.CameraStrategyImpl.lambda$taskCtl$0(CameraStrategyImpl.java:109)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
        at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:333)
        at org.springframework.context.annotation.ConditionEvaluator.getCondition(ConditionEvaluator.java:124)
        at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:96)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:164)
        ... 30 more
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.condition.OnPropertyCondition
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:467)
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:283)
        at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:323)
        ... 35 more

Sample

I was calling other services through openFeign in a synchronous parallel task, and this exception occurred on some of the requests.

This happens from time a time, not in regular way.

Thank you for your help.

SirZiWang commented 5 months ago

The solution is to manually initialize the load balancer client, such as after an application ready event occurs

@Configuration
public class LoadBalanceConfig {
    @Bean
    @ConditionalOnMissingBean
    public LoadBalancerClientFactory loadBalancerClientFactory(LoadBalancerClientsProperties properties) {
        return new LoadBalancerClientFactory(properties) {
            @Override
            public AnnotationConfigApplicationContext createContext(String name) {
                ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
                AnnotationConfigApplicationContext context = (AnnotationConfigApplicationContext) super.createContext(name);
                Thread.currentThread().setContextClassLoader(originalClassLoader);
                return context;
            }
        };
    }
}
OlgaMaciaszek commented 5 months ago

Hello @SirZiWang , thanks for reporting the issue. This looks like a duplicate of https://github.com/spring-cloud/spring-cloud-openfeign/issues/475. The versions of Spring libraries that you're using are no longer in OSS support. Please upgrade to Spring Cloud 2023.0.1 and Boot 3.2.2, but manually set Spring Cloud Commons version to 4.1.1 to avoid a different known regression, and verify. The issue should not be there. If you're still facing the issue after the upgrade, please get back to us.

spring-cloud-issues commented 5 months ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues commented 5 months ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.