连接重置消耗REST服务(scala / spray)

时间:2016-04-07 21:50:20

标签: java scala rest jmeter spray

我发送并发请求休息服务时遇到问题;客户端(Apache JMeter)中的消息对于某些请求是“连接重置”,具体取决于请求数量,例如,我发送100个请求并且服务器的响应是100%成功,但是如果我发送500个请求,则30 %的答案都是错误的。

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:61)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
    at org.apache.jmeter.protocol.http.sampler.MeasuringConnectionManager$MeasuredConnection.receiveResponseHeader(MeasuringConnectionManager.java:201)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:517)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:331)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)
    at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
    at java.lang.Thread.run(Thread.java:745)

我修改了“application.conf”,内容是下一个:

spray.can {
  server {
    server-header = spray-can/${spray.version}
    ssl-encryption = off
    pipelining-limit = 16 
    idle-timeout = 60 s 
    request-timeout = 30 s
    timeout-timeout = 2 s
    timeout-handler = ""
    reaping-cycle = 250 ms
    stats-support = on
    remote-address-header = off
    raw-request-uri-header = off
    transparent-head-requests = on
    chunkless-streaming = off
    verbose-error-messages = on
    request-chunk-aggregation-limit = 1m
    response-header-size-hint = 512
    bind-timeout = infinite
    unbind-timeout = 1s
    registration-timeout = 1s
    default-host-header = ""
    automatic-back-pressure-handling = on
    back-pressure {
      noack-rate = 10
      reading-low-watermark = infinite
    }
    parsing = ${spray.can.parsing}
  }
  client {
    user-agent-header = spray-can/${spray.version}
    idle-timeout = 60 s
    request-timeout = 40 s 
    reaping-cycle = 250 ms
    response-chunk-aggregation-limit = 1m
    chunkless-streaming = off
    request-header-size-hint = 256 
    max-encryption-chunk-size = 1m
    connecting-timeout = 30s
    proxy {
      http = default
      https = default
    }
    ssl-tracing = off
    parsing = ${spray.can.parsing}
  }
  host-connector {
    max-connections = 80 
    max-retries = 8 
    max-redirects = 0
    pipelining = enabled
    idle-timeout = 30 s
    client = ${spray.can.client}
  }
}

JVM的设置是:

-Xms1024M
-Xmx2048M 
-Xss1M 
-XX:MaxPermSize=1024m

重要提示:因为bussines逻辑,服务器支持并发事务是必要的;在不到5秒的时间内完成500个单独的连接(交易)。

2 个答案:

答案 0 :(得分:2)

您的timeout设置看起来很好,每秒处理500个请求绝对不是问题。

您的请求很可能需要很长时间才能处理,即超过request-timeout + timeout-timeout = 32秒。您需要检查您的架构,看看它花费了多少时间和原因。对于常规Web服务而言,这是非常不寻常的,其中大多数请求以毫秒范围完成。如果您需要执行一些繁重的处理,这需要花费超过超时的时间,您可以使用202 Accepted进行回复并在后台进行处理。您可以返回一个URI,其中客户端可以检查请求的状态,或使用回调到客户端或任何其他机制来通知请求已完成。

请记住,不要阻止路由本身,否则您将有效阻止所有其他请求,并且您可能会遇到超时错误。例如,请参阅此答案:Use a Dispatcher with Spray HttpService。要实现非阻止请求处理,请参阅:How does spray.routing.HttpService dispatch requests?

排除故障的一些想法:1)衡量处理单个请求所需的时间,并了解它如何扩展 - 您是否有资源争用? 2)检查您的网络和客户端是否不会导致超时 - 它们的超时应该高于服务器。

答案 1 :(得分:1)

感谢您的合作和时间Aleksey Izmailov

咨询Spray Documentation,我必须设置参数spray.can.server timeout-timeout = 500s,使用此配置,服务器有500个接受完成请求的响应。如果没有respose,服务器会发送一条错误消息来完成请求。

执行此操作的目的是对服务器进行超时以接收答案,因此您已完成应用程序的等待时间(在我的情况下为request-timeout = 30 s)。如果此时间(timeout-timeout)满足并且肯定没有响应,则服务器最终结束请求。

spray.can {
  server {
    ...
    timeout-timeout = 500 s
    ...
    }
  }