问题
在使用openfeign进行远程调用时,出现了异常:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-10 18:26:05.338 ERROR 10788 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'growthChangeHistoryController': Unsatisfied dependency expressed through field 'couponService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hdl.mall.member.feign.CouponService': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer
原因
SpringCloud openFeign在Hoxton.M2 RELEASED版本之后不再使用ribbon
而是使用spring-cloud-loadbalancer,所以在不引入spring-cloud-loadbalancer情况下会报错
解决
在pom.xml中引入spring-cloud-loadbalancer依赖即可
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> <version>2.2.1.RELEASE</version> </dependency>
随后又出现异常:
java.lang.AbstractMethodError: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance;
原因及解决
应该是nacos中的ribbon对spring-cloud-loadbalancer造成了影响,所以移除nacos中的ribbon即可
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency>