今天调用阿里接口,想记录下日志。找了一会发现可以自定义Custom HttpClient ,下面看下相关代码。
HttpClientFactory源码
httpClient是通过HttpClientFactory使用profile配置生产的
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 所以我们在IClientProfile profile 中设置
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);
}
}
}
我们在启动的时候 注入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();
}
}
在静态代理中实现切面操作 记录日志等 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);
// 因为 CompatibleUrlConnClient 没有无参构造器 使用一个 ApplicationContextAware 工具类 获取 spring中的bean
// FaceAuthLogMapper faceAuthLogMapper = ApplicationContextHolder.getBean(FaceAuthLogMapper.class);
return httpResponse;
}
}
在service中 注入 使用
// ...
@Autowired
private IAcsClient client;
// ... 具体调用
GetVerifyTokenResponse response = client.getAcsResponse(getVerifyTokenRequest);
网友评论