初探Tomcat的架构设计_ide
Tomcat 作为 servlet 容器实现,它是基于 Java 语言开发的轻量级应用服务器。因为 Tomcat 作为应用服务器,它有着完全开源,轻量,性能稳定,部署成本低等优点,所以它成为目前 Java 开发应用部署的首选,几乎每个Java Web开发者都有使用过,但是,你对 Tomcat 的整体设计有进行过了解和思考吗?
本文将基于 Tomcat8 进行分析,具体版本为 Tomcat8 当前官网最新修改(2019-11-21 09:28)的版本 v8.5.49总体结构 Tomcat 的总体结构中有很多模块,下图列出我们将要进行分析结构中的主要模块。其中主要分析的是Service,Connector,Engine,Host,Context,Wrapper。为避免图层看着太乱,下图中 n代表该组件可允许存在多个。初探Tomcat的架构设计_实例化_02如上图所描述的是:Server 是 tomcat 服务器,在 Server 中可以存在多个服务 Service 。每个服务中可有多个连接器和一个 Servlet 引擎 Engine,一个 Service 中多个连接器对应一个 Engine。每个 Engine 中,可存在多个域名,这里可用虚拟主机的概念来表示 Host。每个 Host 中可以存在多个应用 Context。Server,Service,Connector,Engine,Host,Context,Wrapper 它们之间的关系,除了Connector和Engine,它们是平行关系,其它的都是存在包含关系。同时,它们也都继承了 Lifecycle 接口,该接口提供的是生命周期的管理,里面包括:初始化(init),启动(start),停止(stop),销毁(destroy)。当它的父容器启动时,会调用它子容器的启动,停止也是一样的。初探Tomcat的架构设计_perl_03上图中,还可以看到,Engine,Host,Context,Wrapper 都继承自 Container。它有个 backgroundProcess()方法,后台异步处理,所以继承它后可以方便的创建异步线程。在 Tomcat7 中,有看到 Service 持有的是 Container,而不是 Engine。估计这也是为什么在当前版本中添加 Engine 方法名叫 setContainerServer Tomcat 源码中有提供 org.apache.catalina.Server接口,对应的默认实现类为 org.apache.catalina.core.StandardServer,接口里面提供有如下图方法。初探Tomcat的架构设计_apache_04上图中可以知道 Server 做的工作:对 Service,Address,Port,Catalina 以及全局命名资源的管理操作。Server 在进行初始化的时候,会加载我们 server.xml 中配置的数据。初探Tomcat的架构设计_perl_05这里对其中的 Service 操作的 addService向定义的服务集添加新服务进行分析:
  1. // 保存服务的服务集

  2. privateService services[] = newService[0];

  3.  

  4. finalPropertyChangeSupport support = newPropertyChangeSupport(this);

  5.  

  6. @Override

  7. publicvoid addService(Service service) {

  8. // 相互关联

  9. service.setServer(this);

  10.  

  11. // 利用同步锁,防止并发访问 来源:https://ytao.top

  12. synchronized(servicesLock) {

  13. Service results[] = newService[services.length + 1];

  14. // copy 旧的服务到新的数组中

  15. System.arraycopy(services, 0, results, 0, services.length);

  16. // 添加新的 service

  17. results[services.length] = service;

  18. services = results;

  19.  

  20. // 如果当前 server 已经启动,那么当前添加的 service 就开始启动

  21. if(getState().isAvailable()) {

  22. try{

  23. service.start();

  24. } catch(LifecycleException e) {

  25. // Ignore

  26. }

  27. }

  28.  

  29. // 使用观察者模式,当被监听对象属性值发生变化时通知监听器,remove 是也会调用。

  30. support.firePropertyChange("service", null, service);

  31. }

  32.  

  33. }

