英文:
Issue when using openfeign client called within an asynchronous method
问题
我在从Java 8迁移到Java 17和Spring 3后,使用Feign异步调用服务时遇到了此错误。
{
"code": "SI-SERVICE-ERROR-000500",
"message": "org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration]
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)
}
我的Feign和主类如下:
@Service
@FeignClient(name = "si-booking-service", path = "/si-booking-service/bookings")
public interface SIBookingServiceClient {...}
@EnableFeignClients
@EnableDiscoveryClient
public class MainApplication {...}
英文:
I encounter this error when calling a service asynchronously using Feign after a migration from Java 8 to Java 17 and to spring 3
{
"code": "SI-SERVICE-ERROR-000500",
"message": "org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration]
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)
}
My Feign and Main class :
@Service
@FeignClient(name = "si-booking-service", path = "/si-booking-service/bookings")
public interface SIBookingServiceClient {...}
@EnableFeignClients
@EnableDiscoveryClient
public class MainApplication {..}
答案1
得分: 0
这个问题是Spring Cloud Feign的一个已知Bug:
https://github.com/spring-cloud/spring-cloud-openfeign/issues/475
这些是我的项目依赖的版本号:
- Spring Boot 3.1.0
- Spring Cloud 2022.0.3
- Java 17
这个解决方法对我有效(我在我的WebclientConfiguration类中添加了这个方法):
@Configuration
@Slf4j
public class WebclientConfiguration {
...
@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;
}
};
}
}
英文:
This issue is an open bug in the spring cloud Feign :
https://github.com/spring-cloud/spring-cloud-openfeign/issues/475
These are the version numbers of my project dependencies :
Spring boot 3.1.0
spring-cloud 2022.0.3
Java 17
This workaround works for me (I added this method in my WebclientConfiguration class) :
@Configuration
@Slf4j
public class WebclientConfiguration {
...
@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;
}
};
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论