Tomcat 8.0 Concurrent Optimization - Optimize server.xml configuration

Tomcat concurrency closely related to its configuration, the average amount of concurrent machine hundreds of sufficient, if set too high may lead to various problems, memory, network and other issues can be exposed at high concurrency, therefore, very important to set the configuration parameters .

1 Tomcat's three modes of operation

1.1 BIO - synchronous blocking IO mode

BIO, synchronous blocking IO, low performance, without any optimization and support.

Server model to achieve a connecting thread that the client has the server when the connection request will need to start a thread for processing, if the connection does not do anything to cause unnecessary overhead of thread, of course, by  the thread pool  to improve the mechanism.

Applicable scene: BIO method is applicable to a relatively small number of connections and fixed infrastructure, this approach is relatively high server resource requirements, concurrency limitations, the only option before JDK1.4.

1.2 NIO - synchronous non-blocking IO mode

Java SE 1.4 is a new IO and later versions of the operation (i.e. java.nio package and its sub-packet). Java NIO is based on a buffer, and can provide non-blocking IO operations Java API, and therefore also NIO is abbreviated as a non-blocking IO (non-blocking IO), it has better performance than the conventional complicated operation BIO.

A request server implements a thread mode, i.e., the connection request sent by the client are registered to the multiplexer, the multiplexer is connected to the polling start a thread when the IO request processing.

Application scenarios: For more connections and connected short time (light operation) architecture, such as chat server concurrency limited to the application in this manner, the program more complicated.

Currently running in Tomcat 8.x default NIO mode.

1.3 APR - Portable runtime pattern

APR (Apache Portable Runtime, Apache portable runtime), Apache HTTP Server is a support library, which provides a set of API is mapped to the underlying operating system, if the operating system does not support a particular function, the APR library will provide simulation. Thus developers can use the APR to make the program truly portable across platforms.

This mode is more complicated installation steps, but it solves the problem of asynchronous IO from the operating system level, can greatly improve application performance.

APR is the essence of the technique using JNI calls the underlying operating system IO interface, it is necessary to install in advance the necessary dependence is given of a subsequent.

2 Tomcat concurrent configuration (Connector)

The Tomcat Connector is the key module receives HTTP requests, IO processing mode may be specified by which specifies the number of threads handling a request received by the Connector, HTTP and other commonly used strategy.

Path Configuration: In the  ${TOMCAT_HOME}/conf/server.xml configuration file in the node.

2.1 thread pool to process the request

Use the thread pool to handle more requests with fewer thread resources, thereby enhancing the Tomcat request processing capability.

Premise: To configure in advance at least a thread pool to handle requests, the configuration file to ${TOMCAT_HOME}/conf/server.xml.

Connector Executor and wherein the same level, can be used to process a plurality Connector thread pool with a request.

(1) Reference default connection pool configuration:

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>

(2) custom thread pool example:

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="200" minSpareThreads="10" maxIdleTime="600000" prestartminSpareThreads="true" maxQueueSize="100" /> 

(3) thread pool parameters:

name: Thread pool name.
namePrefix: Name prefix for each thread created a separate thread name + ThreadNumber namePrefix.
maxThreads: The maximum number of concurrent threads in the thread pool, the default is 200, recommended to set the 400 to 800, depending on the server and to configure business needs.
minSpareThreads: minimum number of active threads, which is the core number of threads, will not be destroyed, will always exist.
prestartminSpareThreads: are generates minSpareThreads threads when you start the program, the default is false, that is, if it is not set to start. true, then the set will not work minSpareThreads.
maxIdleTime: thread maximum idle time, after this time, the idle thread is destroyed, the default value is 6000 milliseconds.
maxQueueSize: maximum number of queue, exceeds the request is denied. the default value is the maximum value of type int (Integer.MAX_VALUE), equivalent to infinity generally not be modified, to avoid part of the request could not be processed occurs.
threadPriority: priority thread in the thread pool, the default value is 5, the value range: 1 to 10
className: implemented thread pool class, the case is not specified, the default implementation class  org.apache.catalina.core.StandardThreadExecutorfor a custom thread pool needs to implement.  org.apache.catalina.Executor Interface.

2.2 Use of the thread pool in Connector

Tomcat Connector receives the request is the entrance of each Connector has its own unique listening port.

  <Connector executor="tomcatThreadPool" 
             port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

(1) Connector parameter description:

redirectPort="8443" # SSL-based port, in case a secure channel based on need, such as when the client's request when the protocol is HTTPS, forwards the request to this port.

minSpareThreads="25" # The minimum number of idle threads Socket connector of the Tomcat, the default is 25. If there is no idle thread, and no more than maxThreads, the number of idle threads will be created once The number of threads created when this value is initialized Tomcat.

