|
|
@@ -3,9 +3,12 @@ package com.ruoyi.web.kb.service.impl;
|
|
3
|
3
|
import java.io.File;
|
|
4
|
4
|
import java.net.URI;
|
|
5
|
5
|
import java.nio.charset.StandardCharsets;
|
|
|
6
|
+import java.util.function.Supplier;
|
|
6
|
7
|
|
|
7
|
8
|
import com.ruoyi.web.kb.support.KbApiProperties;
|
|
8
|
9
|
import com.ruoyi.web.kb.service.KnowledgeBaseClient;
|
|
|
10
|
+import org.slf4j.Logger;
|
|
|
11
|
+import org.slf4j.LoggerFactory;
|
|
9
|
12
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
10
|
13
|
import org.springframework.core.io.FileSystemResource;
|
|
11
|
14
|
import org.springframework.http.HttpEntity;
|
|
|
@@ -16,6 +19,7 @@ import org.springframework.stereotype.Service;
|
|
16
|
19
|
import org.springframework.util.LinkedMultiValueMap;
|
|
17
|
20
|
import org.springframework.util.MultiValueMap;
|
|
18
|
21
|
import org.springframework.web.client.HttpStatusCodeException;
|
|
|
22
|
+import org.springframework.web.client.ResourceAccessException;
|
|
19
|
23
|
import org.springframework.web.client.RestTemplate;
|
|
20
|
24
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
21
|
25
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
@@ -28,6 +32,8 @@ import com.ruoyi.common.utils.StringUtils;
|
|
28
|
32
|
@Service
|
|
29
|
33
|
public class KnowledgeBaseClientImpl implements KnowledgeBaseClient
|
|
30
|
34
|
{
|
|
|
35
|
+ private static final Logger log = LoggerFactory.getLogger(KnowledgeBaseClientImpl.class);
|
|
|
36
|
+
|
|
31
|
37
|
private static final String[] FILE_ID_KEYS = { "file_id", "fileId", "kbDocId", "kb_doc_id", "docId", "id" };
|
|
32
|
38
|
|
|
33
|
39
|
private final KbApiProperties properties;
|
|
|
@@ -100,21 +106,18 @@ public class KnowledgeBaseClientImpl implements KnowledgeBaseClient
|
|
100
|
106
|
HttpHeaders headers = new HttpHeaders();
|
|
101
|
107
|
headers.add(properties.getAuthHeaderName(), properties.authHeaderValue());
|
|
102
|
108
|
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
|
|
103
|
|
- String raw;
|
|
104
|
|
- try
|
|
105
|
|
- {
|
|
106
|
|
- ResponseEntity<String> resp = restTemplate.exchange(URI.create(url), HttpMethod.POST, entity, String.class);
|
|
107
|
|
- raw = resp.getBody();
|
|
108
|
|
- }
|
|
109
|
|
- catch (HttpStatusCodeException e)
|
|
110
|
|
- {
|
|
111
|
|
- String b = e.getResponseBodyAsString(StandardCharsets.UTF_8);
|
|
112
|
|
- throw new ServiceException("知识库上传失败 HTTP " + e.getRawStatusCode() + ":" + abbreviate(b));
|
|
113
|
|
- }
|
|
114
|
|
- catch (Exception e)
|
|
115
|
|
- {
|
|
116
|
|
- throw new ServiceException("知识库上传异常:" + e.getMessage());
|
|
117
|
|
- }
|
|
|
109
|
+ String raw = executeWithRetry("知识库上传", () -> {
|
|
|
110
|
+ try
|
|
|
111
|
+ {
|
|
|
112
|
+ ResponseEntity<String> resp = restTemplate.exchange(URI.create(url), HttpMethod.POST, entity, String.class);
|
|
|
113
|
+ return resp.getBody();
|
|
|
114
|
+ }
|
|
|
115
|
+ catch (HttpStatusCodeException e)
|
|
|
116
|
+ {
|
|
|
117
|
+ String b = e.getResponseBodyAsString(StandardCharsets.UTF_8);
|
|
|
118
|
+ throw new ServiceException("知识库上传失败 HTTP " + e.getRawStatusCode() + ":" + abbreviate(b));
|
|
|
119
|
+ }
|
|
|
120
|
+ });
|
|
118
|
121
|
return parseUploadResponse(raw);
|
|
119
|
122
|
}
|
|
120
|
123
|
|
|
|
@@ -149,27 +152,153 @@ public class KnowledgeBaseClientImpl implements KnowledgeBaseClient
|
|
149
|
152
|
HttpHeaders headers = new HttpHeaders();
|
|
150
|
153
|
headers.add(properties.getAuthHeaderName(), properties.authHeaderValue());
|
|
151
|
154
|
HttpEntity<Void> entity = new HttpEntity<>(headers);
|
|
152
|
|
- try
|
|
153
|
|
- {
|
|
154
|
|
- ResponseEntity<String> resp = restTemplate.exchange(URI.create(url), HttpMethod.DELETE, entity, String.class);
|
|
155
|
|
- if (!resp.getStatusCode().is2xxSuccessful())
|
|
|
155
|
+ executeWithRetry("知识库删除", () -> {
|
|
|
156
|
+ try
|
|
156
|
157
|
{
|
|
157
|
|
- throw new ServiceException("知识库删除失败 HTTP " + resp.getStatusCodeValue());
|
|
|
158
|
+ ResponseEntity<String> resp = restTemplate.exchange(URI.create(url), HttpMethod.DELETE, entity, String.class);
|
|
|
159
|
+ if (!resp.getStatusCode().is2xxSuccessful())
|
|
|
160
|
+ {
|
|
|
161
|
+ throw new ServiceException("知识库删除失败 HTTP " + resp.getStatusCodeValue());
|
|
|
162
|
+ }
|
|
|
163
|
+ return null;
|
|
158
|
164
|
}
|
|
159
|
|
- }
|
|
160
|
|
- catch (HttpStatusCodeException e)
|
|
|
165
|
+ catch (HttpStatusCodeException e)
|
|
|
166
|
+ {
|
|
|
167
|
+ if (e.getRawStatusCode() == 404)
|
|
|
168
|
+ {
|
|
|
169
|
+ return null;
|
|
|
170
|
+ }
|
|
|
171
|
+ String b = e.getResponseBodyAsString(StandardCharsets.UTF_8);
|
|
|
172
|
+ throw new ServiceException("知识库删除失败 HTTP " + e.getRawStatusCode() + ":" + abbreviate(b));
|
|
|
173
|
+ }
|
|
|
174
|
+ });
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ /**
|
|
|
178
|
+ * 超时或 transient 失败时重试,最多 {@link KbApiProperties#getMaxRetries()} 次(不含首次请求)。
|
|
|
179
|
+ */
|
|
|
180
|
+ private <T> T executeWithRetry(String operation, Supplier<T> action)
|
|
|
181
|
+ {
|
|
|
182
|
+ int maxRetries = Math.max(0, properties.getMaxRetries());
|
|
|
183
|
+ Exception last = null;
|
|
|
184
|
+ for (int attempt = 0; attempt <= maxRetries; attempt++)
|
|
161
|
185
|
{
|
|
162
|
|
- if (e.getRawStatusCode() == 404)
|
|
|
186
|
+ try
|
|
|
187
|
+ {
|
|
|
188
|
+ return action.get();
|
|
|
189
|
+ }
|
|
|
190
|
+ catch (ServiceException e)
|
|
|
191
|
+ {
|
|
|
192
|
+ if (attempt >= maxRetries || !isRetryable(e))
|
|
|
193
|
+ {
|
|
|
194
|
+ throw e;
|
|
|
195
|
+ }
|
|
|
196
|
+ last = e;
|
|
|
197
|
+ log.warn("{}失败,第 {}/{} 次重试:{}", operation, attempt + 1, maxRetries, e.getMessage());
|
|
|
198
|
+ sleepBeforeRetry();
|
|
|
199
|
+ }
|
|
|
200
|
+ catch (Exception e)
|
|
163
|
201
|
{
|
|
164
|
|
- return;
|
|
|
202
|
+ ServiceException wrapped = wrapUnexpected(operation, e);
|
|
|
203
|
+ if (attempt >= maxRetries || !isRetryable(wrapped, e))
|
|
|
204
|
+ {
|
|
|
205
|
+ throw wrapped;
|
|
|
206
|
+ }
|
|
|
207
|
+ last = wrapped;
|
|
|
208
|
+ log.warn("{}异常,第 {}/{} 次重试:{}", operation, attempt + 1, maxRetries, e.getMessage());
|
|
|
209
|
+ sleepBeforeRetry();
|
|
165
|
210
|
}
|
|
166
|
|
- String b = e.getResponseBodyAsString(StandardCharsets.UTF_8);
|
|
167
|
|
- throw new ServiceException("知识库删除失败 HTTP " + e.getRawStatusCode() + ":" + abbreviate(b));
|
|
168
|
211
|
}
|
|
169
|
|
- catch (Exception e)
|
|
|
212
|
+ if (last instanceof ServiceException)
|
|
|
213
|
+ {
|
|
|
214
|
+ throw (ServiceException) last;
|
|
|
215
|
+ }
|
|
|
216
|
+ throw wrapUnexpected(operation, last);
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ private static boolean isRetryable(ServiceException e)
|
|
|
220
|
+ {
|
|
|
221
|
+ String msg = e.getMessage();
|
|
|
222
|
+ if (msg == null)
|
|
|
223
|
+ {
|
|
|
224
|
+ return false;
|
|
|
225
|
+ }
|
|
|
226
|
+ if (msg.contains("HTTP 404"))
|
|
|
227
|
+ {
|
|
|
228
|
+ return false;
|
|
|
229
|
+ }
|
|
|
230
|
+ if (msg.contains("HTTP 408") || msg.contains("HTTP 429"))
|
|
|
231
|
+ {
|
|
|
232
|
+ return true;
|
|
|
233
|
+ }
|
|
|
234
|
+ if (msg.matches(".*HTTP [45]\\d\\d.*"))
|
|
|
235
|
+ {
|
|
|
236
|
+ int code = extractHttpStatus(msg);
|
|
|
237
|
+ return code >= 500 || code == 408 || code == 429;
|
|
|
238
|
+ }
|
|
|
239
|
+ return false;
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+ private static boolean isRetryable(ServiceException wrapped, Exception cause)
|
|
|
243
|
+ {
|
|
|
244
|
+ if (cause instanceof ResourceAccessException)
|
|
|
245
|
+ {
|
|
|
246
|
+ return true;
|
|
|
247
|
+ }
|
|
|
248
|
+ return isRetryable(wrapped);
|
|
|
249
|
+ }
|
|
|
250
|
+
|
|
|
251
|
+ private static int extractHttpStatus(String msg)
|
|
|
252
|
+ {
|
|
|
253
|
+ int idx = msg.indexOf("HTTP ");
|
|
|
254
|
+ if (idx < 0)
|
|
|
255
|
+ {
|
|
|
256
|
+ return 0;
|
|
|
257
|
+ }
|
|
|
258
|
+ int start = idx + 5;
|
|
|
259
|
+ int end = start;
|
|
|
260
|
+ while (end < msg.length() && Character.isDigit(msg.charAt(end)))
|
|
|
261
|
+ {
|
|
|
262
|
+ end++;
|
|
|
263
|
+ }
|
|
|
264
|
+ if (end == start)
|
|
|
265
|
+ {
|
|
|
266
|
+ return 0;
|
|
|
267
|
+ }
|
|
|
268
|
+ try
|
|
|
269
|
+ {
|
|
|
270
|
+ return Integer.parseInt(msg.substring(start, end));
|
|
|
271
|
+ }
|
|
|
272
|
+ catch (NumberFormatException e)
|
|
|
273
|
+ {
|
|
|
274
|
+ return 0;
|
|
|
275
|
+ }
|
|
|
276
|
+ }
|
|
|
277
|
+
|
|
|
278
|
+ private void sleepBeforeRetry()
|
|
|
279
|
+ {
|
|
|
280
|
+ int interval = Math.max(0, properties.getRetryIntervalMs());
|
|
|
281
|
+ if (interval <= 0)
|
|
|
282
|
+ {
|
|
|
283
|
+ return;
|
|
|
284
|
+ }
|
|
|
285
|
+ try
|
|
|
286
|
+ {
|
|
|
287
|
+ Thread.sleep(interval);
|
|
|
288
|
+ }
|
|
|
289
|
+ catch (InterruptedException e)
|
|
|
290
|
+ {
|
|
|
291
|
+ Thread.currentThread().interrupt();
|
|
|
292
|
+ }
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ private static ServiceException wrapUnexpected(String operation, Exception e)
|
|
|
296
|
+ {
|
|
|
297
|
+ if (e instanceof ServiceException)
|
|
170
|
298
|
{
|
|
171
|
|
- throw new ServiceException("知识库删除异常:" + e.getMessage());
|
|
|
299
|
+ return (ServiceException) e;
|
|
172
|
300
|
}
|
|
|
301
|
+ return new ServiceException(operation + "异常:" + e.getMessage());
|
|
173
|
302
|
}
|
|
174
|
303
|
|
|
175
|
304
|
private static String encodePathSegment(String id)
|