Tomcat configuration files and Session Replication

Tomcat server configuration

server.xml

tomcat server.xml is the core configuration file server that contains all the configuration of Tomcat Servlet container (Catalina) of

Server

<Server port="8005" shutdown="SHUTDOWN">
...    
</Server>
  • Server is the root element of server.xml for creating a Server instance, the default implementation class is org.apache.catalina.core.StandardServer.
  • Sub-elements
    • port: port shut down the server Tomcat is listening.
    • shutdown: shut down the server command string.
  • Sub-label
    • Listener : Listener (default five kinds)

      <!‐‐ 用于以日志形式输出服务器 、操作系统、JVM的版本信息 ‐‐>
      <Listener className="org.apache.catalina.startup.VersionLoggerListener"
      />
      
      <!‐‐ 用于加载(服务器启动) 和 销毁 (服务器停止) APR。 如果找不到APR库, 则会输出日志, 并不影响Tomcat启动 ‐‐>
      <Listener className="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" />
      
      <!‐‐ 用于避免JRE内存泄漏问题 ‐‐>
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      
      <!‐‐ 用户加载(服务器启动) 和 销毁(服务器停止) 全局命名服务 ‐‐>
      <Listener	className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
      
      <!‐‐ 用于在Context停止时重建Executor 池中的线程, 以避免ThreadLocal 相关的内存泄漏 ‐‐>
      <Listener	className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
      
    • GlobalNamingResources : defines global naming service

      <!‐‐ Global JNDI resources
      	   Documentation at /docs/jndi‐resources‐howto.html
      ‐‐>
      <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>	
      
    • Service

      <Service name="Catalina">
      ...    
      </Service>
      
      • Server a server can contain multiple Service service.
      • Default org.apache.catalina.core.StandardService.
      • By default, Tomcat only specifies the name of the Service, the value of "Catalina".
      • Sub-sections:
        • Listener : add a listener for Service Lifecycle

        • Executor : shared thread pool is used to configure Service (Service default configuration does not add a shared thread pool, if you do not configure a shared thread pool, then give each Connecto independently create a thread pool.)

          <Executor name="tomcatThreadPool"
          namePrefix="catalina‐exec‐"
          maxThreads="200"
          minSpareThreads="100"
          maxIdleTime="60000"
          maxQueueSize="Integer.MAX_VALUE"
          prestartminSpareThreads="false"
          threadPriority="5"
          className="org.apache.catalina.core.StandardThreadExecutor"/>
          

          Property Description:

          • name: the name of the thread pool for Connector specified.
          • namePrefix: name prefix for each thread created, a separate thread named namePrefix + threadNumber.
          • maxThreads: maximum number of threads in the pool.
          • minSpareThreads: the number of active threads, the core of which is the number of threads pool, these threads are not destroyed, it will always exist.
          • maxIdleTime: thread idle time, after this time, the idle thread will be destroyed, the default value is 6000 (one minute), in milliseconds.
          • maxQueueSize: the maximum number of threads to be executed before the line, the default is the maximum Int, which is infinitely broad. Except in special circumstances, this value does not need to change, or there will not be processed requests circumstances occur.
          • prestartminSpareThreads: whether to start minSpareThreads part of the thread startup thread pool. The default value is false, do not start.
          • threadPriority: priority thread pool, the default value is 5, the value from 1 to 10.
          • className: thread pool implementation class, under circumstances not specified, the default implementation class for org.apache.catalina.core.StandardThreadExecutor. If you want to use a custom thread pool org.apache.catalina.Executor first need to implement the interface.
        • Connector : Service for link configuration contains (by default, server.xml configured with two linker, respectively, support the HTTP protocol, AJP protocol in most cases does not require new link configuration, only needed to have. there linker optimization)

          <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
          redirectPort="8443" />
          <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
          

          Property Description:

          • port: port number, Connector and Socket used to create the server listens, waiting for the client request link. If this property is set to 0, Tomcat will randomly select an available port number to the current Connector used.
          • protocol: Current Connector supports access protocols. The default is HTTP / 1.1, and the switching mechanism for automatic selection of a linker based JAVA NIO APR-based local or linker (determined according to whether the local local library containing the Tomcat). If you do not wish to use the automatic switch mechanism, but explicitly specified protocol, the following values ​​may be used.
            • Http protocol:
              • org.apache.coyote.http11.Http11NioProtocol, non-blocking Java NIO linker
                * org.apache.coyote.http11.Http11Nio2Protocol, non-blocking JAVA NIO2 linker
                * org.apache.coyote.http11.Http11AprProtocol, APR linker
            • AJP protocol
              • org.apache.coyote.ajp.AjpNioProtocol, non-blocking Java NIO Linker
              • org.apache.coyote.ajp.AjpNio2Protocol, non-blocking JAVA NIO2 Linker
              • org.apache.coyote.ajp.AjpAprProtocol, APR linker
          • connectionTimeOut: Connector waiting after receiving a link timeout, in milliseconds. -1 indicates no timeout.
          • redirectPort: Connector currently does not support SSL request, a request is received, and also comply with security-constraint constraint requires SSL transport, Catalina automatically redirects the request to the specified port.
          • executor: Specifies the name of the shared thread pool, you can also configure the internal thread pool by maxThreads, minSpareThreads other attributes.
          • URIEncoding: character encoding for encoding the URI specified, Tomcat8.x version default encoding UTF-8, Tomcat7.x default version of ISO-8859-1.
          • maxConnections: maximum number of connections, it is recommended around 2000
          • acceptCount: The maximum number of wait (connections = maxConnections + acceptCount, more than will be rejected)
          • maxThreads: maximum number of threads
          <Connector port="8080"  
          protocol="HTTP/1.1"
          executor="tomcatThreadPool"
          maxThreads="1000"  
          minSpareThreads="100"  
          acceptCount="1000" 
          maxConnections="1000" 
          connectionTimeout="20000"
          compression="on" 
          compressionMinSize="2048" 
          disableUploadTimeout="true" 
          redirectPort="8443" 
          URIEncoding="UTF‐8" />
          
        • Engine : Engine Servlet Container Configuration Service for the corresponding linker

          <Engine name="Catalina" defaultHost="localhost">
          	...    
          </Engine>
          
          • Sub-elements:
            • name: specifies the name of Engine, the default is Catalina. The name will affect the storage path of Tomcat part (such as temporary files).
            • defaultHost: default virtual host name to use when invalid client requests to the host, will be submitted to the default virtual host processing, the default is localhost.
          • Sub-label
            • Host : Host element is used to configure a virtual host
              <Host name="localhost"  appBase="webapps" unpackWARs="true"
              autoDeploy="true">
              	    ...
              </Host>   
              
              • Property Description:
                • name: the name of the current Host common network, must match the registration information on the DNS server. Engine contains a Host there must be consistent with the name of defaultHost Engine settings.
                • appBase: current Host application base directory, Web applications are currently deployed on the Host in the directory (the directory can be absolute, relative path). The default is webapps.
                • unpackWARs: set to true, Host at startup directory will appBase war unpack directory. Is set to false, Host will launch directly from the war file.
                • autoDeploy: tomcat control whether a regular basis to detect and automatically deploy new or changed web application at runtime.
              • If you configure Realm in Engine, then this configuration will be shared among all of the Host in the current Engine. Similarly, if you configure the Host Realm, then shared among all Context under the current Host.
              • Context Realm priority in the priority Realm> Host priority of Realm> Engine in.
            • Sub-label
              • Alias: Host to add an alias (the same can be achieved with multiple network Host Name)
                <Host name="www.web1.com"  appBase="webapps" unpackWARs="true"
                autoDeploy="true">
                    <Alias>www.web2.com</Alias>
                </Host>  
                
              • Context : Configure a Web application, the default configuration is as follows
                <Context docBase="myApp" path="/myApp">
                	    ....
                </Context> 
                
                • Property description:
                  • docBase: Web application directory or path War deployment package. It can be absolute, but may be a path relative to the Host appBase.
                  • path: Context path of the Web application. If we called Host localhost, then access the web application root path is: http: // localhost: 8080 / myApp.
                • Support for inline elements: CookieProcessor, Loader, Manager, Realm, Resources, WatchedResource, JarScanner, Valve.
                  <Host name="www.tomcat.com"  appBase="webapps" unpackWARs="true"
                  		autoDeploy="true">
                      <Context docBase="D:\servlet_project03" path="/myApp"></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>
                  

