aliyuncs/sdk中自定義CustomHttpClient靜態(tài)代理CompatibleUrlConnClient記錄日志

今天調(diào)用阿里接口,想記錄下日志。找了一會(huì)發(fā)現(xiàn)可以自定義Custom HttpClient ,下面看下相關(guān)代碼。

HttpClientFactory源碼

httpClient是通過(guò)HttpClientFactory使用profile配置生產(chǎn)的

public class HttpClientFactory {

    public static String HTTP_CLIENT_IMPL_KEY = "aliyuncs.sdk.httpclient";
    public static String COMPATIBLE_HTTP_CLIENT_CLASS_NAME = CompatibleUrlConnClient.class.getName();

    public static IHttpClient buildClient(IClientProfile profile) {
        try {
            HttpClientConfig clientConfig = profile.getHttpClientConfig();
            if (clientConfig == null) {
                clientConfig = HttpClientConfig.getDefault();
                profile.setHttpClientConfig(clientConfig);
            }

            String customClientClassName = null;
            if (clientConfig.isCompatibleMode()) {
                customClientClassName = COMPATIBLE_HTTP_CLIENT_CLASS_NAME;
            } else if (clientConfig.getClientType() == HttpClientType.Custom && StringUtils.isNotEmpty(clientConfig.getCustomClientClassName())) {
                // 這里是判斷使用自定義 CustomHttpClientType 所以我們?cè)贗ClientProfile profile 中設(shè)置
                customClientClassName = clientConfig.getCustomClientClassName();
            } else {
                customClientClassName = System.getProperty(HTTP_CLIENT_IMPL_KEY);
            }
            if (StringUtils.isEmpty(customClientClassName)) {
                customClientClassName = clientConfig.getClientType().getImplClass().getName();
            }
            Class httpClientClass = Class.forName(customClientClassName);
            if (!IHttpClient.class.isAssignableFrom(httpClientClass)) {
                throw new IllegalStateException(String.format("%s is not assignable from com.aliyuncs.http.IHttpClient", customClientClassName));
            }
            Constructor<? extends IHttpClient> constructor = httpClientClass.getConstructor(HttpClientConfig.class);
            return constructor.newInstance(clientConfig);
        } catch (Exception e) {
            // keep compatibility
            throw new IllegalStateException("HttpClientFactory buildClient failed", e);
        }
    }

}

我們?cè)趩?dòng)的時(shí)候 注入bean AliFaceAuthConfig.class

@Configuration
@ConfigurationProperties("com.aliyuncs.profile")
@Data
public class AliFaceAuthConfig {

    private String regionId;

    private String accessKeyId;

    private String secret;

    // 配置自定義httpClient  返回 IAcsClient 
    @Bean
    public IAcsClient getIAcsClient(){
        DefaultProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKeyId,
                secret);
        HttpClientConfig httpClientConfig = HttpClientConfig.getDefault();
        httpClientConfig.setClientType(HttpClientType.Custom);
        httpClientConfig.setCustomClientClassName("CustomerCompatibleUrlConnClient");
        profile.setHttpClientConfig(httpClientConfig);
        return new DefaultAcsClient(profile);
    }

    @Bean
    public CloseableHttpClient getCloseableHttpClient() {
        HttpRequestInterceptor requestInterceptor = (request, context) -> {
            //Method implementation . . . . .
            System.err.println("aaaaaads!!!!!!!");
        };
        return HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
    }

}

在靜態(tài)代理中實(shí)現(xiàn)切面操作 記錄日志等 CustomerCompatibleUrlConnClient.class


public class CustomerCompatibleUrlConnClient extends CompatibleUrlConnClient {

    public CustomerCompatibleUrlConnClient(HttpClientConfig clientConfig) throws ClientException {
        super(clientConfig);
    }

    @Override
    public HttpResponse syncInvoke(HttpRequest request) throws IOException {
        //記錄日志
        System.out.println("aaaaaaa");
        HttpResponse httpResponse = super.syncInvoke(request);
       // 因?yàn)?CompatibleUrlConnClient 沒(méi)有無(wú)參構(gòu)造器 使用一個(gè) ApplicationContextAware 工具類(lèi) 獲取 spring中的bean
       //  FaceAuthLogMapper faceAuthLogMapper = ApplicationContextHolder.getBean(FaceAuthLogMapper.class);
        return httpResponse;
    }

}

在service中 注入 使用


    // ...
    @Autowired
    private IAcsClient client;

    // ...  具體調(diào)用
   GetVerifyTokenResponse response = client.getAcsResponse(getVerifyTokenRequest);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容