preface

There are two main aspects of Tomcat optimization: memory optimization and concurrency optimization. The knowledge of Tomcat is summarized in a document to share with you:

First, Tomcat memory optimization

① Tomcat startup script

Tomcat memory optimization is essentially a JVM optimization that tells the JVM how much memory it needs at startup (tuning memory is the most straightforward way). The configuration files are catalina.bat in Windows and catalina.sh in Linux.

When Tomcat is started on Linux, the bin/startup.sh file is usually executed. If you view the source file, the catalina.sh script file is executed.

In the catalina.sh script file, you can see the following comment variable definitions:

The script notes describe the environment configuration of CATALINA Server, such as CATALINA_HOME, CATALINA_BASE, CATALINA_OUT, CATALINA_OPTS, and CATALINA_TMPDIR. It is worth noting that it is generally recommended to place the custom environment configuration file in setenv.sh in CATALINA_BASE/bin.

② Variable description in catalina.sh

CATALINA_HOME&&CATALINA_BASE

The former is the Tomcat installation directory and the latter is the Tomcat working directory. CATALINA_BASE This variable is optional. If it is not configured, CATALINA_HOME is used by default. CATALINA_HOME&&CATALINA_BASE If we want to run multiple instances of Tomcat, but don’t want to install multiple copies of Tomcat software. We can then configure multiple working directories, with each running instance owning a single working directory but sharing the same installation directory.

CATALINA_OUT

Also optional, pointing to the full path of the files that will redirect stdout and stderr. The default is $CATALINA_BASE/logs/catalina.out.

CATALINA_OPTS

This configuration is optional. Java runtime options to use when executing start, Run, or debug commands. The options defined here (not in JAVA_OPTS) can only be used by Tomcat itself and cannot be run by stop threads, version commands, etc. Configure head size, GC Logging, JMX Ports, etc.

CATALINA_TMPDIR

This configuration is optional. The temporary path used by the JVM (java.io. Tmpdir corresponds to the system environment variable). The default path is $CATALINA_BASE/temp.

JAVA_HOME&&JRE_HOME

JAVA_HOME – Java development environment, JRE_HOME – Java runtime environment. After JDK 1.7, the JRE is embedded in the JDK. If JRE_HOME does not show the setting, the JAVA_HOME value is taken.

JAVA_OPTS

This configuration is optional. Java runtime options to use when executing any command. The options here (not in CATALINA_OPTS) can be used by Tomcat itself, the stop process, the version command, and so on. Most of the parameters you configure in JAVA_OPTS should go into CATALINA_OPTS and are used by CATALINA_OPTS.

CATALINA_PID

This configuration is optional. Contains the PID file for catalina to start the Java process. When start (fork) is used

LOGGING_CONFIG

This configuration is optional. Cover Tomcat default log configuration file, the default instance for LOGGING_CONFIG = “- Djava. Util. Logging. Config. The file = $CATALINA_BASE/conf/logging properties”

LOGGING_MANAGER

This configuration is optional. Cover the Tomcat default log manager, default instance for LOGGING_MANAGER = “- Djava. Util. Logging. Manager = org. Apache. Juli. ClassLoaderLogManager”

These parameters can be seen in the Tomcat startup log:

3 Catalina. sh Configures JVM parameter instances

Configure the JVM memory space in the catalina.sh file, as follows:

JAVA_OPTS="-server -Xms256m -Xmx2048m -XX:PermSize=256m -XX:MaxNewSize=1024m -XX:MaxPermSize=1024M"

-xms Specifies the initial heap size of the JVM. -xmx Specifies the maximum heap size of the JVM. The actual size depends on the server configuration or project Settings. PermSize and MaxPermSize are not supported in jdK1.8. Instead, they are -xx :MetaspaceSize and -xx :MaxMetaspaceSize.

Tomcat thread optimization is configured in server.xml

Examples are as follows:

Connector port="8080" protocol="HTTP/1.1" maxThreads="1000" minSpareThreads="100" acceptCount="1000" maxConnections="1000" connectionTimeout="20000" maxHttpHeaderSize="8192" tcpNoDelay="true" compression="on" compressionMinSize="2048" disableUploadTimeout="true" redirectPort="8443" enableLookups="false" URIEncoding="UTF-8" />

1) protocol

Protocol type. Four types are available: BIO (blocking IO), NIO, NIO2, and APR.

BIO

BIO(Blocking I/O) Blocking I/O operations, traditional Java I/O operations (that is, the java.io package and its subpackages). By default, Tomcat runs in BIO mode, which is the least efficient of the three running modes. Use the default BIO configuration. BIO is better suited for simple processes, such as programs that process quickly and return results immediately. Simple projects and applications can use BIO.

NIO

NIO(New I/O) is a New way of doing I/O operations (that is, the java.niO package and its subpackages) provided by Java SE 1.4 and later. Java NIO is a Buffer-based, non-blocking Java API for I/O operations that has better concurrency performance than traditional I/O operations (BIO). Take the NIO is more suitable for the background to complete the requested operation, such as program after receiving the request need to deal with this has requested more time-consuming, so I can’t immediately return a result, this will take up a connection using BIO, and after using the NIO this connection can be transferred to other requests, until application processing is completed return.

APR

APR(Apache Portable Runtime/Apache Portable Runtime), is a support library for the Apache HTTP server. Tomcat will call the Apache HTTP server’s core dynamic link library as JNI to handle file reads or network transfers, greatly improving Tomcat’s performance for static files. APR can greatly improve Tomcat’s performance in handling static files, as well as SSL if you use HTTPS for transfer.

