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

相关文章

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然