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

相关文章

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

maven私服配置全过程

《maven私服配置全过程》:本文主要介绍maven私服配置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录使用Nexus作为 公司maven私服maven 私服setttings配置maven项目 pom配置测试效果总结使用Nexus作为 公司maven私