暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Tomcat的Engine配置详述

中间件技术讨论圈 2017-02-12
706

本文重点来聊聊Tomcat的后端容器Engine这一级;


1.Engine是什么

Introduction

The Engine element represents the entire request processing machinery associated with a particular Catalina Service. It receives and processes all requests from one or more Connectors, and returns the completed response to the Connector for ultimate transmission back to the client.

Exactly one Engine element MUST be nested inside a Service element, following all of the corresponding Connector elements associated with this Service.


Engine是Tomcat中的Host层级的上级节点,其抽象的是Servlet引擎,Engine包装了Host,Context下级容器组件,和重要的Servlet,JSP的JAVA EE规范实现;

如下图的绿色框住的部分所示:


它的作用是接收Connector连接器组件的请求,借助于mapper组件,定位到哪一个Host的哪一个Context中的哪一个Servlet进行执行;

在这一系列的流程中,每一个容器组件都有一条pipeline,串其了各种valve,起到了管道过滤加工的作用;


2.关键属性

Common Attributes

All implementations of Engine support the following attributes:

AttributeDescription
backgroundProcessorDelay

This value represents the delay in seconds between the invocation of the backgroundProcess method on this engine and its child containers, including all hosts and contexts. Child containers will not be invoked if their delay value is not negative (which would mean they are using their own processing thread). Setting this to a positive value will cause a thread to be spawn. After waiting the specified amount of time, the thread will invoke the backgroundProcess method on this engine and all its child containers. If not specified, the default value for this attribute is 10, which represent a 10 seconds delay.

className

Java class name of the implementation to use. This class must implement the org.apache.catalina.Engine
 interface. If not specified, the standard value (defined below) will be used.

defaultHost

The default host name, which identifies the Host that will process requests directed to host names on this server, but which are not configured in this configuration file. This name MUST match the name
 attributes of one of the Host elements nested immediately inside.

jvmRoute

Identifier which must be used in load balancing scenarios to enable session affinity. The identifier, which must be unique across all Tomcat servers which participate in the cluster, will be appended to the generated session identifier, therefore allowing the front end proxy to always forward a particular session to the same Tomcat instance.

Note that the jvmRoute
 can also be set using the jvmRoute
 system property. The jvmRoute
 set in an <Engine>
 attribute will override any jvmRoute
 system property.

name

Logical name of this Engine, used in log and error messages. When using multiple Service elements in the same Server, each Engine MUST be assigned a unique name.

startStopThreads

The number of threads this Engine will use to start child Host elements in parallel. The special value of 0 will result in the value of Runtime.getRuntime().availableProcessors()
 being used. Negative values will result in Runtime.getRuntime().availableProcessors() + value
 being used unless this is less than 1 in which case 1 thread will be used. If not specified, the default value of 1 will be used.

上述的属性在conf/server.xml文件中,在<Service>下面就是<Engine>的可以进行配置,默认的Engine是StandardEngine


我们重点讲解一下几个关键属性。

其name属性是catalina,代表着目前我们用的是Tomcat自带的Servlet引擎,

你觉得Tomcat自带的引擎不好,你也可以当然你也可以使用className去指定自己的Engine,但必须要实现 org.apache.catalina.Engine
 接口;


defaultHost属性,比较关键,对于Engine来说,该属性是需要设置到Mapper组件中去:


当路由的时候,发现当前Engine中没有多余的Host,就使用这个DefaultHost进行路由;

或者是请求访问的时候,unkonwn host,最终也是由这个defaultHost进行出面进行解决;

其次,就是这个jvmRoute属性最关键了,该属性之所以像上述的描述的那样可以为唯一标识一个tomcat的servlet实例,其原因就是该jvmroute是在cluster中参与了生成sessionID的算法;


下面我们引自《b.StandardSession实现解析》对jvmroute的描述:


其余的几个属性,startStopThread,backgroundProcessorDelay 等和Host级别定义的一样,优先级没有Engine包装的下级容器组件要高;


总结:

本文简述了Engine这一层级的作用,其抽象的是Servlet引擎,其中最关键的属性是defaultHost和jvmRoute,其中以jvmRoute这一属性尤为重要;


文章转载自中间件技术讨论圈,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论