0

与和spring-cloud-feign结合使用时遇到了一个奇怪的问题。创建了度量节点,但没有数据进入。HystrixCodaHaleMetricsPublisherGraphite

我的配置:

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

1 回答 1

1

最后我找到了问题的解决方案。问题是,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));
    };
}

现在一切正常。

于 2017-09-19T06:35:42.807 回答