gpt4 book ai didi

java - 连接重置使用 REST 服务(scala/spray)

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:58 27 4
gpt4 key购买 nike

我在向休息服务发送并发请求时遇到问题;客户端 (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

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

最佳答案

您的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) 检查您的网络和客户端不会导致超时 - 它们的超时应该高于服务器的。

关于java - 连接重置使用 REST 服务(scala/spray),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36487853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com