ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

记录整合Spring Cloud,OpenFeign没有自动注入IClientConfig的问题

2022-03-02 11:03:14  阅读:345  来源: 互联网

标签:return OpenFeign Spring nacos Bean IClientConfig config class


1.版本:

    <!-- nacos --> 
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
springcloud:Hoxton.RELEASE
springboot:2.2.7.RELEASE

`Error creating bean with name 'ribbonServerList' defined in class path resource,错误如图

2.解决方法:

配置文件加入配置 ribbon.nacos.enabled=false

3.原因:

//nacos默认使用NacosRibbonClientConfiguration,加载ribbonServerList时没有发现IClientConfig,所以报错
//通过ConditionalOnRibbonNacos注解,发现设置ribbon.nacos.enabled=false,会取消加载NacosRibbonClientConfiguration
//取消后会使用RibbonClientConfiguration,而RibbonClientConfiguration已经配置了所有基本属性

4.解决步骤,可以不看

尝试手动注入,但调用Feign时报空指针异常;

  @Configuration
  class IClientConfigComponent {
      @Bean
      public IClientConfig iClientConfig() {
          return new DefaultClientConfigImpl();
      }
  }

查看源码发现时因为许多基本属性没有设置,从OpenFeign注入的Bean赋值基本属性

@Configuration
class IClientConfigComponent {
  @Bean
  public IClientConfig iClientConfig() {
      DefaultClientConfigImpl config = new DefaultClientConfigImpl();
      config.loadProperties("client");
      config.set(CommonClientConfigKey.ConnectTimeout, 1000);
      config.set(CommonClientConfigKey.ReadTimeout, 1000);
      config.set(CommonClientConfigKey.GZipPayload, true);
      return config;
  }
}

重新启动,报Load balancer does not have available server for client: client,没有配置负载均衡。加上:

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        if (this.propertiesFactory.isSet(IRule.class, name)) {
            return this.propertiesFactory.get(IRule.class, config, name);
        }
        ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
        rule.initWithNiwsConfig(config);
        return rule;
    }

还是不行,负载均衡找不到对应服务,点击报错发现nacos默认使用的是NacosRibbonClientConfiguration

设置ribbon.nacos.enabled=false,重启,成功;

5总结

问题虽然解决了,但可能还存在潜在问题,等系统学习了Springcloud负载均衡再自定义配置.....

标签:return,OpenFeign,Spring,nacos,Bean,IClientConfig,config,class
来源: https://www.cnblogs.com/ziye-1/p/15952024.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有