Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.Rib

本文主要是介绍Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.Rib,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b

环境:

  Spring Cloud:Finchley.M8

  Spring Boot:2.0.0.RELEASE

 

目录结构:

  

可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了Exclude配置项,但是启动服务的时候还是报如下的错误:

  

2018 - 04 - 12  15 : 59 : 37.815   WARN 17828  --- [           main] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method 'close'  failed on bean with name 'eurekaRegistration' : org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration' : Singleton bean creation not allowed while  singletons of this  factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
2018 - 04 - 12  15 : 59 : 37.825   INFO 17828  --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018 - 04 - 12  15 : 59 : 37.828   WARN 17828  --- [ost-startStop- 1 ] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] 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$ 2 .call(InetUtils.java: 162 )
  org.springframework.cloud.commons.util.InetUtils$ 2 .call(InetUtils.java: 159 )
  java.util.concurrent.FutureTask.run(FutureTask.java: 266 )
  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1149 )
  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 624 )
  java.lang.Thread.run(Thread.java: 748 )
2018 - 04 - 12  15 : 59 : 37.838   INFO 17828  --- [           main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug'  enabled.
2018 - 04 - 12  15 : 59 : 37.961  ERROR 17828  --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig'  that could not be found.
Action:
Consider defining a bean of type 'com.netflix.client.config.IClientConfig'  in your configuration.
Process finished with exit code 1

  日志关键点在倒数第二行,其实原因很简单ComponentScan不去扫单个Ribbon的配置(RibbonConfigureration)不应用于所有Ribbon客户端,那这个当个客户端去加载的时候就要让Component知道不去管理他,否则就回去扫一遍,看我的Ribbon配置类,55行,被我注释了,没有引用到Exclude注解,所以还是去扫了:

  

问题很简单,把注解加上就好了。。

 

  贴一下几个类的代码:

  1、ExcludeFromComponetScan

  

1
2
public  @interface  ExcludeFromComponetScan {
}

  2、spring cloud启动加载类:

  

1
2
3
4
5
6
7
8
9
10
11
12
@EnableAutoConfiguration
//excludeFilters这里的意思是,只要标有ExcludeFromComponetScan注解的类都不会去扫描
@ComponentScan (value =  "com.cloud" , excludeFilters = { @ComponentScan .Filter(type = FilterType.ANNOTATION, value=ExcludeFromComponetScan. class )})
@SpringBootApplication (exclude = {DataSourceAutoConfiguration. class })
@EnableEurekaClient
@RibbonClient (name =  "SPRING-CLOUD-WEB-PROVIDER" , configuration = RibbonConfiguration. class )
public  class  SpringCloudRibbonApplication {
     public  static  void  main(String[] args) {
         SpringApplication.run(SpringCloudRibbonApplication. class , args);
     }
}

  3、RibbonConfiguration

  

1
2
3
4
5
6
7
8
9
10
11
12
13
//这个类不能喝Spring Boot @ConponentScan所在主类放在同一个包或其子包下,否则需要些Exclude类做区分
@ExcludeFromComponetScan
@Configuration
public  class  RibbonConfiguration {
     @Autowired
     IClientConfig config;
     @Bean
     public  IRule ribbonRule(IClientConfig config) {
         //随机算法
         return  new  RandomRule();
     }
}
转自:https://www.cnblogs.com/dbaxyx/p/8808940.html

这篇关于Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.Rib的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/182850

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是