【微服务】两种方式解决No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter

在使用nacos集成openfeign时,启动项目会报错,这是因为SpringCloud Feign在Hoxton.M2 RELEASE版本之后不再使用ribbon,而是使用spring-cloud-loadbalancer,所以在不引入spring-cloud-loadbalancer情况下会报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'feign'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'priv.happy.nacosclient01.feign.FoodFeign': 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?
	……
	at priv.happy.nacosclient01.NacosClient01Application.main(NacosClient01Application.java:14) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'priv.happy.nacosclient01.feign.FoodFeign': 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?
	... 20 common frames omitted
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
	... 28 common frames omitted

所以需要添加loadbalancer依赖,但是loadbalancer和ribbon存在依赖冲突,解决方法有两种

  • 第一中,通过修改依赖解决,即排除nacos中的ribbon依赖

    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
    




    org.springframework.cloud
    spring-cloud-starter-loadbalancer
  • 第二种,通过修改application.properties中的配置解决【官方的方法】Nacos官方文档

    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery




    org.springframework.cloud
    spring-cloud-starter-loadbalancer
spring.cloud.loadbalancer.ribbon.enabled=false
spring.cloud.loadbalancer.nacos.enabled=true

你可能感兴趣的:(微服务,微服务,云原生,服务发现)