【问题标题】:Jetty doesn't close connection码头没有关闭连接
【发布时间】:2016-09-01 07:21:02
【问题描述】:

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

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

我的响应是一个长 JSON 字符串,我只看到其中的一部分,之后连接挂起。我想知道为什么会发生这种情况,我该如何解决。

注意:

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

如何测试:

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

【问题讨论】:

  • 为什么您认为需要发送Connection: close 来查看响应时间?

标签: java gradle jetty gretty


【解决方案1】:

您似乎混淆了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 生成无效的标头值

让我们使用 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 与手动Connection: close 操作:

$ 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 响应标头说服务器将关闭,并且 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 响应标头如何表示服务器将关闭?

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 甚至同意并说连接保持不变。

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2019-06-26
    • 1970-01-01
    • 2018-02-11
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多