task-77: 统一 Coze、品牌检查和紫鸟 HTTP 客户端的连接复用策略
This commit is contained in:
+22
-4
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user