Before Reading
HttpCore 是HTTP 协议所有内容的基本实现,可以用HttpCore 来实现最基本的Http Client 和 Http Server.
-
HttpCore 提供的功能
A consistent API for building client / proxy / server side HTTP services
A consistent API for building both synchronous and asynchronous HTTP services
A set of low level components based on blocking (classic) and non-blocking (NIO) I/O models
-
HttpCore 职责范围(设计目标)
Implementation of the most fundamental HTTP transport aspects
Balance between good performance and the clarity & expressiveness of API
Small (predictable) memory footprint
Self-contained library (no external dependencies beyond JRE)
-
What HttpCore is NOT
NOT A replacement for HttpClient, More About Apache HttpClient
NOT A replacement for Servlet APIs, (Servlet 是基于 Java 技术的 web 组件,容器托管的,用于生成动态内容。像其他基于 Java 的组件技术一样,Servlet 也是基于平台无关的 Java 类格式,被编译为平台无关的字节码,可以被基于 Java 技术的 web server 动态加载并运行。容器,有时候也叫做 servlet 引擎,是 web server 为servlet 功能扩展的部分。客户端通过 Servlet 容器实现的请求/应答模型与 Servlet 交互。)
Fundamentals
- 定义了最基本的消息类型(HTTP messages)包括: HTTP request/response message, Http Entity ,Http Methods 等。
- HTTP protocol processors 一系列协议拦截器,比如头部包含压缩,那个拦截器就会处理并对实体进行压缩处理。
- HTTP execution context
Originally HTTP has been designed as a stateless, response-request oriented protocol. However, real world applications often need to be able to persist state information through several logically related request-response exchanges. In order to enable applications to maintain a processing state HttpCpre allows HTTP messages to be executed within a particular execution context, referred to as HTTP context. Multiple logically related messages can participate in a logical session if the same context is reused between consecutive requests. HTTP context functions similarly to a java.util.Map<String, Object>. It is simply a collection of logically related named values. Please nore HttpContext can contain arbitrary objects and therefore may be unsafe to share between multiple threads. Care must be taken to ensure that HttpContext instances can be accessed by one thread at a time.
Blocking IO modle
- 什么是阻塞IO?
Blocking (or classic) I/O in Java represents a highly efficient and convenient I/O model well suited for high performance applications where the number of concurrent connections is relatively moderate. Modern JVMs are capable of efficient context switching and the blocking I/O model should offer the best performance in terms of raw data throughput as long as the number of concurrent connections is below one thousand and connections are mostly busy transmitting data. However for applications where connections stay idle most of the time the overhead of context switching may become substantial and a non-blocking I/O model may present a better alternative.
简单来讲就是当IO阻塞的时候,JVM通过切换线程来执行IO操作,适合并发量适中,并且请求不会阻塞太久的情况(1 Thread 1 Connection )。
Asynchronous I/O based on NIO
Asynchronous I/O model may be more appropriate for those scenarios where raw data throughput is less important than the ability to handle thousands of simultaneous connections in a scalable, resource efficient manner. Asynchronous I/O is arguably more complex and usually requires a special care when dealing with large message payloads.
异步IO可以提高并发量和吞吐量,同时需要考虑处理消息体很大的情况。
之前的同步IO 用Per Thead Per Connection的模型,异步IO 使用的Java NIO package, NIO 又是基于操作系统的实现,所以如果想要深入理解的话建议去看《UNIX 网络编程》,不过理解了Java的NIO也差不多理解了IO操作了。
Java NIO
这里这篇文章对JAVA IO 做了整体的介绍 深入分析 Java I/O 的工作机制
下面是java nio package的对NIO的一个介绍
Defines buffers, which are containers for data, and provides an overview of the other NIO packages. The central abstractions of the NIO APIs are:
Buffers, which are containers for data; Charsets and their associated decoders and encoders, which translate between bytes and Unicode characters;
Channels of various types, which represent connections to entities capable of performing I/O operations;
Selectors and selection keys, which together with selectable channels define a multiplexed, non-blocking I/O facility.
这里补充一点关于操作系统IO通道的概念,
操作系统通过IO通道(也叫IO处理器)来执行IO操作,处理器只有IO操作开始和结束的时候被中断,而其他时间就由IO通道来处理,操作系统也是通过增加缓冲区的办法来提高使用率(进程可以被换出,并且不用等待IO操作),所以Java NIO也是采用类似的方案。
NIO带来异步高效的IO操作,同时JDK7 的Future 和CallBack 在 Java 7 中体会 NIO.2 异步执行的快乐 ,JAVA8 的ComplatebleFuture等API的增强JVM 并发性: Java 8 并发性基础,使IO编程操作更加便捷
IO Reactor
HttpCore NIO is based on the Reactor pattern as described by Doug Lea. The purpose of I/O reactors is to react to I/O events and to dispatch event notifications to individual I/O sessions. The main idea of I/O reactor pattern is to break away from the one thread per connection model imposed by the classic blocking I/O model. The IOReactor interface represents an abstract object which implements the Reactor pattern. Internally, IOReactor implementations encapsulate functionality of the NIO java.nio.channels.Selector.
I/O reactors usually employ a small number of dispatch threads (often as few as one) to dispatch I/O event notifications to a much greater number (often as many as several thousands) of I/O sessions or connections. It is generally recommended to have one dispatch thread per CPU core.
IO Reactor也就是HttpCore实现异步IO的最主要部分,使用Reactor模型和JAVA NIO ,模型和这里面的图基本一致Scalable IO in Java
关于Reactor 模式 这个可以参考这里Reactor 模式详解
后续内容参考官方文档,或者参考我的下一篇博客有具体是源码解读(正在写),这里就介绍这么多。
###Conclusion HttpCore实现了Block IO模型和NIO模型的一些基础封装,Java NIO 和 Reactor模型的结合就是现在在用的大多数高性能的Http Client 和Server的模型 也可以参考这篇文章,Netty也是用的同样的模型 Netty系列之Netty高性能之道,理解HTTP协议还有TCP协议之后,再加上这些设计,基本上就是Java IO的全部了,这里没有讲到文件IO,暂略。