The Java SDK Integration Guide describes how to import and configure the Java SDK, parse IP addresses, integrate with a network library, and verify the integration. This topic focuses on how to use the Alibaba Cloud OSS SDK on a Java client to access HTTPDNS.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a versatile cloud storage service that allows developers to easily store and access files, such as profile pictures, images, audio, and video, within various applications. To use this service on a Java client, you can integrate the OSS Java SDK.
To improve the stability and speed of file transfers, you can combine the HTTPDNS SDK with the OSS Java SDK to parse OSS domain names. This method provides effective anti-hijacking and accelerated DNS parsing, which optimizes the network experience for applications that access OSS.
2. Prerequisites
Java 8 or later
OSS Java SDK V2 (alibabacloud-oss-v2) 0.1.0-beta1 or later
HTTPDNS Java SDK (alicloud-java-httpdns) 1.0.0 or later
3. Implement DnsResolver based on Apache HttpClient
The OSS Java SDK V2 uses Apache HttpClient as its underlying component and supports versions 4.x and 5.x. To integrate HTTPDNS into the OSS SDK, you can implement the DnsResolver interface.
HttpClient 5.x (default)
import org.apache.hc.client5.http.DnsResolver;
import com.alibaba.sdk.java.httpdns.HttpDnsClient;
import com.alibaba.sdk.java.httpdns.HTTPDNSResult;
import com.alibaba.sdk.java.httpdns.RequestIpType;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class HttpDnsResolver implements DnsResolver {
@Override
public String resolveCanonicalHostname(String host) { return host; }
@Override
public InetAddress[] resolve(String host) throws UnknownHostException {
HTTPDNSResult result = HttpDnsClient.getClient("your-account-id")
.getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.both);
List<InetAddress> addresses = new ArrayList<>();
addAddresses(addresses, result != null ? result.getIps() : null);
addAddresses(addresses, result != null ? result.getIpv6s() : null);
return addresses.isEmpty() ? InetAddress.getAllByName(host) : addresses.toArray(new InetAddress[0]);
}
private void addAddresses(List<InetAddress> list, String[] ips) {
if (ips == null) return;
for (String ip : ips) {
try { list.add(InetAddress.getByName(ip)); } catch (UnknownHostException e) {}
}
}
}HttpClient 4.x
import org.apache.http.conn.DnsResolver;
import com.alibaba.sdk.java.httpdns.HttpDnsClient;
import com.alibaba.sdk.java.httpdns.HTTPDNSResult;
import com.alibaba.sdk.java.httpdns.RequestIpType;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class HttpDnsResolver4 implements DnsResolver {
@Override
public InetAddress[] resolve(String host) throws UnknownHostException {
HTTPDNSResult result = HttpDnsClient.getClient("your-account-id")
.getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.both);
List<InetAddress> addresses = new ArrayList<>();
addAddresses(addresses, result != null ? result.getIps() : null);
addAddresses(addresses, result != null ? result.getIpv6s() : null);
return addresses.isEmpty() ? InetAddress.getAllByName(host) : addresses.toArray(new InetAddress[0]);
}
private void addAddresses(List<InetAddress> list, String[] ips) {
if (ips == null) return;
for (String ip : ips) {
try { list.add(InetAddress.getByName(ip)); } catch (UnknownHostException e) {}
}
}
}The DnsResolver for versions 4.x and 5.x is in different packages. For version 5.x, you must also implement the resolveCanonicalHostname() method.
4. Integrate with the OSS SDK
You can create a custom HttpClient instance and configure it for the OSSClient to override the default DNS service.
HttpClient 5.x (default)
import com.alibaba.sdk.java.httpdns.HttpDnsClient;
import com.alibaba.sdk.java.httpdns.InitConfig;
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.transport.apache5client.Apache5HttpClient;
import com.aliyun.sdk.service.oss2.transport.apache5client.Apache5HttpClientBuilder;
// Initialize HTTPDNS
InitConfig config = new InitConfig.Builder()
.setSecretKey("your-secret-key")
.setEnableHttps(true)
.setTimeoutMillis(2000)
.setEnableExpiredIp(true)
.enableMemoryCache(true)
.build();
HttpDnsClient.init("your-account-id", config);
// Create an HttpClient and inject DnsResolver
Apache5HttpClient httpClient = Apache5HttpClientBuilder.create()
// ... Other HttpClient configurations
.dnsResolver(new HttpDnsResolver())
.build();
// Create an OSSClient
OSSClient ossClient = OSSClient.newBuilder()
// ... Other OSS configurations
.httpClient(httpClient)
.build();HttpClient 4.x
// Initialize HTTPDNS
InitConfig config = new InitConfig.Builder()
.setSecretKey("your-secret-key")
.setEnableHttps(true)
.setTimeoutMillis(2000)
.setEnableExpiredIp(true)
.enableMemoryCache(true)
.build();
HttpDnsClient.init("your-account-id", config);
// Create an HttpClient and inject DnsResolver
Apache4HttpClient httpClient = Apache4HttpClientBuilder.create()
// ... Other HttpClient configurations
.dnsResolver(new HttpDnsResolver4())
.build();
// Inject the httpClient when creating the OSSClient
OSSClient ossClient = OSSClient.newBuilder()
// ... Other OSS configurations
.useApacheHttpClient4(true) // Specify that HttpClient 4.x is used
.httpClient(httpClient)
.build();Initialize the HTTPDNS SDK before initializing the OSS SDK.
5. Summary
Combining HTTPDNS with the OSS SDK effectively prevents DNS hijacking and accelerates domain name parsing, which improves access stability and security. The core of this implementation is to configure a custom HttpClient with HTTPDNS parsing logic for OSS.
Key points:
After you create a custom HttpClient, you must manually set configurations, such as timeout and concurrency, on the HttpClientBuilder. Otherwise, the default configurations are not applied.
Initialize HTTPDNS before you initialize OSS. Make sure to add the OSS domain names to the domain name whitelist in the HTTPDNS console.