fix(货源): 主图重试换备用域名绕开被钉死的坏 CDN 节点

根因不是"瞬时抖动":m.media-amazon.com 的多 CDN 池里 Akamai 系边缘没配这个
虚拟主机名,带该 SNI 一律 RST(同 IP 不带 SNI 或用 images-na.ssl-images-amazon.com
则正常),而 JVM 正向 DNS 缓存会把坏节点钉住一整批 → 秒级重试三次全打在它身上。
实测最近 20 条失败 URL 走备用域名 20/20 成功且 md5 与原域名逐字节相同。
This commit is contained in:
2026-09-28 12:29:39 +08:00
parent 8f3b879d80
commit e5b79b71e1
5 changed files with 169 additions and 8 deletions
@@ -549,7 +549,8 @@ public class SimilarAsinLlmService {
}
/**
* 主图下载:连接级异常按 {@code aiimage.similar-asin.llm-image-download-retry-times} 重试(默认额外 2 次)。
* 主图下载:连接级异常按 {@code aiimage.similar-asin.llm-image-download-retry-times} 重试(默认额外 2 次),
* 且第 2 次起换到 {@code aiimage.similar-asin.llm-image-host-alias-map} 里的备用域名。
* 只重试连接级失败——非 200 是源站的确定答复,重试只是白等超时。
* 总耗时受 {@link #MAIN_DOWNLOAD_BUDGET_MILLIS} 约束:慢源 hang 住时不放大尾延迟
* (同模块的 alicdn 拼图已被这条咬过,见 SimilarAsinProperties#imageDownloadTimeoutSeconds 注释)。
@@ -564,12 +565,15 @@ public class SimilarAsinLlmService {
long deadline = System.currentTimeMillis() + MAIN_DOWNLOAD_BUDGET_MILLIS;
IOException last = null;
for (int attempt = 0; attempt <= retryTimes; attempt++) {
// 首试保持原域名;连接级失败后换备用域名 —— 坏的是解析到的那个 CDN 节点,
// 同一个名字重试三次还是它,换个名字才等于换一批节点。
String target = attempt == 0 ? trimmed : defaultToAlias(aliasedUrl(trimmed), trimmed);
if (attempt > 0 && (System.currentTimeMillis() > deadline || !sleepBeforeRetry(attempt))) {
break;
}
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(trimmed))
.uri(URI.create(target))
.timeout(Duration.ofSeconds(timeoutSeconds))
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
// 刻意不发 Referer:阿里图床按 Referer 白名单防盗链,外链域名会被判 403
@@ -580,11 +584,11 @@ public class SimilarAsinLlmService {
if (response.statusCode() == 200 && response.body() != null && response.body().length > 0) {
if (attempt > 0) {
log.info("[similar-asin][llm] main download retry-success url={} attempt={}/{}",
abbreviate(trimmed, 160), attempt + 1, retryTimes + 1);
abbreviate(target, 160), attempt + 1, retryTimes + 1);
}
return response.body();
}
log.warn("[similar-asin][llm] main download http {} url={}", response.statusCode(), abbreviate(trimmed, 160));
log.warn("[similar-asin][llm] main download http {} url={}", response.statusCode(), abbreviate(target, 160));
return null;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
@@ -592,7 +596,7 @@ public class SimilarAsinLlmService {
} catch (Exception ex) {
last = new IOException("主图下载失败(" + failureMessage(ex) + ")", ex);
log.warn("[similar-asin][llm] main download fail url={} attempt={}/{} err={}",
abbreviate(trimmed, 160), attempt + 1, retryTimes + 1, failureMessage(ex));
abbreviate(target, 160), attempt + 1, retryTimes + 1, failureMessage(ex));
if (Thread.currentThread().isInterrupted()) {
break;
}
@@ -601,6 +605,50 @@ public class SimilarAsinLlmService {
throw last == null ? new IOException("主图下载失败: " + abbreviate(trimmed, 160)) : last;
}
private static String defaultToAlias(String alias, String fallback) {
return alias == null ? fallback : alias;
}
/** 按 {@code aiimage.similar-asin.llm-image-host-alias-map} 换 host,其余部分逐字符保留。 */
private String aliasedUrl(String url) {
String map = properties.getLlmImageHostAliasMap();
if (map == null || map.isBlank()) {
return null;
}
URI uri;
try {
uri = URI.create(url);
} catch (Exception ex) {
return null;
}
String host = uri.getHost();
if (host == null || host.isEmpty()) {
return null;
}
String text = uri.toString();
int at = text.indexOf("//" + host);
if (at < 0) {
return null;
}
int end = at + 2 + host.length();
char after = end < text.length() ? text.charAt(end) : '/';
if (after != ':' && after != '/' && after != '?' && after != '#') {
return null;
}
for (String pair : map.split(";")) {
int eq = pair.indexOf('=');
if (eq <= 0 || eq >= pair.length() - 1) {
continue;
}
String from = pair.substring(0, eq).trim();
String to = pair.substring(eq + 1).trim();
if (from.equalsIgnoreCase(host) && !to.isEmpty()) {
return text.substring(0, at + 2) + to + text.substring(end);
}
}
return null;
}
private static boolean sleepBeforeRetry(int attempt) {
try {
Thread.sleep(Math.min(1000L, 250L * attempt));