Spring Boot应用程序的保持活动配置

8
我正在尝试修复/调试一个使用嵌入式Tomcat的Spring Boot Web应用程序中太多关闭连接的问题。问题出现是因为它关闭了应该保持活动的连接。
现在,我发现Tomcat有一个配置,限制了保持活动连接的数量(请参见https://tomcat.apache.org/tomcat-8.5-doc/config/http.html中的maxKeepAliveRequests),可能还有其他与该问题相关的配置。但我的问题是,我不知道这些参数在哪里给出,或者如果使用默认值,我如何更改它们。
我的问题:在哪里可以找到解释如何配置Spring Boot /嵌入式Tomcat保持活动参数的文档,并且这些参数是什么?
4个回答

3

并不是所有的Tomcat属性都可以通过属性文件进行配置。 keep-alive 相关属性是其中之一,这意味着它们只能通过编程方式进行配置。 通过配置 WebServerFactoryCustomizer bean来实现此目的。您可以使用协议处理程序来设置 KeepAlive 设置。以下是一个示例:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
                    .getProtocolHandler();
            protocolHandler.setKeepAliveTimeout(80000);
            protocolHandler.setMaxKeepAliveRequests(500);
            protocolHandler.setUseKeepAliveResponseHeader(true);
        }
    });
}

要了解这些设置的更多信息,请阅读Tomcat 9配置参考


2
我不再遇到这个问题,也无法测试它,但这似乎是一个很好的解决方案。所以我会选择它。谢谢。 - Juh_

0
在Dina Bogdan分享的文档中发现了this,我被告知这些属性因Spring Boot版本不同而有所差异。

2
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community
此外,您的答案应该是自包含的,链接只是提供额外细节或参考链接。 - Juh_

0

控制Tomcat保持连接的Spring Boot属性为:

server.tomcat.max-keep-alive-requests=100

保持连接会话的数量,默认为100

server.tomcat.keep-alive-timeout=60000

保持每个会话活动的毫秒数

-2

这些是Tomcat服务器的配置属性:

server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=10s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}\\
        0:0:0:0:0:0:0:1\\
        ::1 # Regular expression that matches proxies that are to be trusted.
server.tomcat.max-connections=10000 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-post-size=2MB # Maximum size of the HTTP post content.
server.tomcat.max-swallow-size=2MB # Maximum amount of request body to swallow.
server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.allow-caching=true # Whether static resource caching is permitted for this web application.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

更多细节请查看这里


4
关于保持连接的部分有没有提到? - Juh_
似乎与保持连接无关,但您可以深入研究该文档,也许会发现一些有用的内容,或者思考如何以解决问题的方式使用这些属性。 - Dina Bogdan
你知道哪个源代码读这些参数并将它们传递给Tomcat吗?这样我就可以直接去看看。 - Juh_
3
这个回答并没有回答问题中所问的"保持连接"的问题,它只是列出了所有属性的摘要。 - Harsh Vardhan Ladha
我同意,这些属性只会让我们更加困惑。 - Guilherme Guini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接