今天調(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);