当使用 HTTP/2 协议时,我如何传送 Keep-alive 元数据?

How do i convey Keep-alive metadata when the HTTP/2 protocol is used?

我有一个 angular Web 应用程序,它使用 Spring 启动,后端有一个嵌入式 tomcat 服务器。我想让已建立的 http 连接保持更长时间,以提高后续 http 请求的响应时间。使用 http/1.1 时,浏览器被告知通过向 response header 添加 Connection: Keep-Alive 和类似 Keep-Alive: timeout=5, max=1000 的内容来保持 http 连接。但是 connection-specific header 字段,例如 Connection 和 Keep-Alive are prohibited in HTTP/2. Because of that Chrome and Firefox ignore them in HTTP/2 responses. With HTTP/2, connection-specific metadata should be conveyed by other means.

不过,我在任何地方都找不到那些 'other means' 应该是什么。我也无法在任何地方找到如何配置嵌入式 Tomcat 9 服务器以将 Keep-alive 元数据添加到 HTTP/2 响应。 tomcat 现在是这样配置的:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
                    .getProtocolHandler();
            protocolHandler.setDisableUploadTimeout(false);
            protocolHandler.setConnectionUploadTimeout(5000);
            protocolHandler.setKeepAliveTimeout(4000);
            protocolHandler.setMaxKeepAliveRequests(200);
            protocolHandler.setUseKeepAliveResponseHeader(true);
        }
    });
}

但是如果我想使用 HTTP/2,这些设置将不起作用。有什么想法吗?

With http/1.1 a browser is told to keep the http connection alive by adding Connection: Keep-Alive and something like Keep-Alive: timeout=5, max=1000 to the response header.

事实并非如此。由于 HTTP/1.1,除非明确设置为 Close,否则 Connection: Keep-Alive header 默认为这种情况。我们甚至有 header 的事实更多是 HTTP/1.0 天遗留下来的,不需要设置。响应总是提供更多关于服务器将做什么的信息,而不是给客户端的指令(“嘿,仅供参考,我将使用这些设置”)但它也总是有点毫无意义,因为服务器(和客户端)如果需要,也可以断开连接。如果一台服务器 运行 没有 TCP 连接,它不应该真正保留未使用的旧连接而不是接受新连接,一般来说。

However connection-specific header fields such as Connection and Keep-Alive are prohibited in HTTP/2. Because of that Chrome and Firefox ignore them in HTTP/2 responses. With HTTP/2, connection-specific metadata should be conveyed by other means.

I can't find anywhere what those 'other means' should be though. Nor can i find anywhere how to configure an embedded Tomcat 9 server to add Keep-alive meta data to a HTTP/2 response. This is how tomcat is configured right now:

与之前一样,HTTP/2 只是去掉了相当无意义的 header 并假定您想让它保持活动状态。它在 HTTP/2 中也不太有意义,因为它用于多个请求和响应,而 HTTP/1.1 一次仅用于一个,而 keep-alive 是一个 connection-level 设置。因此,如果一个 HTTP/2 请求可以指定一个 keep-alive: close,那么对于该连接上已经是 in-flight 的其他请求意味着什么?同样地,将其设置为 keep-aliveHTTP/2’s multiplexing.

的本质所暗示的

由客户端和服务器根据自己的逻辑和资源限制决定何时最好断开连接,尽管 HTTP/2 spec does go on to say this later:

HTTP/2 connections are persistent. For best performance, it is expected that clients will not close connections until it is determined that no further communication with a server is necessary (for example, when a user navigates away from a particular web page) or until the server closes the connection.