4

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

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

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

笔记 :

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

如何测试:

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

1 回答 1

3

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

要么,要么你使用的是一个非常旧的版本curl,仍然默认为HTTP/1.0.

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

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

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

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

Connection...根据HTTP/1.0规范产生无效的标头值

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

于 2016-05-06T12:13:02.547 回答