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

  

public @interface ExcludeFromComponetScan {
}

  2、spring cloud啟動加載類:

  

@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

  

//這個類不能喝Spring Boot @ConponentScan所在主類放在同一個包或其子包下,否則需要些Exclude類做區分
@ExcludeFromComponetScan
@Configuration
public class RibbonConfiguration {
    @Autowired
    IClientConfig config;

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        //隨機算法
        return new RandomRule();
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM