在Feign RequestInterceptor / RequestTemplate中访问URITemplate或RequestLine值

时间:2019-04-05 08:51:47

标签: java spring-boot netflix-feign

我正在针对具有严格api速率限制的云应用程序开发一个应用程序。为了让我的团队了解我们在这些限制方面的差距,我想以有意义的方式计算从我们的应用程序发出的所有API调用。

我们使用Feign作为访问层,我希望能够使用RequestInterceptor来计算我们称为的不同API端点:

RequestInterceptor ri = rq -> addStatistics(rq.url());

现在这行不通,因为生成的URL之后几乎总是计数为“ 1”,因为它们已经包含了所有解析的路径变量,所以我得到了计数

1 - /something/id1valueverycryptic/get
1 - /something/anothercrypticidkey/get

以此类推。

我希望能以某种方式访问​​@ResuqestLine映射值(GET /something/{id}/get)或至少是uri模板预解析(/somethine/{id}/get

有没有办法做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:3)

也许您可以尝试使用自定义的伪装InvocationHandlerFactory。

我已经设法使用以下代码记录RequestInterceptor:

  • 更改EnableFeignClients并添加defaultConfiguration

    @EnableFeignClients(defaultConfiguration = FeignConfig.class)
    
  • 添加默认的伪装配置

    @Configuration
    public class FeignConfig {
    
    @Bean
    @ConditionalOnMissingBean
    public Retryer feignRetryer() {
        return Retryer.NEVER_RETRY;
    }
    
    @Bean
    @Scope("prototype")
    @ConditionalOnMissingBean
    public Feign.Builder feignBuilder(Retryer retryer) {
        return Feign.builder()
                .retryer(retryer)
                .invocationHandlerFactory((target, dispatch) -> new CountingFeignInvocationHandler(target, dispatch));
    }
    
    }
    
  • 创建您的调用处理程序(基于feign.ReflectiveFeign.FeignInvocationHandler的代码)

    public class CountingFeignInvocationHandler implements InvocationHandler {
    
        private final Target target;
        private final Map<Method, MethodHandler> dispatch;
    
        public CountingFeignInvocationHandler(Target target, Map<Method, MethodHandler> dispatch) {
            this.target = checkNotNull(target, "target");
            this.dispatch = checkNotNull(dispatch, "dispatch for %s", target);
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("equals".equals(method.getName())) {
                try {
                    Object otherHandler =
                            args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;
                    return equals(otherHandler);
                } catch (IllegalArgumentException e) {
                    return false;
                }
            } else if ("hashCode".equals(method.getName())) {
                return hashCode();
            } else if ("toString".equals(method.getName())) {
                return toString();
            }
    
            RequestLine requestLine = method.getAnnotation(RequestLine.class);
            addStatistics(requestLine.value());
    
            return dispatch.get(method).invoke(args);
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof CountingFeignInvocationHandler) {
                CountingFeignInvocationHandler other = (CountingFeignInvocationHandler) obj;
                return target.equals(other.target);
            }
            return false;
        }
    
        @Override
        public int hashCode() {
            return target.hashCode();
        }
    
        @Override
        public String toString() {
            return target.toString();
        }
    }
    

请小心,并检查假装配置是否更为复杂,在这种情况下,可以根据需要扩展类。

答案 1 :(得分:1)

    If you are using spring-cloud-starter-openfeign ,  You could do something like below 
    
    add the a primary contract bean 
    @Bean("YourContract")
    @Primary
        public Contract springpringContract() {
            return (targetType) -> {
    
                List<MethodMetadata> parseAndValidatateMetadata = new SpringMvcContract().parseAndValidatateMetadata(targetType);
                parseAndValidatateMetadata.forEach(metadata -> {
                    RequestTemplate template = metadata.template();
                    template.header("unresolved_uri", template.path().replace("{", "[").replace("}", "]"));
    
                });
                return parseAndValidatateMetadata;
            };
        }
    
    Add the contract to the feign client builder 
    @Bean
     public <T> T feignBuilder(Class<T> feignInterface, String targetURL) {
            return Feign.builder().client(getClient())
                    .contract(contract)
                    .
                    .
    }
    
    Once you are done with the above you should be able to access the unresolved path in the RequestTemplate

@component
public class FeignRequestFilter  implements RequestInterceptor {
    @Override
        public void apply(RequestTemplate template) {
            String unresolvedUri = template.headers().getOrDefault("unresolved_uri", Collections.singleton(template.path()))
                    .iterator().next();
    }
}