maxSpareThreads="75" # Maximum number of idle threads, thread creation once exceeds this value, Tomcat will shut Socket thread no longer needed, the default value of 50. The number of threads can roughly as "the number of concurrent users, number of user operations per second, average operating systems time "to calculate.

keepAliveTimeout="6000" # Before the arrival of the next request, Tomcat keep the connection 6000ms.

maxKeepAliveRequests="10" The maximum number of requests supported by the connecting #, the number of connections exceeds the request will also be turned off (in this case returns a Connection: close header to the client). Disables a long connection, not to limit the number of connections -1, the default is 100, disposed generally between 100 and 200.

acceptorThreadCount="1" # Number of threads for receiving connection, the default value is 1. If the server is typically a multi-core CPU, the need to change the configuration to 2.

enableLookups="false" # Whether to support the reverse lookup domain name (ie, DNS resolution), the default is true. In order to improve processing capacity, should be set to false.

disableUploadTimeout="true" # Timeout mechanism is enabled when uploading, if it is true, disables the upload timeout.

connectionTimeout="20000" # Network connection time, the default value 20000ms, set to 0 never expires - the existence of hidden 30000ms generally set.

URIEncoding="UTF-8" # Specify the URL-encoded format Tomcat container.

maxHttpHeaderSize="8192" # HTTP request header to the maximum extent, the length of the components will not exceed this process generally can be set to 8K.

maxPostSize="10485760" # POST request content specified size, in Byte, default size of 2097152 (2MB), 10485760 to 1OM. To disable limit, may be set to -1.

compression="on" Compression function # Open Transport.

compressionMinSize="10240" # Enable output size of the compressed content, the default is 2048, which 2KB.

noCompressionUserAgents="gozilla, traviata" # Set the compression is not enabled browser

compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" # Compressed resource type

(2) Additional information:

① Tomcat compression after the client requests a server corresponding to the resource from the server resource file compression, and then output to the client by the client's browser is responsible for decompressing and browsing. Common browsing process (e.g., browsing HTML respect, CSS, Javascript and Text), it can save about 40% of the traffic. more importantly, it can also be compressed dynamically generated web pages (including CGI, PHP, JSP, ASP, Servlet, SHTML, etc.).

② Note that the compression will increase the burden Tomcat, the best use Nginx + Tomcat or Apache + Tomcat way, the compression handed over to Nginx / Apache to complete node configuration in the server.xml (not yet validated using):

  • <Service name="Catalina" /> --- handles all web client requests received directly by the Tomcat server.
  • <Service name="Apache" /> --- forwarded by Apahce server handles all over the Web client requests.
  • <Service name="Nginx" /> --- forwarded by Nginx server handles all over the Web client requests.

2.2 NIO mode processing request

(1) The default configuration - BlockingIO model:

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

(2) description of the NIO:

  • Each Web client request is a separate thread for the server, the client requests an increase in the number, the number of processing threads on the server side will also increase on the CPU, it will consume more time on the thread switching. The NIO is single-threaded (single CPU) or using more than just a small number of threads (multi-CPU) to accept the Socket, while the thread pool to handle the clogged Pipe or Queue in the request. in this case, as long as the OS may accept the TCP connection, Web server can process the request - greatly improves the scalability of Web servers.
  • Tomcat 8 case of using NIO2, i.e.  org.apache.coyote.http11.Http11Nio2Protocol better.
  • Under Tomcat 6,7 use NIO, i.e.  org.apache.coyote.http11.Http11NioProtocol better.

(3) NIO model configuration:

<Connector executor="tomcatThreadPool" 
           port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol" connectionTimeout="20000" redirectPort="8443" maxPostSize="10485760" acceptorThreadCount="2" />

(4) Parameters:

executor="..."  # 连接器使用的线程池名称. 
port="..."      # 连接端口, URL中指定此端口进行访问.  
protocol="..."  # 连接器使用的请求处理模式.  
redirectPort="8443"   # 基于SSL的端口, 在需要基于安全通道的场合, 比如当客户端的请求协议是HTTPS时, 将该请求转发到此8443端口. 

2.3 APR mode processing request

Can simply be understood as APR mode, Tomcat will call the core dynamic link library JNI Apache HTTP server in the form of files read or network transmission operation, thereby greatly improving the processing performance of Tomcat static files.

APR is the preferred mode for running Tomcat high concurrent application, and if the transmission mode using HTTPS, SSL can also improve the processing performance.

As already mentioned, the APR model calls the underlying operating system IO interface, so it is necessary to install the necessary dependencies.

(1) Installation OpenSSL:

Installation command as follows:

yum -y install openssl-devel

