Tomcat installation and deployment optimization

TOMCAT introduction

Tomcat is a core project of the Apache Software Foundation's Jakarta project, which is jointly developed by Apache, Sun, and some other companies and individuals. Thanks to Sun's participation and support, the latest Servlet and JSP specifications can always be reflected in Tomcat. Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specifications. Because Tomcat has advanced technology, stable performance, and free, it is loved by Java enthusiasts and recognized by some software developers, making it a popular Web application server.
Tomcat server is a free and open-source Web application server, which is a lightweight application server. It is commonly used in small and medium-sized systems and where there are not many concurrent users. It is the first choice for developing and debugging JSP programs. For a beginner, it can be thought that when an Apache server is configured on a machine, it can be used to respond to HTML (an application under the standard universal markup language) page access request. In fact, Tomcat is an extension of the Apache server, but it runs independently at runtime, so when you run tomcat, it actually runs as a separate process from Apache.
The trick is that when configured correctly, Apache serves HTML pages, while Tomcat actually runs JSP pages and servlets. In addition, Tomcat, like IIS and other Web servers, has the function of processing HTML pages. In addition, it is also a Servlet and JSP container. The independent Servlet container is the default mode of Tomcat. However, Tomcat is not as capable of handling static HTML as the Apache server.

tomcat icon

wrong! Come again! !
Insert picture description here
This is right!
Insert picture description here

A brief overview is that the
free, open source web application server
Apache Software Foundation (Apache Software Foundation) Jakarta project is a core project
jointly developed by Apache, Sun, and some companies and individuals and is
deeply loved by Java enthusiasts. It has been recognized by some software developers. It
is currently a popular web application server.

Tomcat schematic


Insert picture description here
Insert picture description hereSchematic diagram of the core component request process
Insert picture description hereconnector: Connector
A Connector component will listen to client requests on a specified port, receive the tcp connection request sent by the browser, and create a Request and a Response object to communicate with you. Exchange data, and then a thread will be generated to process the request and pass the generated Request and Response objects to the Engine, get the response from the Engine and return it to the client. Tomcat has two classic Connectors, one directly listens to HTTP requests from the browser, and the other listens to requests from other WebServers. Cotote HTTP/1.1 Connector listens to HTTP requests from client browsers at port 8080, and Coyote JK2 Connector listens to Servlet/JSP requests from other WebServers at port 8009. The most important function of the Connector is to receive connection requests and then allocate threads for the Container to process the request, so this must be multi-threaded, and multi-threaded processing is the core of the connector design.


Container : Container is the parent interface of the container. The design of the container uses a typical responsibility chain design pattern. It consists of four self-container components, namely Engine, Host, Context, and Wrapper. These four components are responsible, and there is a containment relationship. Usually a Servlet class corresponds to a Wrapper. If there are multiple Servlets, define multiple Wrappers. If there are multiple Wrappers, define a higher Container, such as Context. Context is defined in the parent container Host, where Host is not necessary, but to run the war program, you must host, because there must be a web.xml file in the war, and the resolution of this file requires Host. If there are multiple Hosts It is necessary to define a top container Engine. Engine has no parent container anymore, and an Engine represents a complete Servlet engine.

engine: Engine
Engine container is relatively simple, it only defines some basic association relationship Host container

host: Virtual host
Host is the word container of Engine. A Host represents a virtual host in Engine. The function of this virtual host is to run multiple applications. It is responsible for installing and deploying these applications, and identifying the applications so that they can be distinguished. Its child container is usually Context. In addition to associating child containers, it also stores information that a host should have

Context: The
Context of the front page of the JSP represents the Context of the Servlet. It has the basic environment for the Servlet to run. In theory, the Servlet can be run as long as there is a Context. Simple Tomcat can have no Engine and Host. The most important function of Context is to manage the Servlet instances in it. Servlet instances appear as Wrapper in Context. Another point is how can Context find the correct Servlet to execute it? Tomcat5 was previously managed by a Mapper class. After Tomcat5, this function has been moved to request. In the previous sequence diagram, you can find that obtaining sub-containers is allocated through request.

servlet: processing code

Explanation of the request process
1. The user enters the URL localhost:8080/test/index.jsp in the browser, and the request is sent to the local port 8080, which is obtained by the Coyote HTTP/1.1 Connector listening there;
2. The Connector sends the request Give it to the Engine (Container) of the Service where it is located, and wait for the response of the Engine;
3. The Engine gets the request localhost/test/index.jsp, which matches all virtual hosts Host;
4. The Engine matches the Host named localhost ( Even if it fails to match, the request is handed over to the Host for processing, because the Host is defined as the default host of the Engine), the Host named localhost gets the request /test/index.jsp, matching all the Contexts it owns. Host matches the Context whose path is /test (if it fails to match, hand over the request to the Context whose path name is "" for processing);
5. The Context whose path="/test" gets the request /index.jsp, in it Find the corresponding Servlet in the mapping table. Context matches the Servlet whose URL Pattern is *.jsp, which corresponds to the JspServlet class;
6. Construct HttpServletRequest object and HttpServletResponse object, call doGet() or doPost() of JspServlet as parameters, execute business logic, data storage, etc.;
7.Context Return the HttpServletResponse object after execution to the Host;
8. Host returns the HttpServletResponse object to the Engine;
9.Engine returns the HttpServletResponse object to the Connector;
10.Connector returns the HttpServletResponse object to the client Browser

The picture below can help us understand more easily
Insert picture description here

bring it on! Show!

download link

Tomcat 9.0.37 download

Install JDK

The software development kit of the java language in the JDK is
also a necessary environment for tomcat runtime

