Tomcat is JavaWeb component architecture in an apache server open-source software, through its study, summarize and share knowledge about it, ppt below to share content, we hope to help people want to know tomcat.
Tomcat history
- In 1999 the first version (3.x), based on Sun contributed code to achieve Servlet2.2 and JSP1.1
- In 2001 Milestone version 4.0 specification to achieve Servlet2.3 and JSP1.2
- There are five major current Tomcat version:
- 6.x: JDK5 more
- 7.x: JDK6 more
- 8.x: JDK7 more
- 8.5x: JDK7 above, released after 9.0, for 8-substituted, to achieve some characteristic 9
- 9.x: JDK8 more
Tomcat license
- Apache License issued to licensing
- free
- Edit and publish the user does not change the version of the open-source modification section
- Modify the tomcat user does not have to change donated to the Apache Software Foundation
- All re-release includes source code and binaries are included Apache license
- Any information must be re-released Apache Foundation approved
- None Apache Software Foundation license and products derived from the Tomcat source code can not be named Tomcat, Jakarta, Apache
Tomcat installation and startup
- Installation on Windows
- .zip
- .exe
- Installation on Linux
- apache-tomcat-8.5.6.tar.gz
- Modify $ CATALINA_HOME / bin / catalina.sh modify the Tomcat startup configuration: JAVA_OPTS
Use the embedded Tomcat support
- Tomcat support embedded applications, such as: SpringBoot in tomcat
- Installation of the system in the form of separate packages to provide
- In order to satisfy the PAAS application development environment, delivery and management.
- Bind a port for the HTTP export service
Tomcat Debug start
- bin>catalina jpda start
- JPDA depend on the JDK
- The default debug port: 8000
Tomcat main directory structure
New features after Tomcat8.5
- Servlet3.1, JSP2.3, EL3.0, WebSocket1.1,9.0 began to support Servlet4.0
- Servlet4.0预览API
- 默认的HTTP、AJP链接器采用NIO,而非BIO
- 全新的资源实现
- 链接器新增支持JDK7的NIO2
- 链接器新增支持HTTP/2协议
- 默认采用异步日志处理方式
- 大量代码重构
Tomcat总体架构设计
Tomcat启动流程
Tomcat请求处理流程
Tomcat类加载机制
- 从缓存中加载
- 如果没有,从JVM的Bootstrap类加载器加载
- 如果没有,则从当前类加载器加载(WEB-INF/classes 、WEB-INF/lib的顺序)
- 如果没有,则从父类加载器加载,由于父类加载器采用默认的委派模式,所以加载顺序为System、Common、Shared
- 如果启用Java委派模式(delegate=true):缓存-Bootstrap-从父类(System、common、shared)-当前类加载器加载
组件Catalina
- 包含所有的容器组件
- 通过松耦合方式集成Cotoye
- 包含启动入口以及Shell
- 使用Apache Digester解析XML配置文件并创建服务器
Catalina依赖关系:
Coyote组件:
- Tomcat链接器名称
- 外部访问接口服务
- 建立链接,发送请求,并接收响应
- 封装底层网络通信协议,为Catalina提供接口,使之与请求协议及I/O方式解耦
- 将Socket输入转换为Request交由Catalina处理
- 提供Response对象将Catalina的处理结果写入输出流
- 此处的Request与Response与Servlet还未关联
Coyote与Catalina交互关系:
Cotoye支持的协议
- HTTP/1.1
- AJP
- HTTP/2.0
- 8.5/9之后移除BIO支持
- NIO java类库实现
- NIO2 JDK7最新的NIO2
- APR(跨平台统一API工具) 采用Apache可移植运行库实现APR是使用C/C++编写的本地库
Jasper:
- 从Tomcat4.1开始重新设计了Jasper2
- 运行时编译
- 预编译:提供JspC用于支持JSP预编译
- 转换为Servlet将页面内容输出
JVM配置项
- 例:JAVA_OPTS= “-server -Xms1024m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m”
- Xms:堆内存的初始大小
- Xmx:堆内存上限
- XX:PermSize:非堆内存初始大小(JDK8中改为:-XX:MetaspaceSize)
- XX:MaxPermSize:非堆内存上限,(JDK8:-XX:MaxMetaspaceSize)
- 涉及:性能、调试、JVM行为、垃圾回收、可按需添加详见Oracle官网文档
系统属性配置
- 通过-Dproperty命令指定系统属性
- 涉及:属性替换、集群、EL、Jasper、安全、规范、
- 会话、日志、jar扫描等
服务器配置
- catalina.properties
- server.xml
- context.xml
Web应用配置
- SerlverContext初始化参数
- 会话配置
- Cookie
- URL
- SSL
- Servlet声明及映射
- 生命周期监听器
- Filter定义及映射(比如:CorsFilter)
- MIME类型映射
- 欢迎页列表、错误页列表、本地化及编码映射
- 安全配置
- JNDI配置以及其他配置
Web服务器集成
- 静态资源优化
- 多应用、虚拟机整合
- 负载均衡
- 复合场景
- Apache HTTP Server
- Ngnix(高并发、延迟处理、SSL、静态内容压缩缓存、请求限流、HTTP流媒体)
其它Tomcat内容
- 集群
- 可伸缩、高可用、高性能
- 使用Apache Tribes通信模块,
- 会话同步
- 安全
- server.xml移除不必要组件、修改关键配置如shutdown端口
- 应用安全
- 传输安全:SSL ,TLS
- catalina.opolicy
- 性能调优
- 测试与诊断工具
- 配置优化
- Appache Bench、JMeter
- Linux:nload、bmon slurm 套接字:iftop iptraf tcptrack pktstat netwatch等
参考资料:
https://book.douban.com/subject/27034717/
完