(2) Installation APR components:

  • Step One: Download apr and apr-util package install:
    Download:  http://apr.apache.org/download.cgi here to download apr-1.6.3.tar.gzand apr-util-1.6.1.tar.gzthen install the package uploaded to the server, where uploaded to  /usr/local the directory.

  • Step 2: Install apr:

    cd /usr/local
    tar -zxf apr-1.6.3.tar.gz
    cd apr-1.6.3
    # 配置安装路径
    ./configure --prefix=/usr/local/apr
    # 编译安装
    make
    make install
  • Step 3: Install apr-util:

    cd /usr/local
    tar -zxf apr-util-1.6.1.tar.gz
    cd apr-util-1.6.1
    # 配置安装路径
    ./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr
    # 编译安装
    make
    make install

    Error message:

    Gcc is not installed dependent error may occur, by  yum install gcc command to install.

    If you make mistakes in the process, resolve the error before reinstalling the need to perform cleanup:  make cleanand then try make and make install process again.

    If you throw  xml/apr_xml.c:35:19: error: expat.h: No such file or directory, indicating the lack of expat libraries, executables subordinate command to install:  yum install expat-devel.

  • Step Four: Configure environment variables:

    After the installation is provided under the above environment variables:
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib

    Alternatively, add / usr / local / apr / lib /etc/ld.so.conf file path to the package:
    echo "/usr/local/apr/lib" >> /etc/ld.so.conf

(3) mounted tomcat-native components:

tomcat-nativeComponents can be seen as the middle part of Tomcat with APR interaction.

  • Step One: Download and extract the native installation package:

    Go  ${TOMCAT_HOME}/bin find tomcat-native.tar.gz, also can go to the official website (directory http://tomcat.apache.org/download-native.cgi) download other versions , it is recommended to use the version of Tomcat that comes, there is greater compatibility Guarantee.

    Tomcat-native decompression assembly and install or upload their native components downloaded to the server, then execute the following procedure:

     cd /usr/local/tomcat/bin/
     tar -zxf tomcat-native.tar.gz
     cd tomcat-native-1.2.14-src/native/

    Note: 1.1.x versions of the directory is tomcat-native-1.1.x-src / jni / native.

  • Step 2: Install components:

    # 配置安装参数, 其中java-home可使用默认环境变量中的配置
    ./configure --with-apr=/usr/local/apr --with-ssl=yes
    make
    make install

(4) Tomcat integration APR:

  • Step: Modify the startup script catalina.sh:
    the ${TOMCAT_HOME}/bin/catalina.sh file  cygwin=false is added the following starting parameters (about 110 lines) before:

    JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/apr/lib"
  • Step two: Modify the container configuration file server.xml:

    View  ${TOMCAT_HOME}/conf/server.xml documents to ensure that the listener not commented as follows:

    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  • Modify Connector options:

    <Connector port="8443" 
                protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true" > </Connector>

(5) Verify the configuration is successful:

Start Tomcat, in  ${TOMCAT_HOME}/logs/catalina.out view the log file information:

  • If the content of the following occurs, indicating an unsuccessful installation APR components:

    Sep 14, 2018 19:11:20 PM org.apache.catalina.core.AprLifecycleListener init 
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:...
  • If the content of the following occurs, a successful installation instructions APR components:

    Sep 14, 2018 19:19:47 PM org.apache.catalina.core.AprLifecycleListener init
    INFO: Loaded APR based Apache Tomcat Native library 1.1.27 using APR version 1.6.3.
    Sep 14, 2018 19:19:47 PM org.apache.catalina.core.AprLifecycleListener init
    INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
    Sep 14, 2018 19:19:47 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
    INFO: OpenSSL successfully initialized (OpenSSL 1.0.1e-fips 11 Feb 2013)
    Sep 14, 2018 19:19:47 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-apr-8080"]
    Sep 14, 2018 19:19:47 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
  • Tomcat APR mode by the successful launch:

    Sep 14, 2018 19:19:56 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-apr-8986"]
    Sep 14, 2018 19:19:56 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-apr-8915"]
    Sep 14, 2018 19:19:56 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 9421 ms

3 AJP connector configuration

AJP (Apache JServer Protocol) for communication between the HTTP server and the Tomcat customized protocol, can provide high communication speed and efficiency.

And Case Description: AJP v13 protocol is packet-oriented, Web Server and Servlet container to interact via a TCP connection, in order to save the cost of expensive Socket creation, Web server will try to maintain a permanent TCP connection to a Servlet container, and request more It reuses the TCP connection with the process of the response period.

If Apache architecture, AJP connector is necessary to use, when the dynamic page request received by Apache, in the configuration specified by the request to the port number on this port to listen AJP connector assembly.

If you do not use Tomcat + Apache, but with other architectures, such as Tomcat + Nginx, you need to log out of the connector.

<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  -->

Guess you like

Origin www.cnblogs.com/jtlgb/p/10985688.html