关于openfeign远程调用配置

 

问题

在使用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>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值