My architecture dream: details of Tomcat server core configuration

Keywords: Apache Linker Tomcat xml

Tomcat server core configuration details

be careful:

  • Tomcat is the configuration of the server, mainly conf/server.xml File configuration;
  • server.xml It contains the configuration of Servlet container, that is, Catalina configuration;
  • Xml file is mainly about the use of tags.

1, Main label structure

<!--
	Server Root element, creating a Server Instance, child tags have Listener,GlobalNamingResources,
	Service 
--> 
<Server>
	<!--Defining listeners-->
	<Listener/> 
	<!--Define the global of the server JNDI resources --> 
	<GlobalNamingResources/> 
	<!--Define a Service Service, one Server Labels can have multiple Service Service instance -->
    <Service/>
</Server>

2, Server label

<!--
	port:Turn off the listening port of the server 
	shutdown:Command string to shut down the server
-->
<Server port="8005" shutdown="SHUTDOWN">
  <!-- Output the server, operating system, and JVM Version information for -->
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <!-- load(Server start) And destruction (Server stop) APR.  If not found APR Library, the log will be output without affecting Tomcat start-up -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <!-- avoid JRE Memory leak -->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- load(Server start) And destruction(Server stop) Global naming service -->
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <!-- stay Context Rebuild on stop Executor Threads in the pool to avoid ThreadLocal Related memory leaks -->
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
       GlobalNamingResources Global naming service defined in
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
  	...
  </Service>
</Server>

3, Service tag

 
<!--
	This label is used to create a Service instance. It is used by default org.apache.catalina.core.StandardService .   By default, Tomcat only specifies the name of the Service, with a value of Catalina.
	The Service sub tags are Listener, Executor, Connector and Engine,
	Among them:
		Listener is used to add a lifecycle listener for a Service,
		Executor is used to configure the Service shared thread pool,
		Connector is used to configure the linker included in the Service,
		Engine is used to configure the Servlet container engine corresponding to the linker in the Service
-->
<Service name="Catalina">
	...
</Service>

4, Executor label

<!--
	By default, the service does not add the shared thread pool configuration. If we want to add a thread pool, we can add the following configuration under < Service >
	Name: thread pool name, which is used to specify in Connector
	namePrefix: the name prefix of each thread created. A separate thread name is namePrefix+threadNumber
	maxThreads: the maximum number of threads in the pool minSpareThreads: the number of active threads, that is, the number of threads in the core pool. These threads will not be destroyed. There will always be maxIdleTime: thread idle time. After this time, idle threads will be destroyed. The default value is 6000 (1 minute), unit
	millisecond
	maxQueueSize: the maximum number of threads queued before being executed. The default is the maximum value of Int, that is, the generalized infinite. Unless
	In particular, this value does not need to be changed. Otherwise, prestartminSpareThreads will occur when the request will not be processed: whether to start minSpareThreads when starting the thread pool. The default value is
	false, that is, do not start threadPriority: thread priority in thread pool. The default value is 5, and the value is from 1 to 10 className: thread pool implementation class. If not specified, the default implementation class is
	org.apache.catalina.core.StandardThreadExecutor .  If you want to use a custom thread pool, you need to implement org.apache.catalina.Executor interface
-->
<Executor name="commonThreadPool"
	namePrefix="thread-exec-"
	maxThreads="200"
	minSpareThreads="100"
	maxIdleTime="60000"
	maxQueueSize="Integer.MAX_VALUE" prestartminSpareThreads="false"
	threadPriority="5" className="org.apache.catalina.core.StandardThreadExecutor"/>

5, Connector label

Connector tags are used to create linker instances

By default, server.xml Two linkers are configured, one supports HTTP protocol and the other supports AJP protocol

In most cases, we don't need to add a new linker configuration, just optimize the existing linker as needed.

<!-- port:
	Port number. The Connector is used to create a Socket on the server and listen to it to wait for the client to request a link. If the property is set to 0, Tomcat will randomly select an available port number for the current Connector to use
	protocol:
	The access protocol supported by the current Connector. The default is HTTP/1.1, and the automatic switch mechanism is used to select a linker based on JAVA NIO or a linker based on local APR (based on whether the local library contains Tomcat or not). connectionTimeOut:
	Timeout for waiting after the Connector receives the link, in milliseconds. -1 means no timeout. redirectPort:
	The current Connector does not support SSL requests. It has received a request and also meets the security constraint. SSL transmission is required. Catalina automatically redirects the request to the specified port.
	executor:
	Specify the name of the shared thread pool. You can also configure the internal thread pool through maxThreads, minSpareThreads and other properties.
	URIEncoding:
	Used to specify the character encoding of the encoding URI. The default encoding of Tomcat8.x is UTF-8, and the default encoding of tomcat7. X is ISO-
	8859-1
-->
<!-- org.apache.coyote . http11.http11nioprotocol, non blocking Java NIO linker -- > 
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Shared thread pool is available

<Connector port="8080" 
	protocol="HTTP/1.1" 
	executor="commonThreadPool" 
	maxThreads="1000" 
    minSpareThreads="100" 
    acceptCount="1000" 
    maxConnections="1000" 
    connectionTimeout="20000" 
    compression="on" 
    compressionMinSize="2048" 
    disableUploadTimeout="true" 
    redirectPort="8443" 
    URIEncoding="UTF-8" />

6, Engine tag

Engine for Servlet engine

<!--
	Name: used to specify the name of Engine. The default is Catalina
	defaultHost: the virtual host name used by default. When the client request points to an invalid host, it will be handled by the default virtual host, which is localhost by default
-->
<Engine name="Catalina" defaultHost="localhost">
	...
</Engine>

Seven. Host label

The Host label is used to configure a virtual Host

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> 
	...
</Host>

8, Context label

The Context tag is used to configure a Web application, as follows:

<Host name="www.abc.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
	<!--
	docBase:Web Application directory or War The deployment path of the package. It can be absolute path or relative to Host appBase Relative path of.
	path:Web Applied Context route. If we Host be known as localhost, Then the web The root path of application access is: http://localhost:8080/web_demo. 
	-->
	<Context docBase="/Users/yingdian/web_demo" path="/web3"></Context>
	
	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 	
		prefix="localhost_access_log" suffix=".txt" 
		pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

Posted by kelvin on Wed, 24 Jun 2020 23:47:05 -0700