当前位置: 首页 > news >正文

Apache HttpCore (理解IO基础)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Before Reading

HttpCore 是HTTP 协议所有内容的基本实现,可以用HttpCore 来实现最基本的Http Client 和 Http Server.

  1. 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

  2. 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)

  3. 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

  1. 定义了最基本的消息类型(HTTP messages)包括: HTTP request/response message, Http Entity ,Http Methods 等。
  2. HTTP protocol processors 一系列协议拦截器,比如头部包含压缩,那个拦截器就会处理并对实体进行压缩处理。
  3. 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

此图是引用的上面的url

关于Reactor 模式 这个可以参考这里Reactor 模式详解

后续内容参考官方文档,或者参考我的下一篇博客有具体是源码解读(正在写),这里就介绍这么多。

###Conclusion HttpCore实现了Block IO模型和NIO模型的一些基础封装,Java NIO 和 Reactor模型的结合就是现在在用的大多数高性能的Http Client 和Server的模型 也可以参考这篇文章,Netty也是用的同样的模型 Netty系列之Netty高性能之道,理解HTTP协议还有TCP协议之后,再加上这些设计,基本上就是Java IO的全部了,这里没有讲到文件IO,暂略。

转载于:https://my.oschina.net/tigerlene/blog/823776

相关文章:

  • 启动eclipse时出现“Failed to load the JNI shared library jvm.dll”错误及解决
  • 软件项目技术点(3)——多画布职责分离
  • 浅尝springboot中的Actuator包(一)
  • 【使用教程】论Windows下必备的抓包工具Fiddler2如何安装证书(查看Https)
  • RPC学习
  • js动画(三)
  • Django admin 自定制
  • B4X 大疆 dji开发
  • 微软Azure首席架构师John Gossman就微软加入Linux基金会一事答疑
  • mysql5.5以上my.ini中设置字符集
  • Codeforces 758A Holiday Of Equality
  • redhat配置caffe
  • CCF201312-1 出现次数最多的数(100分)
  • 如何在Flutter工程中添加Android AAR文件
  • Confluence-企业知识管理与协同软件安装步骤
  • hexo+github搭建个人博客
  • #Java异常处理
  • 30秒的PHP代码片段(1)数组 - Array
  • Bootstrap JS插件Alert源码分析
  • chrome扩展demo1-小时钟
  • js
  • Redis字符串类型内部编码剖析
  • Vue实战(四)登录/注册页的实现
  • 大型网站性能监测、分析与优化常见问题QA
  • 代理模式
  • 解析 Webpack中import、require、按需加载的执行过程
  • 深入浏览器事件循环的本质
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 用jQuery怎么做到前后端分离
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • #前后端分离# 头条发布系统
  • (javascript)再说document.body.scrollTop的使用问题
  • (ZT)出版业改革:该死的死,该生的生
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (二)hibernate配置管理
  • (分享)自己整理的一些简单awk实用语句
  • (理论篇)httpmoudle和httphandler一览
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (转)http-server应用
  • (轉)JSON.stringify 语法实例讲解
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .bat批处理(一):@echo off
  • .net6+aspose.words导出word并转pdf
  • .net项目IIS、VS 附加进程调试
  • [ vulhub漏洞复现篇 ] AppWeb认证绕过漏洞(CVE-2018-8715)
  • [CF482B]Interesting Array
  • [Deep Learning] 神经网络基础
  • [Excel] vlookup函数
  • [hive] sql中distinct的用法和注意事项
  • [Oh My C++ Diary]内联函数
  • [Oracle]4--查询操作