HystrixCodaHaleMetricsPublisher 不能与 spring-cloud-feign hystrix 结合使用

HystrixCodaHaleMetricsPublisher doesn't work in conjunction with spring-cloud-feign hystrix

我在结合使用 spring-cloud-feignHystrixCodaHaleMetricsPublisherGraphite 时遇到了一个奇怪的问题。指标节点已创建,但没有数据进入。

我的配置:

@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();
    }

我终于找到了问题的解决方案。问题是,FeignHistrix 的默认 SetterFactory 生成带有无效(对于石墨)字符的 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));
    };
}

现在一切正常。