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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值