源码中可以看到,向服务器中添加服务后,随机会启动服务,实则也服务启动入口。Service Service 的主要职责就是将 Connector 和 Engine 的组装在一起。两者分开的目的也就是使请求监听和请求处理进行解耦,能拥有更好的扩展性。每个 Service 都是相互独立的,但是共享一个JVM和系统类库。这里提供了 org.apache.catalina.Service接口和默认实现类 org.apache.catalina.coreStandardService初探Tomcat的架构设计_tomcat_06在实现类 StandardService 中,主要分析 setContaineraddConnector两个方法。
  1. privateEngine engine = null;

  2.  

  3. protectedfinalMapperListener mapperListener = newMapperListener(this);

  4.  

  5. @Override

  6. publicvoid setContainer(Engine engine) {

  7. Engine oldEngine = this.engine;

  8. // 判断当前 Service 是否有关联 Engine

  9. if(oldEngine != null) {

  10. // 如果当前 Service 有关联 Engine,就去掉当前关联的 Engine

  11. oldEngine.setService(null);

  12. }

  13. // 如果当前新的 Engine 不为空,那么 Engine 关联当前 Service,这里是个双向关联

  14. this.engine = engine;

  15. if(this.engine != null) {

  16. this.engine.setService(this);

  17. }

  18. // 如果当前 Service 启动了,那么就开始启动当前新的 Engine

  19. if(getState().isAvailable()) {

  20. if(this.engine != null) {

  21. try{

  22. this.engine.start();

  23. } catch(LifecycleException e) {

  24. log.error(sm.getString("standardService.engine.startFailed"), e);

  25. }

  26. }

  27. // 重启 MapperListener ,获取一个新的 Engine ,一定是当前入参的 Engine

  28. try{

  29. mapperListener.stop();

  30. } catch(LifecycleException e) {

  31. log.error(sm.getString("standardService.mapperListener.stopFailed"), e);

  32. }

  33. try{

  34. mapperListener.start();

  35. } catch(LifecycleException e) {

  36. log.error(sm.getString("standardService.mapperListener.startFailed"), e);

  37. }

  38.  

  39. // 如果当前 Service 之前有 Engine 关联,那么停止之前的 Engine

  40. if(oldEngine != null) {

  41. try{

  42. oldEngine.stop();

  43. } catch(LifecycleException e) {

  44. log.error(sm.getString("standardService.engine.stopFailed"), e);

  45. }

  46. }

  47. }

  48.  

  49. // Report this property change to interested listeners

  50. support.firePropertyChange("container", oldEngine, this.engine);

  51. }

  52.  

  53. /**

  54. * 实现方式和 StandardServer#addService 类似,不在细述

  55. * 注意,Connector 这里没有像 Engine 一样与 Service 实现双向关联

  56. */

  57. @Override

  58. publicvoid addConnector(Connector connector) {

  59.  

  60. synchronized(connectorsLock) {

  61. connector.setService(this);

  62. Connector results[] = newConnector[connectors.length + 1];

  63. System.arraycopy(connectors, 0, results, 0, connectors.length);

  64. results[connectors.length] = connector;

  65. connectors = results;

  66.  

  67. if(getState().isAvailable()) {

  68. try{

  69. connector.start();

  70. } catch(LifecycleException e) {

  71. log.error(sm.getString(

  72. "standardService.connector.startFailed",

  73. connector), e);

  74. }

  75. }

  76.  

  77. // Report this property change to interested listeners

  78. support.firePropertyChange("connector", null, connector);

  79. }

  80.  

  81. }

Connector Connector 主要用于接收请求,然后交给 Engine 处理请求,处理完后再给 Connector 去返回给客户端。当前使用版本支持的协议有:HTTP,HHTP/2,AJP,NIO,NIO2,APR 主要的功能包括:
  • 监听服务器端口来读取客户端的请求。

  • 解析协议并交给对应的容器处理请求。

  • 返回处理后的信息给客户端

Connector 对应服务器 server.xml 中配置信息的例子:
  1. <Connectorport="8080"protocol="HTTP/1.1"

  2. connectionTimeout="20000"

  3. redirectPort="8443"/>

