代码之家  ›  专栏  ›  技术社区  ›  Roman T

HystrixCodaHaleMetricsPublisher不能与spring cloud Faign hystrix配合使用

  •  0
  • Roman T  · 技术社区  · 7 年前

    spring-cloud-feign 结合 HystrixCodaHaleMetricsPublisher Graphite . 已创建度量节点,但未输入任何数据。

    @Configuration
    @RequiredArgsConstructor
    @EnableConfigurationProperties(ApiGatewayProperties.class)
    @EnableFeignClients
    public class AccountSettingsClientConfig {
        private final ApiGatewayProperties apiGatewayProperties;
    
        @Bean
        public RequestInterceptor oauth2FeignRequestInterceptor() {
            return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), resource());
        }
    
        @Bean
        public okhttp3.OkHttpClient okHttpClient() {
            return new OkHttpClient.Builder().hostnameVerifier((s, sslSession) -> true)
                    .build();
        }
    
        @Bean
        public AccountSettingsClientFallbackFactory accountSettingsClientFallbackFactory() {
            return new AccountSettingsClientFallbackFactory();
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Roman T    7 年前

    最后我找到了问题的解决方案。问题是,FeignHistrix的默认SetterFactory生成了带有无效字符(对于graphite)的commandKey,即。 development.local.AccountSettingsClient.AccountSettingsClient#accountSettings(String).countBadRequests . 在这种情况下,无效字符是#和()。当GraphiteReport开始向Graphite发送数据时,一切都正常,数据也会被发送,但Graphite无法处理。因此没有数据被持久化。

    作为解决方法,我注册了一个定制SetterFactory:

     @Bean
    public SetterFactory setterFactoryThatGeneratesGraphiteConformCommandKey() {
        return (target, method) -> {
            String groupKey = target.name();
            //allowed chars for graphite are a-z, A-Z, 0-9, "-", "_", "." and "/".
            //We don't use default SetterFactory.Default because it generates command key with parenthesis () and #
            String commandKey = target.type().getSimpleName() + "-" + method.getName();
            return HystrixCommand.Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
        };
    }
    

    现在一切都好了。