首页 > 其他分享 >SpringBoot在@PostConstruct方法中调用FeignClient加载不到Hystrix配置问题

SpringBoot在@PostConstruct方法中调用FeignClient加载不到Hystrix配置问题

时间:2022-08-30 17:44:05浏览次数:84  
标签:FeignClient SpringBoot Hystrix String key public 加载

版本信息

  • spring-cloud-starter-openfeign:2.1.3.RELEASE
  • spring-boot-starter-parent:2.2.2.RELEASE

问题描述

  • @Service@Autowired了一个@FeignClient,在@PostConstruct方法中调用此@FeignClient的方法,会直接进入Fallback,怀疑配置的Hystrix超时timeoutInMilliseconds配置没有生效

配置&代码

  • application. yml

    hystrix:
      threadpool:
        default:
          coreSize: 30
          maximumSize: 100
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 9000
    feign:
      hystrix:
        enabled: true
      httpclient:
        enabled: true
      client:
        config:
          default:
            connectTimeout: 3000
            readTimeout: 5000
    
  • @FeignClient

    @FeignClient(name = "dictionaryService", path = "internal/metrics", url = "${host}", decode404 = true, configuration = {ResponseDecoder.class}, fallback = DictionaryServiceFallback.class)
    public interface DictionaryService {
    
        @PostMapping("dictionary/getValueByPathKey")
        String getValueByPathKey(@RequestParam("path") String path, @RequestParam("key") String key);
    }
    
  • @Service

    @Slf4j
    @Service
    public class RsaServiceImpl implements RsaEncService {
    
        @Autowired
        private DictionaryService dictionaryService;
    
        private static String PUBLIC_KEY = null;
    
        private static String PRIVATE_KEY = null;
    
        private static final String XAPI_TOKEN = "xapi.token";
    
        @PostConstruct
        public void initKey() {
            PUBLIC_KEY = getPublicKey();
            PRIVATE_KEY = getPrivateKey();
        }
    
        @Override
        public String getPublicKey() {
            String value = dictionaryService.getValueByPathKey(XAPI_TOKEN, "public_key");
            if (StringUtils.isBlank(value)) {
                log.error("PublicKey获取失败");
            }
            return value;
        }
    
        @Override
        public String getPrivateKey() {
            String value = dictionaryService.getValueByPathKey(XAPI_TOKEN, "private_key");
            if (StringUtils.isBlank(value)) {
                log.error("PrivateKey获取失败");
            }
            return value;
        }
    
  • FallBack

    @Slf4j
    @Component
    public class DictionaryServiceFallback implements DictionaryService {
    
        @Override
        public String getValueByPathKey(String path, String key) {
            log.error("获取字典失败 , path : {}, key : {}", path, key);
            return null;
        }
    }
    

原因分析

  • 怀疑是在@Service的Bean创建后,@PostConstruct方法调用时,还没有加载Hystrix的配置,所以Hystrix使用的默认配置(默认timeoutInMilliseconds为1s)
  • debugHystrixCommandProperties,发现没有加载到Hystrix的配置。而实际Hystrix配置应该通过ArchaiusAutoConfiguration中的Bean:ConfigurableEnvironmentConfiguration负责加载
  • @FeignClient是通过@EnableFeignClients注解扫描组装Bean得到的,HystrixCommand是在使用@FeignClient时才创建,通过feign.hystrix.HystrixInvocationHandler#invoke方法
  • 在com.netflix.config.DynamicProperty#DynamicProperty(java.lang.String)构造函数中从ConcurrentCompositeConfiguration对象获取配置

解决

  • 使用@DependsOn("configurableEnvironmentConfiguration")注解

    • 单独控制可以加在@Service注解上

      @DependsOn("configurableEnvironmentConfiguration")
      @Slf4j
      @Service
      public class RsaServiceImpl implements RsaEncService {
          ......
      }
      
    • 全局控制加在Application启动类上

      @DependsOn("configurableEnvironmentConfiguration")
      @SpringBootApplication
      public class DevApplication{
          ......
      }
      

总结

  • HystrixCommand是在首次调用@FeignClient才初始化,所以需要确定Hystrix的配置此时是否已经加载到
  • Hystrix的配置默认是通过ArchaiusAutoConfiguration中的Bean--ConfigurableEnvironmentConfiguration负责加载
  • 确保在使用@FeignClientConfigurableEnvironmentConfiguration已经被加载

参考

标签:FeignClient,SpringBoot,Hystrix,String,key,public,加载
From: https://www.cnblogs.com/wftop1/p/16640235.html

相关文章

  • SpringBootDependencyInjectionTestExecutionListener
    ============================CONDITIONSEVALUATIONREPORT============================Positivematches:-----------------AopAutoConfigurationmatched:......
  • Java SPI与SpringBoot 自动配置
    JavaSPI设计思想1、使用约定的配置文件2、谁提供jar包,也要负责提供配置文件3、使用ClassLoader的getResource和getResources方法,来读取classpath中的配置文件 Sprin......
  • 如何在springBoot中进行ReactiveFeignClient超时配置
    最近项目中用到了ReactiveFeign请求第三方的http接口,需要自定义一个请求超时时间,但在网上查了很多资料都没有一个比较准确的配置方法。pom依赖<dependency><groupId>......
  • springboot加载静态资源
      第一步写一个config的类继承,WebMvcConfigurationSupport   重写这个方法/**是指这个后面的路径都可以加载另一个配置类加载web:resources:static-lo......
  • 9.Hystrix快速失败、降级及熔断机制
    降级介绍降级是一种"无奈"的选择,就是俗称的备胎Command降级需要fallback方法ObservableCommand降级实现resumeWithFallback方法 降级触发原则HystrixBadRequestE......
  • 8.Hystrix隔离术
    Hystrix隔离之ThreadPoolKeyHystrix可以不填写ThreadPoolKey默认Hystrix会使用GroupKey命名线程池在Setting中加入andThreadPoolKey进行命名 Hystrix隔离介绍Hys......
  • SpringBoot整合EMQ
    1.引入依赖<dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId><version>1.2.5</version></depe......
  • springboot1
    1.5.5常用的监控终端在百度搜索“springbootactuator”即可找到如下表格。第2章SpringBoot重要用法2.1自定义异常页面对于404、405、500等异常状态,服务器......
  • SpringBoot+@Async
    1.使用@Async注解使用@Async注解就能简单的将原来的同步函数变为异步(注:@Async所修饰的函数不要定义为static类型,这样异步调用不会生效)@ComponentpublicclassTask{......
  • springboot定时同步数据,从sqlserver到mysql
    定时同步数据,从sqlserver到mysql 注意事项:一.primary:master #设置默认的数据源或者数据源组,默认值即为master二.@Scheduled()和 @DS("slave_1")注解 步骤:1.......