google文档
Http2
- http2 引入了不兼容的new binary framing layer, 所以大版本号增加了
- 除非使用tcp socket,否则看不出http的版本区别 (指Http向上层暴露的API接口一致,内部实现不同)
- 基于SPDY
SPDY
google在2009年弄的项目,最大的目标是减少 减低网页加载延迟,通过解决http1.1 性能瓶颈
- Target a 50% reduction in page load time (PLT).
- Avoid the need for any changes to content by website authors.
- Minimize deployment complexity, and avoid changes in network infrastructure.
- Develop this new protocol in partnership with the open-source community.
- Gather real performance data to (in)validate the experimental protocol.
通过新的 binary framing layer来实现 请求和相应的 多路复用(multiplexing)、优先级排序(prioritization)、首部压缩
最终提高了55%的速度。
2015年早起,http 2协议被IESG(Steering 指导、操控)通过。
版本比较
http 1.*
- client需要多个connection,来实现 并发(concurrency)、减低延迟(reduce latency)
- 不对request、response的首部压缩,浪费流量
- 不支持对资源的优先级排序,导致对tcp连接的利用新能底下
http 2
- Binary framing layer
- 首部压缩
- 允许在 同一 connection上多concurrent(并发)交换信息
- 特别是: 它允许在同一连接上交错请求和响应消息,并对HTTP头字段使用高效的编码。
- 允许对请求进行 优先级排序
- push
比较
- 使用更少的tcp connection,降低了 资源的竞争、提高了connection的存活时间
- http2通过binary message framing 更高效处理信息
总结
http2是对http的拓展,而不是替代。status code、header等都保持不变,下面讨论关于 底层API的修改。
Binary framing layer (二进制 帧 层)
- http2的核心更改
- 规定http信息的封装 和 传输
- layer:在暴露给应用的 高级http接口 和 底层socket接口之间的 优化的编码机制
- 不同于 http 1.*在普通文本中通过 换行符分割
- http 2通信都被分割为 小的message和frame,并采用 二进制的编码格式
影响
- http 2.0的client、server端必须处理、解析 binary frame格式
- http1.0的client不会理解只支持http2.0的server,反之亦然
- 可幸的是,应用层 对此无感知
术语 (terminology)
- Streams
- 在connection上建立的 双向byte通信流
- 用于传输message
- Messages
- 完整的 一串 frame
- 可以映射逻辑上 request或response信息
- frames
- http2.0中最小的通信单元
- 都包含 frame header
联系
- 所有的通信通过 一个tcp链接完成。
- 一个tcp链接可carry(进行、搬运) 多个双向的 Stream
- 每个Stream都有 独有的识别信息 和 可选的优先级信息
- Stream用于carry 双向的message
- 每个Message,都代表逻辑上的 Http message(request or response)
- 每个Message,都包含一个或多个frame
- frame是最小的通信单元,用于carry指定的数据类型(例如:header、 部分message等等)
- 不同stream的frame,可以在connection中交错传输,最后通过frame的header里嵌入的 stream 识别信息 来重新组装。
小结
- http2.0 将 Http协议的通信 分解为 binary-encoded frames的交换。
- 将其映射为 属于指定stream的 message
- 在一个TCP连接上 通过 多路复用 通信
Request and response multiplexing
Http1.*
- 如果想 多路 并行(parallel)请求 提高效率
- 必须需要 多个TCP connection (例如加载网页时,多线程加载 图片、JS、CSS、html)
- 这是Http1.*的交付模型直接导致的
- 在任意时间,只允许一个response在一个connection内传递
- 导致 head-of-line blocking,和底层TCP connection的浪费
- 因顺序完成,所以相同Connection内,head头阻塞,后续信息就无法传递了
Http 2
- 在new binary framing layer中打破,一个connection同时只能有一个response的限制
- 允许一个connction,完全的 多路request、response
- 将一个http信息分解成独立的 frames
- frames可交叉传递
- 最终frame再组装
HTTP/2最重要的能力
- 将HTTP message分割为独立的frames(帧)
- 可交错传递(interleave them)
- 并在接收端 重新组装frames
由此也引入了其他的好处
- 交叉多个 request, 并行(parallel)处理,而不会被 其中任意一个阻塞
- 交叉多个 response, 并行(parallel)处理,而不会被 其中任意一个阻塞
- 使用 一个 connection,并行 传递多个request、response
- 移走不再需要HTTP/1.x的变通方法(concatenated files, image sprites, and domain sharding)
- 提高网络的可用容量,通过消除不必要的延迟,降低 page 加载时间
好处
- resolves the head-of-line blocking problem found in HTTP/1.x
- eliminates the need for multiple connections to enable parallel processing and delivery of requests and responses
As a result, this makes our applications faster, simpler, and cheaper to deploy.
Stream prioritization
因为multiplex,使得frame的顺序很重要,通过 weight 和 dependence控制:
- 给stream分配weight,区间[1, 256]
- stream可以显式的dependence (依赖) 其他stream
- 依赖可以构成一个树形结构
- 顶部的依赖优先执行
- 顶部为虚拟的一个 root stream
- stream通过 另一个stream唯一识别码 可以指定依赖,将其作为parent,parent将优先执行
- 没指定依赖,则被认为依赖于 root stream(即优先级最高)
- 当dependence相同时,即多个stream 为兄弟 stream,则通过 weight占比,来按比例分配资源
- dependence、weight可以动态调整
One connection per origin
因为 new binary framing 机制,Http 2不再需要多个TCP连接,来达到并发(parallel)处理。
每个stream可以interleave(交替传输)和prioritized(设置优先级)。
因此HTTP/2 连接都是持久的,每个origin(起源)只需一个connection。
- 降低操作的性能消耗
- 降低延迟
- 还提高了server的吞吐量,增大了网络的容量(capacity)
特别是HTTPS,耗性能的 TLS handshacks
- Https的TCP建立,需要TLS握手,用于确定https的证书、非对称加密,生成对称加密的key
Flow control
Server push
打破了 严格的request-response语音,enables one-to-many and server-initiated push workflows(允许1对多 和 服务端推送的工作流)
Header compression
HTTP/1.x
这些元数据都是plain text(纯文本),一次请求要占 500–800 bytes ,如果 HTTP cook被使用还会到 kb,
HTTP/2
使用HPACK压缩格式,压缩 request、response请求头的元数据
HPACK压缩格式:
- It allows the transmitted header fields to be encoded via a static Huffman code, which reduces their individual transfer size.(使用霍夫曼编码,降低单次transfer消耗)
- It requires that both the client and server maintain and update an indexed list of previously seen header fields (in other words, it establishes a shared compression context), which is then used as a reference to efficiently encode previously transmitted values.(采用index,复用相同header)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)