503, https://www.dmxapi.cn/v1/chat/completions at com.sopwr.scrape.common.HtmlUt...

2026年07月10日 15:31 44.74 秒 success

错误信息

com.sopwr.scrape.common.model.WorkerException: java.lang.RuntimeException: ERROR 503, https://www.dmxapi.cn/v1/chat/completions at com.sopwr.scrape.common.HtmlUtil.request(HtmlUtil.java:396) at com.sopwr.scrape.common.HtmlUtil.requestJson(HtmlUtil.java:635) at com.sopwr.scrape.common.HtmlUtil.requestJson(HtmlUtil.java:614) at com.sopwr.server.cache.TestDmxapi.main(TestDmxapi.java:22) Caused by: java.lang.RuntimeException: ERROR 503, https://www.dmxapi.cn/v1/chat/completions at com.sopwr.scrape.common.HtmlUtil.request(HtmlUtil.java:372)

AI 解决方案

🔍 错误分析

1. 🌐 错误翻译


原文:com.sopwr.scrape.common.model.WorkerException: java.lang.RuntimeException: ERROR 503, https://www.dmxapi.cn/v1/chat/completions
译文:com.sopwr.scrape.common.model.WorkerException:java.lang.RuntimeException:错误 503,https://www.dmxapi.cn/v1/chat/completions

2. 🎯 原因分析


  • 根本原因:HTTP 503 服务不可用错误,表明目标服务(DMXAPI)暂时无法处理请求

  • 发生位置:在 HtmlUtil.request() 方法的第372行和396行发起的HTTP请求失败

  • 上下文TestDmxapi.main() 方法调用 HtmlUtil.requestJson() 时触发了底层HTTP请求,最终抛出503错误
  • 3. 💡 解决方案


    由于503错误是服务端问题,客户端需要添加重试机制和超时处理:

    // 在请求方法中添加重试逻辑和超时配置
    public static String requestJson(String url, String jsonPayload, int maxRetries) throws WorkerException {
        int attempt = 0;
        while (attempt <= maxRetries) {
            try {
                // 构建带超时设置的HTTP请求
                HttpRequest request = HttpRequest.newBuilder()
                        .uri(new URI(url))
                        .header("Content-Type", "application/json")
                        .timeout(Duration.ofSeconds(10)) // 10秒超时
                        .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                        .build();
                
                HttpResponse<String> response = HttpClient.newHttpClient()
                        .send(request, HttpResponse.BodyHandlers.ofString());
                
                if (response.statusCode() == 200) {
                    return response.body();
                } else if (response.statusCode() == 503 && attempt < maxRetries) {
                    attempt++;
                    Thread.sleep(1000 * attempt); // 指数退避重试
                    continue;
                } else {
                    throw new WorkerException("HTTP Error: " + response.statusCode());
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new WorkerException("Request interrupted", e);
            } catch (Exception e) {
                if (attempt >= maxRetries) {
                    throw new WorkerException("Max retries exceeded", e);
                }
                attempt++;
                try {
                    Thread.sleep(1000 * attempt);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    throw new WorkerException("Request interrupted", ie);
                }
            }
        }
        throw new WorkerException("Request failed after " + maxRetries + " retries");
    }
    

    使用时调用:

    // 修改TestDmxapi.main方法中的调用
    public static void main(String[] args) {
        try {
            String result = HtmlUtil.requestJson("https://www.dmxapi.cn/v1/chat/completions", payload, 3);
            System.out.println(result);
        } catch (WorkerException e) {
            e.printStackTrace();
            // 记录重试失败日志,考虑降级处理
        }
    }
    

    4. 🛡️ 预防措施


  • 添加重试机制:对临时性服务错误(503、504等)实现指数退避重试,建议最大重试次数3次

  • 超时配置:为HTTP请求设置合理的连接超时和读取超时(建议10-30秒),避免长时间阻塞

  • 监控告警:集成健康检查工具,如Prometheus监控HTTP状态码,对5xx错误率配置告警

  • 降级方案:当服务不可用时,设计降级逻辑,如返回缓存数据或友好提示,保障核心功能可用
  • 推荐工具:

  • Postman:用于手动测试接口状态

  • JUnit测试框架:编写重试逻辑的单元测试

  • Resilience4j:成熟的Java容错库,提供重试、熔断、限流功能