1
This commit is contained in:
+64
-4
@@ -13,6 +13,13 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -841,9 +848,16 @@ public class SocialMediaServiceImpl implements ISocialMediaService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("转发 Jarvis LLM 测试: {}", url);
|
log.info("转发 Jarvis LLM 测试: {}", url);
|
||||||
String resp = HttpUtils.sendJsonPost(url, body.toJSONString());
|
String resp;
|
||||||
|
try {
|
||||||
|
resp = postJarvisJson(url, body.toJSONString(), 8000, 120000);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("转发 Jarvis LLM 测试失败 url={}", url, e);
|
||||||
|
return AjaxResult.error("连不上 Jarvis(" + url + "):" + e.getMessage()
|
||||||
|
+ "。请确认 Jarvis_java 已启动,且 jarvis.server.jarvis-java.base-url 正确");
|
||||||
|
}
|
||||||
if (StringUtils.isEmpty(resp)) {
|
if (StringUtils.isEmpty(resp)) {
|
||||||
return AjaxResult.error("Jarvis 返回为空,请检查 jarvis.server.jarvis-java.base-url 与 Jarvis 服务是否可达");
|
return AjaxResult.error("Jarvis 返回为空(" + url + "),请确认 Jarvis_java 已启动");
|
||||||
}
|
}
|
||||||
Object parsed = JSON.parse(resp);
|
Object parsed = JSON.parse(resp);
|
||||||
if (parsed instanceof JSONObject) {
|
if (parsed instanceof JSONObject) {
|
||||||
@@ -1118,9 +1132,14 @@ public class SocialMediaServiceImpl implements ISocialMediaService
|
|||||||
if (StringUtils.isNotEmpty(profileId)) {
|
if (StringUtils.isNotEmpty(profileId)) {
|
||||||
body.put("profileId", profileId);
|
body.put("profileId", profileId);
|
||||||
}
|
}
|
||||||
String resp = HttpUtils.sendJsonPost(url, body.toJSONString());
|
String resp;
|
||||||
|
try {
|
||||||
|
resp = postJarvisJson(url, body.toJSONString(), 8000, 180000);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("连不上 Jarvis(" + url + "):" + e.getMessage(), e);
|
||||||
|
}
|
||||||
if (StringUtils.isEmpty(resp)) {
|
if (StringUtils.isEmpty(resp)) {
|
||||||
throw new RuntimeException("Jarvis 返回为空,请检查服务可达性");
|
throw new RuntimeException("Jarvis 返回为空(" + url + "),请确认 Jarvis_java 已启动");
|
||||||
}
|
}
|
||||||
Object parsed = JSON.parse(resp);
|
Object parsed = JSON.parse(resp);
|
||||||
if (!(parsed instanceof JSONObject)) {
|
if (!(parsed instanceof JSONObject)) {
|
||||||
@@ -1167,5 +1186,46 @@ public class SocialMediaServiceImpl implements ISocialMediaService
|
|||||||
return text.replaceAll(DEFAULT_XIANYU_TITLE_CLEAN_REGEX, "");
|
return text.replaceAll(DEFAULT_XIANYU_TITLE_CLEAN_REGEX, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String postJarvisJson(String url, String json, int connectMs, int readMs) throws Exception {
|
||||||
|
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||||
|
try {
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setConnectTimeout(connectMs);
|
||||||
|
conn.setReadTimeout(readMs);
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
||||||
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
|
os.write(bytes);
|
||||||
|
}
|
||||||
|
int code = conn.getResponseCode();
|
||||||
|
InputStream is = code >= 200 && code < 300 ? conn.getInputStream() : conn.getErrorStream();
|
||||||
|
String resp = readAll(is);
|
||||||
|
if (code < 200 || code >= 300) {
|
||||||
|
throw new RuntimeException("HTTP " + code + (StringUtils.isEmpty(resp) ? "" : (" " + resp)));
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
} catch (ConnectException e) {
|
||||||
|
throw new RuntimeException("连接被拒绝", e);
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
throw new RuntimeException("连接超时", e);
|
||||||
|
} finally {
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String readAll(InputStream is) throws java.io.IOException {
|
||||||
|
if (is == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
byte[] buf = new byte[4096];
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int n;
|
||||||
|
while ((n = is.read(buf)) >= 0) {
|
||||||
|
sb.append(new String(buf, 0, n, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user