[root@7CentOS ~]# cd /
[root@7CentOS /]# mkdir tom
[root@7CentOS /]# cd tom/
[root@7CentOS tom]# rz -E
rz waiting to receive.
[root@7CentOS tom]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.rpm

[root@7CentOS tom]# rpm -ivh jdk-8u201-linux-x64.rpm 
警告:jdk-8u201-linux-x64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID ec551f03: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:jdk1.8-2000:1.8.0_201-fcs        ################################# [100%]
Unpacking JAR files...
	tools.jar...
	plugin.jar...
	javaws.jar...
	deploy.jar...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...

Set environment variables

[root@7CentOS ~]# java -version
openjdk version "1.8.0_222-ea"
OpenJDK Runtime Environment (build 1.8.0_222-ea-b03)
OpenJDK 64-Bit Server VM (build 25.222-b03, mixed mode)
[root@7CentOS ~]# vim /etc/profile
##末行添加
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@7CentOS ~]# source /etc/profile
[root@7CentOS ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Unzip Tomcat

[root@7CentOS tom]# tar zxvf apache-tomcat-9.0.16.tar.gz 
[root@7CentOS tom]# mv apache-tomcat-9.0.16 /usr/local/tomcat9

Let systemctl control tomcat

[root@7CentOS /]# vim /lib/systemd/system/tomcat.service 
[Unit]
Description=tomcat
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/tomcat9/logs/tomcat.pid
ExecStart=/usr/local/tomcat9/bin/startup.sh
ExecReload=/usr/bin/kill -S HUP $MAINPID
ExecStop=/usr/local/tomcat9/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target

root@7CentOS /]# netstat -natp |grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      6158/java  

Insert picture description here

tomcat directory structure

Insert picture description here

Tomcat startup optimization

Reduce tomcat startup time

[root@7CentOS /]# vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security 
搜索securerandom
securerandom.source=file:/dev/urandom

Tomcat virtual host

Install DNS...I really don’t want to write this
one. One domain name is www.ora.com and the
other is www.two.com

Create a homepage for two domains

[root@7CentOS named]# cd /usr/local/tomcat9/webapps/
[root@7CentOS webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@7CentOS webapps]# mkdir one
[root@7CentOS webapps]# cd one/
[root@7CentOS one]# vim index.jsp
[root@7CentOS one]# cd ..
[root@7CentOS webapps]# mkdir two
[root@7CentOS webapps]# cp -p one/index.jsp two/
[root@7CentOS webapps]# cd two/
[root@7CentOS two]# vim index.jsp

Edit tomcat main configuration file

[root@7CentOS two]# vim /usr/local/tomcat9/conf/server.xml 
搜素 Engine,手敲
<Host name="www.one.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
        <Context docBase="/usr/local/tomcat9/webapps/one" path="" reloadable="true"/>
      </Host>
      <Host name="www.two.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
        <Context docBase="/usr/local/tomcat9/webapps/two" path="" reloadable="true"/>
      </Host>
    </Engine>   ##在这行之上增加

verification
Insert picture description here
Insert picture description here

Tomcat common optimization

Common optimization parameters are as follows:
maxThreads: Tomcat uses threads to process each request received. This value represents the maximum number of threads that Tomcat can create. The default value is 200.
minSpareThreads: The minimum number of idle threads, the number of threads initialized when Tomcat starts, which means that there are so many empty threads waiting even if no one is using it. The default value is 10.
maxSpareThreads: The maximum number of spare threads. Once the created threads exceed this value, Tomcat will shut down socket threads that are no longer needed. The default value is -1 (unlimited). Generally do not need to be specified.
URIEncoding: Specify the URL encoding format of the Tomcat container. The language encoding format is not as easy to configure as other Web server software and needs to be specified separately.
connnectionTimeout: network connection timeout, unit: milliseconds, set to 0 means never time out, this setting has hidden dangers. Usually the default is 20000 milliseconds.
enableLookups: Whether to reverse check the domain name to return the host name of the remote host. The value is true or false. If it is set to false, it will directly return the IP address. In order to improve the processing capacity, it should be set to false.
disableUploadTimeout: Whether to use the timeout mechanism when uploading. Should be set to true.
connectionUploadTimeout: Upload timeout. After all, file upload may take more time. Adjust this according to your own business needs, so that the Servlet has a longer time to complete its execution. It needs to be used in conjunction with the previous parameter. Will take effect.
acceptCount: Specify the maximum queue length of incoming connection requests when all available threads for processing requests are used. Requests exceeding this number will not be processed, and the default is 100.
compression: whether to perform GZIP compression on the response data, off: means that compression is prohibited; on: means that compression is allowed (the text will be compressed), force: means that compression is carried out in all cases, the default value is off, which can be effective after compressed data Reducing the page size can generally be reduced by about 1/3 to save bandwidth.
compressionMinSize: Represents the minimum value of the compressed response. Only when the size of the response message is larger than this value will the message be compressed. If the compression function is enabled, the default value is 2048.
compressableMimeType: Compression type, which specifies which types of files are compressed.
noCompressionUserAgents="gozilla, traviata": For the following browsers, compression is not enabled.

To add

[root@localhost ~]# vim /usr/local/tomcat9/conf/server.xml 
<Connector port="8080" protocol="HTTP/1.1"		##找到此段落'
               connectionTimeout="20000"
               redirectPort="8443" />
##在 redirectPort="8443" 和/>中间添加以下段落'
minSpareThreads="50"
enableLookups="false" 
disableUploadTimeout="true" 
acceptCount="300" 
maxThreads="500" 
processorCache="500"
URIEncoding="UTF-8"
compression="on" 
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png"
[root@7CentOS conf]# systemctl restart tomcat

Guess you like

Origin blog.csdn.net/Ora_G/article/details/107971458