Modify the way

/ / BIO protocol = "HTTP / 1.1" / / NIO protocol = "org. Apache. Coyote. Http11. Http11NioProtocol" / / NIO2 protocol="org.apache.coyote.http11.Http11Nio2Protocol" //APR protocol="org.apache.coyote.http11.Http11AprProtocol"

(2) the maxThreads

The connector creates the maximum number of threads to process requests, and the maximum number of simultaneous requests to process. The default is 200.

If an executor is associated with this connector, this property is ignored because it will be ignored, so the connector will use the executor instead of an internal thread pool to perform tasks. MaxThreads is an important configuration attribute, and the proper configuration of maxThreads directly affects the performance of Tomcat. MaxThreads is not always configured as big as possible, in fact it is useless if you set it to 999999 because the maximum value is OS and hardware dependent and is not always the optimal value, so we are looking for the optimal value rather than the maximum value.

(3) minSpareThreads

The minimum number of threads that are always running (idle threads). If not specified, the default value is 10.

(4) acceptCount

Maximum queue length, generally the same as maxThreads, 100 by default.

The maximum queue length of incoming connection requests when all possible request processing threads are in use. If not specified, the default value is 100. It is usually set to the same or half value as maxThreads. Too much of this value will cause queued requests to time out and not be processed. So this value should be configured primarily based on the application’s peak access versus average.

(5) maxConnections

The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server accepts but does not process it, waiting for further connections (enqueued). The default value for NIO and NIO2 is 10000 and the default value for APR is 8192.

6 connectionTimeout

The timeout in waiting when a request has been accepted but not processed. The unit is millisecond. The default value is 60000. The value is usually set to 30000.

All landowners maxHttpHeaderSize

The maximum size of HTTP headers for requests and responses, specified in bytes. If not specified, this property is set to 8192 (8 KB).

Today tcpNoDelay

If true, the server socket sets the TCP_NO_DELAY option, which can improve performance in most cases. By default, it is set to true.

Pet-name ruby compression will

Whether to enable gzip compression. By default, gzip compression is disabled. Acceptable values for this parameter are “off” (no compression), “on” (compression of text data), and “force” (compression by force in all cases).

Attending compressionMinSize

If compression= “on”, this is enabled. The minimum value of data before compression, that is, the value exceeded before compression. If not specified, this property defaults to “2048” (2K) in bytes.

(1) (1) disableUploadTimeout

This flag allows the Servlet Container to use a different, longer connection timeout when a servlet executes. The end result is a longer time for the servlet to complete its execution, or a longer timeout when data is uploaded. If not specified, set to false.

(1) (2) enableLookups

Example Disable DNS reverse lookup.

Third, Tomcat IO optimization

Synchronization blocking IO(JAVA BIO)

Synchronization and blocking, the server implementation mode is one connection one thread (think horrible, threads are very valuable resources), of course can be improved through the thread pool mechanism.

② JAVA NIO is divided into synchronous non-blocking IO and asynchronous blocking IO

The biggest difference from BIO is one request one Thread. Multiple connections can be multiplexed using the same thread.

Asynchronous non-blocking IO(Java NIO2 also called AIO)

The main difference with NIO is mainly the bottom difference of the operating system, which can be compared to express delivery. NIO is to check whether the express has arrived on the official website after online shopping (it may be many times), and then go to pick up the express. AIO is when a Courier delivers to your door (regardless of progress).

BIO mode is suitable for small and fixed number of connections. This mode requires high server resources, concurrency is limited to the application, and the only choice before JDK1.4, but the program is intuitive, simple and easy to understand.

NIO is suitable for architectures with a large number of connections and relatively short (light operation) connections, such as chat servers, where concurrency is limited to applications and programming is complicated. JDK1.4 supports NIO.

AIO mode is used in the architecture with a large number of connections and long connections (heavy operation), such as photo album server. It fully calls OS to participate in concurrent operations, and the programming is complicated, which is supported by JDK7.

In the server. The XML:

<Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" URIEncoding="UTF-8" useBodyEncodingForURI="true" enableLookups="false" redirectPort="8443" />

Implement I/O switch to Tomcat.

Fourth, the big kill APR

APR solves asynchronous IO problems at the operating system level, dramatically improving performance (apr.apache.org/).

APR(Apache Portable Runtime) is a highly Portable library that is at the heart of Apache HTTP Server 2.x and integrates better with other native Web technologies. Making Java more efficient as a high-performance Web server platform in general rather than simply as a backend container;

In a production environment, especially if Tomcat is used directly as a WEB server, Tomcat Native should be used to improve its performance. Without the APR, basically 300 threads will be filled up quickly and future requests will have to wait. But with APR, the number of concurrent threads drops significantly, from 300 to only a few dozen, and new requests come in without blocking.

In the LOCAL area network environment test, even 400 concurrent, is also a moment to process/transmission completed, but in the real Internet environment, page processing time only accounts for less than 0.1%, most of the time is used for page transmission, if not APR, a thread can only process a user at the same time, is bound to cause blocking, So it’s very necessary to use APR in a production environment.

Apache Tomcat Native Library (tomcat.apache.org/native-doc/)

It is inherently apr-based, and Tomcat is optimized to this level to handle most performance requirements without code issues.

Finally, optimization is preceded by good code quality and design.

conclusion

I here organized a SpringBoot related information documents, Spring series of family barrel, Java systematic information (including Java core knowledge points, interview topics and 20 years of the latest Internet real questions, e-books, etc.) friends who need to pay attention to the public number can be obtained.