gpt4 book ai didi

java - jetty 不关闭连接

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:10 24 4
gpt4 key购买 nike

我使用 Gradle 创建了一个简单的服务器 Java 应用程序。作为嵌入式服务器,我正在使用 Jetty。我也在使用 Gretty插件,因为它支持最新的 Jetty 版本。

项目运行良好。我试图对其进行压力测试。作为测试的一部分,我需要检查响应时间,因此我通过 curl 发送 "Connection:Close" header 。

我的响应是一个很长的 JSON 字符串,我只看到它的一部分,之后连接挂起。我想知道为什么会这样,我该如何解决。

注意:

  1. 发送 Connection:Keep-alive header 时,一切正常
  2. 当来自服务器的响应不是长字符串,而是更小的字符串时。它工作正常(不会挂起)
  3. 尝试了 gradle 的标准 Jetty 插件,结果是一样的。

如何测试:

  1. 构建并运行 my project从控制台 ./gradlew appRun
  2. 从 bash 控制台运行 curl -H "Connection:Close"-i "http://localhost:8080/Environment/example"
  3. 看到部分响应和连接仍然存在...

最佳答案

您似乎混淆了 HTTP/1.0HTTP/1.1 之间的持久连接模式。

要么,要么您使用的是 curl 的旧版本,它仍然默认为 HTTP/1.0

HTTP/1.0 默认没有持久连接,所以要使用持久连接,我们发送Connection: keep-alive

HTTP/1.1 默认使用持久连接,所以要禁用它我们可以发送 Connection: close

使用 HTTP/1.0,使用 Connection: close 就像发送这个 ...

GET /Environment/example HTTP/1.0
Host: localhost:8080
Connection: close

... 根据 HTTP/1.0 规范

Connection 生成无效的 header 值

让我们使用 curl 的详细功能来查看连接方面真正发生了什么...

示例:HTTP/1.1 正常运行:

$ curl --verbose --http1.1 http://apache.org/ -so /dev/null
* Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:05:39 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:05:39 GMT
< Content-Type: text/html
<
{ [1125 bytes data]
* Connection #0 to host apache.org left intact

注意到它说它保持连接完好无损吗?

示例:HTTP/1.1 手动连接:关闭操作:

$ curl --verbose --http1.1 --header "Connection: close" http://apache.org/ -so /dev/null
* Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: close
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:06:35 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:06:35 GMT
< Connection: close
< Content-Type: text/html
<
{ [1106 bytes data]
* Closing connection 0

啊,HTTP 响应 header 说服务器将关闭,curl 看到连接正在关闭。我们想要的。

示例:HTTP/1.0 正常运行:

$ curl --verbose --http1.0 http://apache.org/ -so /dev/null
* Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:27 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:27 GMT
< Connection: close
< Content-Type: text/html
<
{ [4002 bytes data]
* Closing connection 0

查看 HTTP 响应 header 如何说明服务器将关闭?

Curl 也看到连接被关闭。

这就是我们对正常 HTTP/1.0 操作的期望。

示例:HTTP/1.0 具有持久连接:

$ curl --verbose --http1.0 --header "Connection: keep-alive" http://apache.org/ -so /dev/null
* Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: keep-alive
>
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:37 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:37 GMT
< Keep-Alive: timeout=30, max=100
< Connection: Keep-Alive
< Content-Type: text/html
<
{ [3964 bytes data]
* Connection #0 to host apache.org left intact

是的,服务器表明它也将使用 Keep-Alive(根据 HTTP/1.0 规范),curl 甚至同意并表示连接保持完好。

关于java - jetty 不关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37068638/

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