访问 URI 模板或请求线值假装请求接收器/请求模板

我正在针对具有硬 API 速率限制的云应用程序开发一个应用程序。为了让我的团队了解我们在这些限制方面的接近程度,我想以有意义的方式计算从我们的应用程序进行的所有API调用。

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

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

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

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

等等。

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

有没有办法做到这一点?


aluckdog
浏览 72回答 3
3回答

交互式爱情

也许你可以尝试使用自定义假装调用处理程序工厂。我已经设法使用如下代码记录请求感知器:更改启用特征客户端并添加默认配置@EnableFeignClients(defaultConfiguration = FeignConfig.class)添加默认的假装配置@Configurationpublic class FeignConfig {@Bean@ConditionalOnMissingBeanpublic Retryer feignRetryer() {&nbsp; &nbsp; return Retryer.NEVER_RETRY;}@Bean@Scope("prototype")@ConditionalOnMissingBeanpublic Feign.Builder feignBuilder(Retryer retryer) {&nbsp; &nbsp; return Feign.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .retryer(retryer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .invocationHandlerFactory((target, dispatch) -> new CountingFeignInvocationHandler(target, dispatch));}}创建调用处理程序(基于假装的代码。反光费恩.假装调用手)public class CountingFeignInvocationHandler implements InvocationHandler {&nbsp; &nbsp; private final Target target;&nbsp; &nbsp; private final Map<Method, MethodHandler> dispatch;&nbsp; &nbsp; public CountingFeignInvocationHandler(Target target, Map<Method, MethodHandler> dispatch) {&nbsp; &nbsp; &nbsp; &nbsp; this.target = checkNotNull(target, "target");&nbsp; &nbsp; &nbsp; &nbsp; this.dispatch = checkNotNull(dispatch, "dispatch for %s", target);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {&nbsp; &nbsp; &nbsp; &nbsp; if ("equals".equals(method.getName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object otherHandler =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return equals(otherHandler);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalArgumentException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if ("hashCode".equals(method.getName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return hashCode();&nbsp; &nbsp; &nbsp; &nbsp; } else if ("toString".equals(method.getName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return toString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; RequestLine requestLine = method.getAnnotation(RequestLine.class);&nbsp; &nbsp; &nbsp; &nbsp; addStatistics(requestLine.value());&nbsp; &nbsp; &nbsp; &nbsp; return dispatch.get(method).invoke(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object obj) {&nbsp; &nbsp; &nbsp; &nbsp; if (obj instanceof CountingFeignInvocationHandler) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CountingFeignInvocationHandler other = (CountingFeignInvocationHandler) obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return target.equals(other.target);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; return target.hashCode();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return target.toString();&nbsp; &nbsp; }}要小心,检查你是否假装配置不是更复杂,在这种情况下,根据需要扩展类。

ITMISS

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

烙印99

也许你可以尝试覆盖假装记录器。假设我们有一个假装的客户,@FeignClient(name = "demo-client", url = "http://localhost:8080/api", configuration = FeignConfig.class)public interface DemoClient {&nbsp; &nbsp; @GetMapping(value = "/test/{id}")&nbsp; &nbsp; void test(@PathVariable(name = "id") Integer id) {&nbsp; &nbsp; }}import feign.Logger;import feign.Request;import feign.Response;import java.io.IOException;public class CustomFeignRequestLogging extends Logger {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void logRequest(String configKey, Level logLevel, Request request) {&nbsp; &nbsp; &nbsp; &nbsp; super.logRequest(configKey, logLevel, request);&nbsp; &nbsp; &nbsp; &nbsp; // targetUrl = http://localhost:8080/api&nbsp; &nbsp; &nbsp; &nbsp; String targetUrl = request.requestTemplate().feignTarget().url();&nbsp; &nbsp; &nbsp; &nbsp; // path = /test/{id}&nbsp; &nbsp; &nbsp; &nbsp; String path = request.requestTemplate().methodMetadata().template().path();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java