这里通过配置监听的端口号 port,指定处理协议 protocol,以及重定向地址 redirectPort。协议处理类型通过实例化连接器时设置:
  1. publicConnector() {

  2. // 无参构造,下面 setProtocol 中默认使用HTTP/1.1

  3. this(null);

  4. }

  5.  

  6. publicConnector(String protocol) {

  7. // 设置当前连接器协议处理类型

  8. setProtocol(protocol);

  9. // 实例化协议处理器,并保存到当前 Connector 中

  10. ProtocolHandler p = null;

  11. try{

  12. Class<?> clazz = Class.forName(protocolHandlerClassName);

  13. p = (ProtocolHandler) clazz.getConstructor().newInstance();

  14. } catch(Exception e) {

  15. log.error(sm.getString(

  16. "coyoteConnector.protocolHandlerInstantiationFailed"), e);

  17. } finally{

  18. this.protocolHandler = p;

  19. }

  20.  

  21. if(Globals.STRICT_SERVLET_COMPLIANCE) {

  22. uriCharset = StandardCharsets.ISO_8859_1;

  23. } else{

  24. uriCharset = StandardCharsets.UTF_8;

  25. }

  26. }

  27.  

  28. /**

  29. * 这个设置再 tomcat9 中被移除,改为必配项

  30. */

  31. publicvoid setProtocol(String protocol) {

  32.  

  33. boolean aprConnector = AprLifecycleListener.isAprAvailable() &&

  34. AprLifecycleListener.getUseAprConnector();

  35.  

  36. // 这里指定了默认协议和 HTTP/1.1 一样

  37. if("HTTP/1.1".equals(protocol) || protocol == null) {

  38. if(aprConnector) {

  39. setProtocolHandlerClassName("org.apache.coyote.http11.Http11AprProtocol");

  40. } else{

  41. setProtocolHandlerClassName("org.apache.coyote.http11.Http11NioProtocol");

  42. }

  43. } elseif("AJP/1.3".equals(protocol)) {

  44. if(aprConnector) {

  45. setProtocolHandlerClassName("org.apache.coyote.ajp.AjpAprProtocol");

  46. } else{

  47. setProtocolHandlerClassName("org.apache.coyote.ajp.AjpNioProtocol");

  48. }

  49. } else{

  50. // 最后如果不是通过指定 HTTP/1.1,AJP/1.3 类型的协议,就通过类名实例化一个协议处理器

  51. setProtocolHandlerClassName(protocol);

  52. }

  53. }

ProtocolHandler 是一个协议处理器,针对不同的请求,提供不同实现。实现类 AbstractProtocol 在初始化时,会在最后调用一个抽象类 AbstractEndpoint 初始化来启动线程来监听服务器端口,当接收到请求后,调用 Processor 读取请求,然后交给 Engine 处理请求。Engine Engine 对应的是, org.apache.catalina.Engine接口和 org.apache.catalina.core.StandardEngine默认实现类。Engine 的功能也比较简单,处理容器关系的关联。初探Tomcat的架构设计_实例化_07但是实现类中的 addChild()不是指的子 Engine,而是只能是 Host。同时没有父容器, setParent是不允许操作设置的。
  1. @Override

  2. publicvoid addChild(Container child) {

  3. // 添加的子容器必须是 Host

  4. if(!(child instanceofHost))

  5. thrownewIllegalArgumentException

  6. (sm.getString("standardEngine.notHost"));

  7. super.addChild(child);

  8. }

  9.  

  10. @Override

  11. publicvoid setParent(Container container) {

  12.  

  13. thrownewIllegalArgumentException

  14. (sm.getString("standardEngine.notParent"));

  15.  

  16. }

server.xml 可以配置我们的数据:
  1. <!-- 配置默认Host,及jvmRoute -->

  2. <Enginename="Catalina"defaultHost="localhost"jvmRoute="jvm1">

Host Host 表示一个虚拟主机。应为我们的服务器可设置多个域名,比如 demo.ytao.top,dev.ytao.top。那么我们就要设置两个不同 Host 来处理不同域名的请求。当过来的请求域名为 demo.ytao.top 时,那么它就会去找该域名 Host 下的 Context。 所以我们的 server.xml 配置文件也提供该配置:
  1. <!-- name 设置的时虚拟主机域名 -->

  2. <Hostname="localhost"appBase="webapps"

  3. unpackWARs="true"autoDeploy="true">

Context 到 Context 这里来,就拥有 Servlet 的运行环境,Engine,Host都是主要维护容器关系,不具备运行环境。我们暂且可将 Context 理解为一个应用,例如我们在根目录下有 ytao-demo-1 和 ytao-demo-2 两个应用,那么这里就是有两个 Context。这里主要介绍的 addChild方法,该添加的子容器是 Wrapper:
  1. @Override

  2. publicvoid addChild(Container child) {

  3.  

  4. // Global JspServlet

  5. Wrapper oldJspServlet = null;

  6.  

  7. // 这里添加的子容器只能时 Wrapper

  8. if(!(child instanceofWrapper)) {

  9. thrownewIllegalArgumentException

  10. (sm.getString("standardContext.notWrapper"));

  11. }

  12.  

  13. // 判断子容器 Wrapper 是否为 JspServlet

  14. boolean isJspServlet = "jsp".equals(child.getName());

  15.  

  16. // Allow webapp to override JspServlet inherited from global web.xml.

  17. if(isJspServlet) {

  18. oldJspServlet = (Wrapper) findChild("jsp");

  19. if(oldJspServlet != null) {

  20. removeChild(oldJspServlet);

  21. }

  22. }

  23.  

  24. super.addChild(child);

  25.  

  26. // 将servlet映射添加到Context组件

  27. if(isJspServlet && oldJspServlet != null) {

  28. /*

  29. * The webapp-specific JspServlet inherits all the mappings

  30. * specified in the global web.xml, and may add additional ones.

  31. */

  32. String[] jspMappings = oldJspServlet.findMappings();

  33. for(int i=0; jspMappings!=null&& i<jspMappings.length; i++) {

  34. addServletMappingDecoded(jspMappings[i], child.getName());

  35. }

  36. }

  37. }

