Spring Cloud Ribbon負載均衡配置類放在Spring boot主類同級增長Exclude過濾後報Field config in com.cloud.web.controller.Rib

環境:java

  Spring Cloud:Finchley.M8web

  Spring Boot:2.0.0.RELEASE算法

 

目錄結構:spring

  

能夠看到代碼第13行的註釋,我已經在@ComponentScan註解中添加了Exclude配置項,可是啓動服務的時候仍是報以下的錯誤:apache

  

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註解,因此仍是去掃了:app

  

問題很簡單,把註解加上就行了。。dom

 

  貼一下幾個類的代碼:ide

  一、ExcludeFromComponetScanui

  

public @interface ExcludeFromComponetScan {
}

  二、spring cloud啓動加載類:this

  

@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);
	}
}

  三、RibbonConfiguration

  

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

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        //隨機算法
        return new RandomRule();
    }
}
相關文章
相關標籤/搜索