[spring-projects/spring-boot]server.connection-timeout 默认值

2018-02-08 492 views
2
    /**
     * Time in milliseconds that connectors will wait for another HTTP request before
     * closing the connection. When not set, the connector's container-specific default
     * will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
     */
    private Integer connectionTimeout;

我不知道这是一个错误还是一个功能。但是,我肯定觉得这是不合理的,在tomcat中,默认的connectionTimeout值是2000。然而在spring boot中,他没有默认值,导致系统出现大量的TIME_WAIT。我觉得自动传输的时候也应该给一个默认值。不为空或 0。

回答

5

正如您引用的评论中所述,如果server.connection-timeout未设置,则嵌入式容器将使用连接器的默认超时。如果您看到不同的行为,请提供重现问题的完整示例。此时我们甚至不知道您使用的是哪个版本的 Boot。

9

@wilkinsona 我的 Spring Boot 版本是 1.5.9.RELEASE

7

不幸的是,这只是我们需要知道的一部分。正如我上面所说,如果您看到不同的行为,请提供重现问题的完整示例。

3

@wilkinsona你好,要重现问题很简单,只要你创建一个spring boot程序,使用默认的tomcat配置,部署,将你的项目然后使用nginx来代理这个项目,当然要开启keepalive然后使用nginx、ab命令对你的项目进行压力测试,你会发现spring boot内置的tomcat中出现了time_wait。

2

套接字处于 in 状态TIME_WAIT表明套接字已关闭并且 TCP 堆栈正在等待任何其他数据包。我将关闭这个问题,因为我认为 Spring Boot 没有问题。具体来说,我没有看到任何表明 Tomcat 的连接被配置为无限期超时的信息。

5

当客户端发送 header keepalive 时,spring boot tomcat 不应在 20 秒内关闭套接字,20 秒是 tomcat 中的默认值。但 spring boot tomcat 不包含默认值。该值为 6 秒?

5

Tomcat 的默认值为 60 秒。 Spring Boot将使 Tomcat 使用默认值,除非您已配置它。我刚刚使用我们的一个示例验证了这种行为。

如果您希望有人花更多时间查看此内容,那么您需要提供一个小示例,该示例显示 Spring Boot 使用默认超时以外的超时配置 Tomcat。

3

tomcat conf/server.xml,默认超时时间为20000

    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
0

这是我的样本

https://github.com/hellojukay/springboot-sample

这是我的 nginxconf,启用 nginx keepalive


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  120s 120s;
    keepalive_requests 10000;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

#        location / {
#           root   html;
#          index  index.html index.htm;
#     }
       root /Users/jukay;
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /hello {
          proxy_pass http://127.0.0.1:7777;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

这是我的情况,7777端口释放连接太快,我认为连接状态不应该是TIME_WAIT,这意味着tomcat快速关闭连接而不等待默认超时。看看这张动图,git

8

这与 Spring Boot 无关。您正在使用curl,它会关闭退出时打开的所有连接。

0

@wilkinsona 不,不,不

您也可以使用 ab -k 来请求连接 TIME_WAIT 。

0

默认情况下,nginx 将使用协议 http/1.0 与您的 spring boot 应用程序连接,这将导致 nginx 和 tomcat 之间出现大量短连接。大量的短连接(频繁地打开tcp和关闭tcp)会产生大量的TIME_WAIT。