通过 http 集成两个 Java webapps 的 IO 错误

IO errors integrating two Java webapps via http

我有两个 java webapps,每个都部署在单独的 tomcat 服务器上(并且在单独的机器上),其中一个进行了很多连续的(但顺序的,不是并行的)对另一个 (+1000) 的 http 请求。它大部分时间都有效,但是当 "client server"(生成请求的那个)的负载很高时,我会收到无法追踪的随机 IO 错误。

这是客户端的简化版本

public class BPXProxy {
    private static HttpClient client;
    public static void init(){
        client = new HttpClient();
    }
    public static InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
        JSONObject invoiceJSON = new JSONObject();
        invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
        invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
        String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
        PostMethod updatePage = new PostMethod(serviceURL);
        updatePage.addParameter("invoice", invoiceJSON.toJSONString());
        updatePage.addParameter("invoiceSubject", invoice.getInvoiceSubject());
        client.executeMethod(updatePage); // java.io.IOException: Stream closed, java.net.SocketException: Socket closed
        String response = updatePage.getResponseBodyAsString();  // java.io.IOException: chunked stream ended unexpectedly, java.io.IOException: CRLF expected at end of chunk: -1/-1
        updatePage.releaseConnection();
        JSONObject jsonResponse = JSONSimpleHelper.parseJSON(response);
        return jsonResponse;
    }
}

这些例外的例子:

java.io.IOException: chunked stream ended unexpectedly
        at org.apache.commons.httpclient.ChunkedInputStream.getChunkSizeFromInputStream(ChunkedInputStream.java:251)
        at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:220)
        at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:175)
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
        at java.io.FilterInputStream.read(FilterInputStream.java:107)
        at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
        at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
        at org.apache.commons.httpclient.HttpMethodBase.getResponseBodyAsString(HttpMethodBase.java:735)
        at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:208)

java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
    at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
    at org.apache.commons.httpclient.HttpParser.parseHeaders(HttpParser.java:165)
    at org.apache.commons.httpclient.HttpMethodBase.readResponseHeaders(HttpMethodBase.java:1790)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1592)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:207)

java.net.SocketException: Socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:146)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:77)
    at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105)
    at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1115)
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1832)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.getAccountStatusInvoices(BPXProxy.java:2905)

java.io.IOException: CRLF expected at end of chunk: -1/-1
    at org.apache.commons.httpclient.ChunkedInputStream.readCRLF(ChunkedInputStream.java:206)
    at org.apache.commons.httpclient.ChunkedInputStream.nextChunk(ChunkedInputStream.java:218)
    at org.apache.commons.httpclient.ChunkedInputStream.read(ChunkedInputStream.java:175)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:107)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
    at org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:684)
    at org.apache.commons.httpclient.HttpMethodBase.getResponseBodyAsString(HttpMethodBase.java:735)
    at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.getPersonOpenReceivables(BPXProxy.java:3132)

org.apache.commons.httpclient.ProtocolException: The server ec2-54-196-218-148.compute-1.amazonaws.com failed to respond with a valid HTTP response
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at net.slashware.mobilia.slashbillingClient.proxy.BPXProxy.registerInvoice(BPXProxy.java:207)

知道为什么会发生这些吗?看起来应该针对 "client server" 或其他服务器进行修复?

我在 aws 上执行此操作,每个服务器都在一个单独的 ec2 节点上,每个请求大约需要 4 秒才能完成。我正在使用 apache commons httpclient 进行调用;我已经监控了服务器,内存消耗没有问题,而且我没有看到每台服务器上的网络输入和网络输出峰值。

我推荐两件事。

首先,使用实例字段和方法,而不是静态字段和方法。您可能有竞争条件,因为响应没有按您预期的那样进入,并且您在 registerInvoice 的一次调用中关闭流,然后尝试在另一次调用中读取它们。所以,你的 class 看起来像

public class BPXProxy {
    private HttpClient client;
    public void init(){
        client = new HttpClient();
    }
    public InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
        JSONObject invoiceJSON = new JSONObject();
        invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
        invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
        String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
        PostMethod updatePage = new PostMethod(serviceURL);
        updatePage.addParameter("invoice", invoiceJSON.toJSONString());
        updatePage.addParameter("invoiceSubject", invoice.getInvoiceSubject());
        client.executeMethod(updatePage); // java.io.IOException: Stream closed, java.net.SocketException: Socket closed
        String response = updatePage.getResponseBodyAsString();  // java.io.IOException: chunked stream ended unexpectedly, java.io.IOException: CRLF expected at end of chunk: -1/-1
        updatePage.releaseConnection();
        JSONObject jsonResponse = JSONSimpleHelper.parseJSON(response);
        return jsonResponse;
    }
}

你会像

那样调用它
BPXProxy instance = new BPXProxy();
instance.init();
InvoiceCreationInfo info = instance.registerInvoice(invoice);

其次,您使用的似乎是 Apache HttpClient 3.x,它已经严重过时了。升级到 4.3 版(或更高版本,如果存在)。您必须更改您的代码,但基于 quick start,它不应该太剧烈:

public class BPXProxy {

    private CloseableHttpClient client = HttpClients.createDefault();

    public InvoiceCreationInfo registerInvoice(InvoiceData invoice) throws SQLException, HttpException, IOException {
        JSONObject invoiceJSON = new JSONObject();
        invoiceJSON.put("invoicer", invoice.getInvoicerIdentification().getDocument());
        invoiceJSON.put("buyer", invoice.getBuyerIdentification().getDocument());
        String serviceURL = ConfigurationManager.getBPXServicesPath() + "Invoice?operation=registerExternalInvoice&branchCode="+ branchCode;
        HttpPost httpPost = new HttpPost(serviceURL);
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add("invoice", invoiceJSON.toJSONString());
        nvps.add("invoiceSubject", invoice.getInvoiceSubject());
        client.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse rsp = client.execute(httpPost);
        try {
            HttpEntity entity = rsp.getEntity();
            String rspJson = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            return JSONSimpleHelper.parseJSON(rspJson);
        } finally {
            rsp.close();
        }
    }
}

(警告:我没有尝试编译或测试这段代码。)