tomcat-users.xml

  • The configuration file is the main configuration of Tomcat users, roles, and other information, is used to control the Tomcat manager, access to the host-manager.
  • Since the early days of the Tomcat version, it provides a Web management console version, they are two separate Web application, located in the webapps directory. Tomcat management application provided for Host managed host-manager and manager for managing Web applications.

host-manager

  • After starting Tomcat, you can http: // localhost: 8080 / host-manager / html access the Web application.

  • host-manager added default access control, when you open the URL, you need to enter a user name and password (conf / tomcat-users.xml configured).

  • So in order to access this page, you need to configure the conf / tomcat-users.xml and assign the corresponding roles:

    <role rolename="admin‐gui"/>
    <role rolename="admin‐script"/>
    <user username="yzx" password="yzx" roles="admin‐script,admin‐gui"/>
    

    admin-gui: used to control page access
    admin-script: it is used to control access in the form of simple text

    Here Insert Picture Description

manager

  • manager access address is http: // localhost: 8080 / manager
  • manager also added a page access control, so we need to assign roles to user login
    <role rolename="manager‐gui"/>
    <role rolename="manager‐script"/>
    <user username="yzx" password="yzx" roles="admin‐script,admin‐gui,manager‐gui,manager‐script"/>
    
    Here Insert Picture Description
    Here Insert Picture Description

web.xml

web.xml web application description file, which supports the elements and attributes defined from the Servlet specification.

  • In Tomcat, the description of Web applications, including tomcat / conf / web.xml in the default configuration, and Web
    custom configuration of the application WEB-INF / web.xml.

catalina.bat / catalina.sh

Configuring JVM

  • catalina.bat(windows)
    set JAVA_OPTS=‐server  ‐Xms2048m  ‐Xmx2048m  ‐XX:MetaspaceSize=256m ‐
    
  • catalina.sh(linux)
    JAVA_OPTS="‐server ‐Xms1024m ‐Xmx2048m ‐XX:MetaspaceSize=256m ‐XX:MaxMetaspaceSize=512m  ‐XX:SurvivorRatio=8"
    

Tomcat clustering (Session replication)

  • In the Tomcat conf / server.xml configuration is as follows:
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    
  • Added as follows Tomcat application deployed servlet_demo01 of web.xml:
    <distributable/>
    

note

  • The above scheme, for smaller cluster environment (no more than 4 nodes)
  • If the number of nodes in the cluster of more, then, to complete the Session replication through this form of broadcasting, it will consume a lot of network bandwidth and affect the performance of the service.
Published 109 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43934607/article/details/104386263