亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問 URI 模板或請求線值假裝請求接收器/請求模板

訪問 URI 模板或請求線值假裝請求接收器/請求模板

aluckdog 2022-09-22 13:54:59
我正在針對具有硬 API 速率限制的云應用程序開發一個應用程序。為了讓我的團隊了解我們在這些限制方面的接近程度,我想以有意義的方式計算從我們的應用程序進行的所有API調用。我們使用 Feign 作為訪問層,我希望能夠使用 來計算我們調用的不同 API 端點:RequestInterceptorRequestInterceptor ri = rq -> addStatistics(rq.url());現在這不起作用,因為生成的URL幾乎總是在之后計數“1”,因為它們已經包含所有解析的路徑變量,所以我得到計數1 - /something/id1valueverycryptic/get 1 - /something/anothercrypticidkey/get等等。我希望以某種方式訪問映射值()或至少uri模板預解析(@ResuqestLineGET /something/{id}/get/somethine/{id}/get)有沒有辦法做到這一點?
查看完整描述

3 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

也許你可以嘗試使用自定義假裝調用處理程序工廠。


我已經設法使用如下代碼記錄請求感知器:


更改啟用特征客戶端并添加默認配置


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

}


}

創建調用處理程序(基于假裝的代碼。反光費恩.假裝調用手)


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

    }

}

要小心,檢查你是否假裝配置不是更復雜,在這種情況下,根據需要擴展類。


查看完整回答
反對 回復 2022-09-22
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

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

    }

}


查看完整回答
反對 回復 2022-09-22
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

也許你可以嘗試覆蓋假裝記錄器。


假設我們有一個假裝的客戶,


@FeignClient(name = "demo-client", url = "http://localhost:8080/api", configuration = FeignConfig.class)

public interface DemoClient {


    @GetMapping(value = "/test/{id}")

    void test(@PathVariable(name = "id") Integer id) {

    }

}



import feign.Logger;

import feign.Request;

import feign.Response;


import java.io.IOException;


public class CustomFeignRequestLogging extends Logger {

    @Override

    protected void logRequest(String configKey, Level logLevel, Request request) {

        super.logRequest(configKey, logLevel, request);

        // targetUrl = http://localhost:8080/api

        String targetUrl = request.requestTemplate().feignTarget().url();

        // path = /test/{id}

        String path = request.requestTemplate().methodMetadata().template().path();

    }

}


查看完整回答
反對 回復 2022-09-22
  • 3 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號