使用异步方法调用的OpenFeign客户端时出现问题。

huangapple go评论56阅读模式
英文:

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;
      }
    };
  }
}

huangapple
  • 本文由 发表于 2023年6月22日 18:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定