这里也就是每个应用中的 Servlet 管理中心。Wrapper Wrapper 是一个 Servlet 的管理中心,它拥有 Servlet 的整个生命周期,它是没有子容器的,因为它自己就是最底层的容器了。这里主要对 Servlet 加载的分析:
  1. publicsynchronizedServlet loadServlet() throwsServletException{

  2.  

  3. // 如果已经实例化或者用实例化池,就直接返回

  4. if(!singleThreadModel && (instance != null))

  5. return instance;

  6.  

  7. PrintStream out = System.out;

  8. if(swallowOutput) {

  9. SystemLogHandler.startCapture();

  10. }

  11.  

  12. Servlet servlet;

  13. try{

  14. long t1=System.currentTimeMillis();

  15. // 如果 servlet 类名为空,直接抛出 Servlet 异常

  16. if(servletClass == null) {

  17. unavailable(null);

  18. thrownewServletException

  19. (sm.getString("standardWrapper.notClass", getName()));

  20. }

  21.  

  22. // 从 Context 中获取 Servlet

  23. InstanceManager instanceManager = ((StandardContext)getParent()).getInstanceManager();

  24. try{

  25. servlet = (Servlet) instanceManager.newInstance(servletClass);

  26. } catch(ClassCastException e) {

  27. unavailable(null);

  28. // Restore the context ClassLoader

  29. thrownewServletException

  30. (sm.getString("standardWrapper.notServlet", servletClass), e);

  31. } catch(Throwable e) {

  32. e = ExceptionUtils.unwrapInvocationTargetException(e);

  33. ExceptionUtils.handleThrowable(e);

  34. unavailable(null);

  35.  

  36. // Added extra log statement for Bugzilla 36630:

  37. // https://bz.apache.org/bugzilla/show_bug.cgi?id=36630

  38. if(log.isDebugEnabled()) {

  39. log.debug(sm.getString("standardWrapper.instantiate", servletClass), e);

  40. }

  41.  

  42. // Restore the context ClassLoader

  43. thrownewServletException

  44. (sm.getString("standardWrapper.instantiate", servletClass), e);

  45. }

  46.  

  47. // 加载声明了 MultipartConfig 注解的信息

  48. if(multipartConfigElement == null) {

  49. MultipartConfig annotation =

  50. servlet.getClass().getAnnotation(MultipartConfig.class);

  51. if(annotation != null) {

  52. multipartConfigElement =

  53. newMultipartConfigElement(annotation);

  54. }

  55. }

  56.  

  57. // 对 servlet 类型进行检查

  58. if(servlet instanceofContainerServlet) {

  59. ((ContainerServlet) servlet).setWrapper(this);

  60. }

  61.  

  62. classLoadTime=(int) (System.currentTimeMillis() -t1);

  63.  

  64. if(servlet instanceofSingleThreadModel) {

  65. if(instancePool == null) {

  66. instancePool = newStack<>();

  67. }

  68. singleThreadModel = true;

  69. }

  70.  

  71. // 初始化 servlet

  72. initServlet(servlet);

  73.  

  74. fireContainerEvent("load", this);

  75.  

  76. loadTime=System.currentTimeMillis() -t1;

  77. } finally{

  78. if(swallowOutput) {

  79. String log = SystemLogHandler.stopCapture();

  80. if(log != null&& log.length() > 0) {

  81. if(getServletContext() != null) {

  82. getServletContext().log(log);

  83. } else{

  84. out.println(log);

  85. }

  86. }

  87. }

  88. }

  89. return servlet;

  90.  

  91. }

这里加载 Servlet,如果该 Servlet 没有被实例化过,那么一定要加载一个。
到目前为止,大致介绍了 Tomcat8 的主要组件,对 Tomcat 的整体架构也有个大致了解了,Tomcat 源码进行重构后,可读性确实要好很多,建议大家可以去尝试分析下,里面的使用的一些设计模式,我们在实际编码过程中,还是有一定的借鉴意义。