task-77: 统一 Coze、品牌检查和紫鸟 HTTP 客户端的连接复用策略

This commit is contained in:
2026-08-30 21:08:28 +08:00
parent d8682bb4a1
commit 295cdc815c
5 changed files with 253 additions and 12 deletions
@@ -12,6 +12,8 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import com.nanri.aiimage.config.HttpClientPool;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -160,10 +162,9 @@ public class BrandCheckClient {
}
synchronized (this) {
if (sharedRestClient == null) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(properties.getConnectTimeoutMillis());
requestFactory.setReadTimeout(properties.getReadTimeoutMillis());
sharedRestClient = RestClient.builder().requestFactory(requestFactory).build();
sharedRestClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(properties.getReadTimeoutMillis()))
.build();
}
return sharedRestClient;
}
@@ -15,6 +15,8 @@ import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestClient;
import com.nanri.aiimage.config.HttpClientPool;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -749,10 +751,9 @@ public class SimilarAsinCozeClient {
}
synchronized (this) {
if (sharedRestClient == null) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(properties.getCozeConnectTimeoutMillis());
requestFactory.setReadTimeout(properties.getCozeReadTimeoutMillis());
sharedRestClient = RestClient.builder().requestFactory(requestFactory).build();
sharedRestClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(properties.getCozeReadTimeoutMillis()))
.build();
}
return sharedRestClient;
}
@@ -13,6 +13,8 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import com.nanri.aiimage.config.HttpClientPool;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -25,6 +27,9 @@ public class ZiniaoClientImpl implements ZiniaoClient {
private final ZiniaoProperties ziniaoProperties;
private final ObjectMapper objectMapper;
/** Task 77:单例 RestClient(共享连接池),避免每次调用新建短命客户端。 */
private volatile RestClient sharedRestClient;
@Override
public Long getCompanyIdByApiKey(String apiKey) {
String raw = getWithApiKey(apiKey, "/app/builtin/company", "获取 companyId");
@@ -246,10 +251,23 @@ public class ZiniaoClientImpl implements ZiniaoClient {
}
private RestClient getRestClient() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(ziniaoProperties.getConnectTimeoutSeconds() * 1000);
requestFactory.setReadTimeout(ziniaoProperties.getReadTimeoutSeconds() * 1000);
return RestClient.builder().requestFactory(requestFactory).build();
RestClient client = sharedRestClient;
if (client != null) {
return client;
}
synchronized (this) {
if (sharedRestClient == null) {
sharedRestClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(ziniaoProperties.getReadTimeoutSeconds() * 1000))
.build();
}
return sharedRestClient;
}
}
/** 反射/测试可见:与 getRestClient 同一单例。 */
RestClient restClient() {
return getRestClient();
}
private void addStaffItem(List<ZiniaoStaffItemVo> items, JsonNode itemNode) {