From fcec51bb7154791ddcbb03e11145d26c2104f14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Mon, 28 Sep 2026 09:27:19 +0800 Subject: [PATCH] =?UTF-8?q?perf(OSS):=20=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E8=AF=BB=E5=86=99=E4=B8=8E=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E9=A2=84=E7=AD=BE=E5=90=8D=E6=8B=86=E6=88=90=E4=B8=A4?= =?UTF-8?q?=E5=BC=A0=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java 在自己机器上用 https://oss.aishufu.top 读写对象,等于走公网 EIP 回环 (ip route get 自己的公网 IP 是 via 192.168.0.253 dev eth0),实测只有 0.98 MB/s、 TLS 首字节 39ms;同一台走 VPC 内网是 900 MB/s、0.9ms。结果 Excel 出表与输入件下载都吃这条。 不能直接把 endpoint 改成内网地址:预签名 URL 的 host 必须是浏览器可达的反代入口, 否则用户侧直传当场全断。所以新增 aiimage.oss.internal-endpoint(默认空=沿用 endpoint, 行为与今天完全一致),只给服务端 I/O 的 client 用;presign 那两处改用 public-endpoint 的 client, 并把内网地址纳入 managedEndpoints,避免历史数据里出现内网 host 时不归一化。 启动打一行 [oss] 服务端读写端点=…(预签名端点=…),发版后直接回读生效值。 --- .../nanri/aiimage/config/OssProperties.java | 5 ++ .../file/service/oss/OssStorageService.java | 51 ++++++++++++++++--- .../src/main/resources/application.yml | 3 ++ .../service/oss/OssStorageServiceTest.java | 24 +++++++++ 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/OssProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/OssProperties.java index 946b4e176..3ffefc8d4 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/OssProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/OssProperties.java @@ -9,6 +9,11 @@ public class OssProperties { private String region; private String endpoint; private String publicEndpoint; + /** + * 服务端自己读写对象时用的地址(VPC 内网),为空表示与 {@link #endpoint} 相同。 + * 拆开是因为 endpoint 必须保持公网域名:预签名 URL 的 host 要等于浏览器能到达的反代入口。 + */ + private String internalEndpoint; private String downloadEndpoint; private String bucket; private String imageVideoBucket; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java index 8987f5746..b89f74c6e 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java @@ -47,7 +47,10 @@ public class OssStorageService { private static final long DEFAULT_READ_MAX_BYTES = 20L * 1024 * 1024; private final OssProperties ossProperties; + /** 服务端读写对象用的 client:配置了 internal-endpoint 时走 VPC 内网,否则与 endpoint 一致。 */ private final MinioClient minioClient; + /** 只用于签发浏览器直传预签名 URL:host 必须是公网反代入口,内网地址签出来的 URL 用户侧不可达。 */ + private final MinioClient presignClient; @Autowired public OssStorageService(OssProperties ossProperties) { @@ -56,7 +59,15 @@ public class OssStorageService { OssStorageService(OssProperties ossProperties, MinioClient minioClient) { this.ossProperties = ossProperties; - this.minioClient = minioClient == null ? createClient(ossProperties) : minioClient; + if (minioClient != null) { + this.minioClient = minioClient; + this.presignClient = minioClient; + return; + } + this.minioClient = createClient(ossProperties, objectIoEndpoint(ossProperties)); + this.presignClient = objectIoEndpoint(ossProperties).equals(signingEndpoint(ossProperties)) + ? this.minioClient + : createClient(ossProperties, signingEndpoint(ossProperties)); } /** @@ -66,6 +77,8 @@ public class OssStorageService { */ @PostConstruct void checkShopDataBucketConfigured() { + log.info("[oss] 服务端读写端点={}(预签名端点={})", + withScheme(objectIoEndpoint(ossProperties)), withScheme(signingEndpoint(ossProperties))); String configured = ossProperties.getShopDataBucket(); if (configured == null || configured.isBlank()) { log.warn("[oss] aiimage.oss.shop-data-bucket 未配置,店铺数据采集累计档将落默认桶 {};" @@ -133,14 +146,15 @@ public class OssStorageService { /** * 为软件版本安装包签发浏览器直传用的预签名 PUT URL(client 桶公开路径)。 - * 签名 host 与反代入口一致(endpoint 即 public-endpoint oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象。 + * 签名 host 取 public-endpoint(oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象; + * 服务端自己的读写走 internal-endpoint 那张 client,两者不能合用一张。 */ public String presignSoftwareVersionUpload(String objectKey, int expirySeconds) { if (objectKey == null || !objectKey.startsWith(SOFTWARE_VERSION_PREFIX)) { throw new IllegalArgumentException("software version objectKey must start with " + SOFTWARE_VERSION_PREFIX); } try { - return buildClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() + return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.PUT) .bucket(publicClientBucket()) .object(objectKey) @@ -197,12 +211,12 @@ public class OssStorageService { /** * 为教程安装包签发浏览器直传用的预签名 PUT URL(公开 client 桶 tutorial/ 前缀)。 - * 签名 host 与反代入口一致,浏览器对返回的 URL 直接 PUT 即可落对象。 + * 签名 host 同 {@link #presignSoftwareVersionUpload}:必须是公网入口,不能用内网 client。 */ public String presignTutorialUpload(String objectKey, int expirySeconds) { requireTutorialKey(objectKey); try { - return buildClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() + return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.PUT) .bucket(publicClientBucket()) .object(objectKey) @@ -664,7 +678,8 @@ public class OssStorageService { } private List managedEndpoints() { - return Stream.of(ossProperties.getEndpoint(), ossProperties.getPublicEndpoint(), LEGACY_MINIO_ENDPOINT) + return Stream.of(ossProperties.getEndpoint(), ossProperties.getPublicEndpoint(), + ossProperties.getInternalEndpoint(), LEGACY_MINIO_ENDPOINT) .filter(Objects::nonNull) .map(String::trim) .filter(endpoint -> !endpoint.isBlank()) @@ -719,9 +734,29 @@ public class OssStorageService { return minioClient; } - private static MinioClient createClient(OssProperties properties) { + private MinioClient presignClient() { + return presignClient; + } + + /** 服务端读写用的地址:配了内网就走内网,否则维持 endpoint(旧行为)。 */ + static String objectIoEndpoint(OssProperties properties) { + return hasInternalEndpoint(properties) ? properties.getInternalEndpoint().trim() : properties.getEndpoint(); + } + + /** 预签名 URL 的 host:优先 public-endpoint,与 {@link #publicEndpoint()} 的取值口径一致。 */ + static String signingEndpoint(OssProperties properties) { + String preferred = properties.getPublicEndpoint(); + return preferred == null || preferred.isBlank() ? properties.getEndpoint() : preferred; + } + + private static boolean hasInternalEndpoint(OssProperties properties) { + String value = properties.getInternalEndpoint(); + return value != null && !value.isBlank(); + } + + private static MinioClient createClient(OssProperties properties, String endpoint) { MinioClient client = MinioClient.builder() - .endpoint(withScheme(properties.getEndpoint())) + .endpoint(withScheme(endpoint)) .credentials(properties.getAccessKeyId(), properties.getAccessKeySecret()) .build(); // MinIO 8.5.17 默认把 connect/write/read 三项全设成 5 分钟(S3Base.DEFAULT_CONNECTION_TIMEOUT)。 diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 7b6161c18..0a265499f 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -111,6 +111,9 @@ aiimage: region: ${AIIMAGE_OSS_REGION:us-east-1} endpoint: ${AIIMAGE_OSS_ENDPOINT:https://oss.aishufu.top} public-endpoint: ${AIIMAGE_OSS_PUBLIC_ENDPOINT:https://oss.aishufu.top} + # 服务端读写对象用的内网地址;留空=沿用 endpoint。生产两台都在同一 VPC,公网入口只有 5–8 Mbps + # 而内网 ~1 GB/s,且发往自己的 EIP 要出 VPC 网关绕一圈。预签名 URL 仍按 endpoint 签,不受影响。 + internal-endpoint: ${AIIMAGE_OSS_INTERNAL_ENDPOINT:} download-endpoint: ${AIIMAGE_OSS_DOWNLOAD_ENDPOINT:} bucket: ${AIIMAGE_OSS_BUCKET:nanri-ai-images} image-video-bucket: ${AIIMAGE_IMAGE_VIDEO_OSS_BUCKET:shufu-video} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/oss/OssStorageServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/oss/OssStorageServiceTest.java index dba5d0ded..44f1aa9b9 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/oss/OssStorageServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/oss/OssStorageServiceTest.java @@ -228,4 +228,28 @@ class OssStorageServiceTest { "https://download.aishufu.top/shufu-shop-data/result/shop_data_crawl/abc/out.xlsx", storageService.generateFreshDownloadUrl("result/shop_data_crawl/abc/out.xlsx")); } + + @Test + void serverIoEndpointFollowsPublicEndpointWhenInternalIsAbsent() { + assertEquals("https://oss.aishufu.top", OssStorageService.objectIoEndpoint(properties)); + assertEquals("https://oss.aishufu.top", OssStorageService.signingEndpoint(properties)); + } + + @Test + void internalEndpointMovesServerIoButKeepsPresignPublic() { + properties().setInternalEndpoint(" http://192.168.0.171:9000 "); + assertEquals("http://192.168.0.171:9000", OssStorageService.objectIoEndpoint(properties)); + assertEquals("https://oss.aishufu.top", OssStorageService.signingEndpoint(properties)); + } + + /** 内网地址只给服务端自己用;万一有历史数据把它写进了 URL,读出来仍要归一化成公网入口。 */ + @Test + void internalEndpointUrlsAreNormalizedToPublicEndpoint() { + properties().setInternalEndpoint("http://192.168.0.171:9000"); + OssStorageService oss = new OssStorageService(properties); + assertEquals( + "https://oss.aishufu.top/nanri-ai-images/result/appearance_patent/abc/out.xlsx", + oss.normalizeManagedPublicUrl( + "http://192.168.0.171:9000/nanri-ai-images/result/appearance_patent/abc/out.xlsx")); + } }