background

Java programmers usually choose to use Tomcat to distribute their Java applications, but how to fully control Tomcat and let it play its best performance?

This is also a hot question in the interview. Based on years of work practice, MY name is Li Fuchun.

The use of a tomcat

download

Now the latest stable version is tomcat9, download page: tomcat.apache.org/download-90…

5 different download packages

Generally, we choose core package to run Tomcat, or directly choose the image of docker to run;

Tomcat directory description:

The installation

Decompress the decompression command tar -zxvf tomcat-XXX.tar. gz

Start and stop

Follow the instructions in running. TXT to start in two ways:

Sh ${catalina.home}/bin/startup.sh 2, sh ${catalina.home}/bin/catalina.sh start

There are two ways to stop Tomcat:

Sh ${catalina.home}/bin/shutdown.sh 2, sh ${catalina.home}/bin/catalina.sh stop

The log

Logs generated by Tomcat are classified into four types

Catalina.date. out Indicates the latest logs of all levels. 2. Localhost-date. log Error log

Run the tail -f catalina.out command to view logs in real time

AJP protocol

Tomcat is used to connect to other HTTP servers.

For example Apache+Tomcat do static and dynamic separation:

Apache handles all static resources;

Apache forwards dynamic resource requests to Tomcat through JK (load balancing component) through AJP protocol.

Tomcat monitor

By keeping the ROOT, host-manager, and manager applications of webapps under default Tomcat, you can monitor the status of a single Tomcat node.

By default, the access is disabled. You need to add the user and permission to view the access. Otherwise, 403 is reported.

You can add them to conf/tomcat-user.xml

<role rolename='admin' />
...
<user username='admin' password='admin' roles='admin,admin-gui,admin-script,
manager-script,manager-gui,manager-jmx,manager-status' />Copy the code

The monitoring page is shown as follows:

Server status: You can view tomcat and JVM version information, JVM partition information, tomcat internal thread pool status.

Manager-app: Manages applications running under Tomcat, providing control buttons, starting, stopping, restarting, uninstalling, and constantly installing new applications;

Host-manger: provides virtual host management, that is, configuring aliases and secondary paths to Tomcat applications.

IO tuning of Tomcat

Nio is used by default in Tomcat9 to handle Java IO. This can be seen in logs and configuration files.

09 - Apr - 2020 07:46:27. 606 information [main] org. Apache. Coyote. AbstractProtocol. Start to handle protocol processing [HTTP - nio - 8080 ""]Copy the code

APR optimization IO

The Use of APR (Apache Portable Runtime) solves the problem of asynchronous IO at the operating system level and can significantly improve performance. If Linux has APR and tomcat-Native installed, Tomcat starts with APR;

NIO optimizes older versions of BIO

If the old version of Tomcat uses BIO(as can be seen from logs), you can change it to NIO by using conf/server.xml

Old configuration:

< connector protocol = "HTTP / 1.1" / >Copy the code

New configuration:

/ / tomcat6 choose nio1 < connector protocol = "org. Apache. Coyote. Http11. Http11NioProtocol" / > / / tomcat8 choose nio2, apr better < connector protocol="org.apache.coyote.http11.Http11Nio2Protocol" /> <connector protocol="org.apache.coyote.http11.Http11AprProtocol" />Copy the code

Tomcat thread pool tuning

Tomcat does not enable thread pools by default. You can enable thread pools to improve thread utilization

Thread pool parameters:

Defining a thread pool

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

Enable Connector configuration

<connector executor="tomcatThreadPool">Copy the code

The connector parameters

JVM parameter tuning for Tomcat

The GC optimization

JAVA_GC=" -xx :SurvivorRatio= 10-xx :MaxTenuringThreshold= 15-xx :NewRatio= 2-xx :+DisableExplicitGC -Djava.security.egd=file:/dev/./urandom"Copy the code

JVM and thread pool optimization

JVM_LEVEL="info" JVM_Xms="100m" JVM_Xmx="2048m" JVM_Xmn="600m" JVM_Xss=" 256K "TOMCAT_acceptCount=4096 Number of requests that a thread can accept TOMCAT_maxThreads=512 Maximum number of threads TOMCAT_minSpareThreads=512 Initial number of threadsCopy the code

summary

This article reviews the basics of Tomcat.

And some basic knowledge of monitoring Java applications using tomcat built-in monitor.

Then, based on my working experience, I tuned Tomcat from IO, thread pool and JVM

Original is not easy, praise attention to support it! Reprint please indicate the source, let us exchange, common progress, welcome communication. I will continue to share Java software programming knowledge and programmer development career path, welcome to pay attention to, I sorted out these years of programming learning all kinds of resources, pay attention to the public account ‘Li Fuchun continuous output’, send ‘learning materials’ to share with you!