离墨狂客Z 2023-09-03 15:38 采纳率: 100%
浏览 2
已结题

SpringCloud项目启动报错,NoUniqueBeanDefinitionException

springboot版本:2.2.4.RELEASE
springcloud版本:Hoxton.SR9

启动springcloud项目报错:

Field feignService in com.example.client.web.WebController required a single bean, but 2 were found:
    - loadBalancedRetryPolicyFactory: defined by method 'loadBalancedRetryPolicyFactory' in class path resource [org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.class]
    - loadBalancedRetryFactory: defined by method 'loadBalancedRetryFactory' in class path resource [org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfiguration$BlockingLoadbalancerClientConfig$BlockingLoadBalancerRetryConfig.class]

详细信息

2023-09-03 15:29:52.946  WARN [client,,,] 32552 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webController': Unsatisfied dependency expressed through field 'feignService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.client.service.FeignService': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory' available: expected single matching bean but found 2: loadBalancedRetryPolicyFactory,loadBalancedRetryFactory
2023-09-03 15:29:52.948  INFO [client,,,] 32552 --- [  restartedMain] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2023-09-03 15:29:52.955  INFO [client,,,] 32552 --- [  restartedMain] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
2023-09-03 15:29:53.962  WARN [client,,,] 32552 --- [  restartedMain] z.r.AsyncReporter$BoundedAsyncReporter   : Timed out waiting for in-flight spans to send
2023-09-03 15:29:53.964  INFO [client,,,] 32552 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-09-03 15:29:53.968  WARN [client,,,] 32552 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [client] appears to have started a thread named [RxIoScheduler-1 (Evictor)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
2023-09-03 15:29:53.968  WARN [client,,,] 32552 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [client] appears to have started a thread named [spring.cloud.inetutils] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.net.Inet6AddressImpl.getHostByAddr(Native Method)
 java.net.InetAddress$2.getHostByAddr(InetAddress.java:932)
 java.net.InetAddress.getHostFromNameService(InetAddress.java:617)
 java.net.InetAddress.getHostName(InetAddress.java:559)
 java.net.InetAddress.getHostName(InetAddress.java:531)
 org.springframework.cloud.commons.util.InetUtils$$Lambda$109/666823162.call(Unknown Source)
 java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
 java.util.concurrent.FutureTask.run(FutureTask.java)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
2023-09-03 15:29:53.984  INFO [client,,,] 32552 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-09-03 15:29:53.993 ERROR [client,,,] 32552 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field feignService in com.example.client.web.WebController required a single bean, but 2 were found:
    - loadBalancedRetryPolicyFactory: defined by method 'loadBalancedRetryPolicyFactory' in class path resource [org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.class]
    - loadBalancedRetryFactory: defined by method 'loadBalancedRetryFactory' in class path resource [org/springframework/cloud/loadbalancer/config/BlockingLoadBalancerClientAutoConfiguration$BlockingLoadbalancerClientConfig$BlockingLoadBalancerRetryConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Disconnected from the target VM, address: '127.0.0.1:48477', transport: 'socket'

Process finished with exit code 0

相关的文件

FeignService

@FeignClient(value = "client-2")
public interface FeignService {

    @GetMapping("/client/callClient")
    String callClient();
}

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class);
    }

}

pom文件依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>4.0.2</version>
        </dependency>
        <!-- 客户端包 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-client</artifactId>
            <version>${spring.boot.admin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

  • 写回答

2条回答 默认 最新

  • 离墨狂客Z 2023-09-03 17:26
    关注

    最后还是自己解决的,泪目了

    No qualifying bean of type 'org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory' available: expected single matching bean but found 2: loadBalancedRetryPolicyFactory,loadBalancedRetryFactory
    

    出错原因是创建FeignService的过程中,因为用到了ribbon负载均衡,在LoadBalancedRetryFactory实例化的时候发现了两个相同匹配的bean,没有指定某一个bean,所以报了错。

    解决方案:
    在FeignService上加上qualifier = "loadBalancedRetryFactory"(我是随便加的,也不清楚换另一个有什么区别)

    
    @FeignClient(value = "client-2",qualifier = "loadBalancedRetryFactory")
    public interface FeignService {
    
        @GetMapping("/client/callClient")
        String callClient();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 9月3日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大