简体   繁体   English

HTTP2:内部服务 API 是否应该使用 http2

[英]HTTP2: Should inner service apis use http2

I'm using a Microservice architecture, where one service calls multiple services at a time with servers on nodejs我正在使用微服务架构,其中一个服务一次调用多个服务,服务器在 nodejs

I was planning to use HTTP2 for API calls from one service to another, as it makes use of just one TCP connection with header compression.我计划将 HTTP2 用于从一个服务到另一个服务的 API 调用,因为它仅使用一个 TCP 连接和 header 压缩。

However, HTTP2 needs TLS support, which implies a TLS handshake for every API call made by service to others, adding overhead roundtrips.但是,HTTP2 需要 TLS 支持,这意味着服务向其他人发出的每个 API 调用都要进行 TLS 握手,从而增加了往返开销。

Although TLS1.3 takes only one round trip, still it adds up some extra overhead time.尽管 TLS1.3 只需要一次往返,但它仍然会增加一些额外的开销时间。

My question is, Is it a good idea to use HTTP2 in the first place for API calls from one service to another, or it's better to continue with HTTP1.1我的问题是,对于从一个服务到另一个服务的 API 调用,首先使用 HTTP2 是个好主意,还是继续使用 HTTP1.1 更好

HTTP2 most likely won't be more performant than ordinary HTTP1.1. HTTP2 很可能不会比普通的 HTTP1.1 性能更高。 It's only faster when you compare them in the context of HTTPS and parallel requests.只有在 HTTPS 和并行请求的上下文中比较它们时才会更快。 HTTP2 allows to re-use the same TLS handshake, as well as use the same connection for multiple parallel requests (multiplex). HTTP2 允许重复使用相同的 TLS 握手,以及对多个并行请求(多路复用)使用相同的连接。

This is the reason you wouldn't set up HTTP2 between nginx and your app server - as you usually don't need TLS between them.这就是您不会在 nginx 和您的应用服务器之间设置 HTTP2的原因 - 因为您通常不需要它们之间的 TLS。 So unless a) you need a secure connection between your services and b) you're planning to issue parallel requests - it doesn't seem to make sense to use HTTP2 for service-to-service communication.因此,除非 a) 您需要在服务之间建立安全连接,并且 b) 您计划发出并行请求 - 使用 HTTP2 进行服务到服务通信似乎没有意义。

HTTP/2 does not require TLS support. HTTP/2 不需要 TLS 支持。

It just so happen that all browser vendors (and only them) decided to not support clear-text HTTP/2, but other clients such as curl , or Java clients, etc. do support clear-text HTTP/2.碰巧所有浏览器供应商(而且只有他们)决定不支持明文 HTTP/2,但其他客户端(例如curl或 Java 客户端等)确实支持明文 HTTP/2。

However, for server-to-server communication, it is possible to use clear-text HTTP/2, and in fact this is a very common deployment.但是,对于服务器到服务器的通信,可以使用明文 HTTP/2,实际上这是一种非常常见的部署。

It is also unfortunate that popular servers that are used as reverse-proxies or load balancers do not support invoking the back-end using HTTP/2, but that's just an implementation limitation.不幸的是,用作反向代理或负载均衡器的流行服务器不支持使用 HTTP/2 调用后端,但这只是一个实现限制。

HAProxy , for example, allows to offload TLS and then invoke the back-end with clear-text HTTP/2.例如, HAProxy允许卸载 TLS,然后使用明文 HTTP/2 调用后端。

If you receive many multiplexed requests on the front-end, you can leverage HTTP/2 multiplexing towards the back-end, saving a lot of resources.如果你在前端收到很多多路复用的请求,你可以在后端利用 HTTP/2 多路复用,节省大量资源。 A web page that requests say 30 multiplexed resources to the front-end will need to open or otherwise use 30 different connections to the back-end when using HTTP/1 (or less connections and be less efficient), versus using just 1 when using HTTP/2.一个 web 页面请求说 30 个多路复用资源到前端,在使用 HTTP/1 时需要打开或以其他方式使用 30 个到后端的不同连接(或更少的连接并且效率较低),而在使用时仅使用 1 个HTTP/2。 When using HTTP/2 towards the back-end, you are not limited to just 1 connection, exactly like you are not limited to use only 6 connections when using HTTP/1 (these are just browser limits and do not apply to server-to-server communication).在后端使用 HTTP/2 时,您不仅限于 1 个连接,就像使用 HTTP/1 时不限于仅使用 6 个连接一样(这些只是浏览器限制,不适用于 server-to -服务器通信)。

Furthermore, back-end applications that use HTTP/2 features such as HTTP/2 push (albeit now being phased out) would work transparently using pure HTTP/2 communication, while won't be able to use the HTTP/2 features when using HTTP/1 communication towards the back-end.此外,使用 HTTP/2 功能(例如 HTTP/2 推送)的后端应用程序(尽管现在正在逐步淘汰)将使用纯 HTTP/2 通信透明地工作,而在使用时将无法使用 HTTP/2 功能面向后端的 HTTP/1 通信。 This is true for any other HTTP/2 feature such as PING frames, SETTINGS frames, etc. all of which are lost when communicating to the back-end application when using HTTP/1.对于任何其他 HTTP/2 功能(例如 PING 帧、SETTINGS 帧等)都是如此。在使用 HTTP/1 与后端应用程序通信时,所有这些功能都会丢失。

From the resources point of view, a smart reverse-proxy or load balancer can just offload TLS, and then blindly forward the HTTP/2 clear-text bytes to the back-end -- no need to interpret the bytes in either direction.从资源的角度来看,智能反向代理或负载均衡器可以卸载 TLS,然后盲目地将 HTTP/2 明文字节转发到后端——无需解释任一方向的字节。 There would be no need to parse the HTTP/2 request, convert it to the HTTP/1 format and viceversa when receiving the HTTP/1 response (from HTTP/1 to HTTP/2).在接收到 HTTP/1 响应(从 HTTP/1 到 HTTP/2)时,不需要解析 HTTP/2 请求,将其转换为 HTTP/1 格式,反之亦然。 This causes unnecessary CPU burning and TCP connection usage.这会导致不必要的 CPU 烧毁和 TCP 连接使用。

So, in principles HTTP/2 towards the back-end is very desirable.因此,原则上 HTTP/2 面向后端是非常可取的。

I have worked with clustered systems where HTTP/2 for server-to-server communication was a huge benefit just for the many less resources used across the whole cluster.我曾使用过集群系统,其中用于服务器到服务器通信的 HTTP/2 是一个巨大的好处,只是因为整个集群使用的资源更少。

The reality, however, is that the most popular front-end servers do not support HTTP/2 towards the back-end (mostly for non-technical reasons), so most of the times you just have to give up and deploy sub-optimal systems.然而,现实情况是,最流行的前端服务器不支持后端的 HTTP/2(主要是出于非技术原因),所以大多数时候你只需要放弃并部署次优系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM