Tomcat deployment optimization

Tomcat

Tomcat, an open source web application server, is developed by java code.
Tomcat handles dynamic requests and page development based on java code
. You can write java code in html, tomcat can parse iava in html pages, and execute dynamic request
dynamic page mechanism There is a problem: if tomcat is not optimized, it will appear in: suspended animation, downtime
Small clusters, small services, and small applications are used in tomcat; large concurrency scenarios are not suitable
 

1. Free
2. Open source, can be repackaged.
3. Strong configurability: custom configuration can be performed according to needs, including port number, virtual host, etc. 4.
Security: Tomcat has its own security mechanism, which can configure user authentication, authorization, and encrypted transmission
5. Deploying applications is very fast : Tomcat will automatically deploy and run automatically

Tomcat core components

web container

Complete the function of web server, web application

Working method: web→http or https→access page→point to the file index.jsp

servlet container

The name (catalina) is used to process servlet code, which is to process web requests (http), and Java classes that generate dynamic content, to process http requests
 

Function:

  • Handle http requests 
  • Generating dynamic content is to interact with the database in jsp initiated by the user   
  • Session management: Track the user's status between different requests, and through management, the user's status can be maintained when the user visits different pages
  • To interact with the database, servlets can connect to the database, perform queries and update operations

 

jsp container

jsp dynamic page, translate it into servlet code, display jsp static page in standard format

  • java server pages dynamic page development technology, using jsp tags (index.jsp)
  • Insert java code in html page
  • Java container→java code translation in html page→execution→display results

 

Tomcat functional components 

Connector

Responsible for receiving and responding to external requests. It is the communication hub between Tomcat and the outside world. The listening port receives external requests, processes the requests and passes them to the container for business processing, and finally responds the processed results of the container to the outside world.

Container

Responsible for internal processing of business logic. It consists of four containers, Engine, Host, Context and Wrapper, which
are used to manage and call Servlet-related logic; there are four widgets and their functions:

Engine

Engine, used to manage multiple virtual hosts, a Service can only have at most one Engine

Host

Represents a virtual host, which can also be called a site, and a site can be added by configuring the Host

Context

Represents a web application that contains multiple Servlet wrappers

Wrapper

Wrapper, the bottom layer of the container. Each Wrapper encapsulates a Servlet, which is responsible for the creation, execution and destruction of object instances

Summarize

The working method of these four components is also from top to bottom. The working order of the four containers belongs to the parent-child relationship; Tomcat has two core functions, namely the Connector responsible for receiving and feeding back external requests, and the connector responsible for The container Container that handles the request; the connector and the container complement each other to form the basic web service Service; each Tomcat server can manage multiple Services
 

work process

The user enters the URL in the browser, and the request is sent to the local port 8080. The Connector monitored there obtains the Connector and
hands the request to the Engine (Container) of the Service where it is located for processing, and waits for the response
from the Engine. , Host, Context, and Wrapper are called layer by layer between the four containers,
and finally the corresponding business logic, data storage, etc. are executed in the Servlet.
After the execution, the request response is returned layer by layer between the Context, Host, and Engine containers, and finally returned to Connector
and return to the client through the Connector

 

Tomcat configuration file

bin: store Tomcat startup or shutdown script files

conf: the main configuration file, which contains several important files

  • server.xml: main configuration file
  • contex.html: default configuration information of host
  • tomcat - usr.xml: configuration file for authenticating user passwords
  • web.xml: configure servlet and its standard files

lib: The jar package of the runtime library (generally does not move)

logs: log files

webapps: default deployment directory for web applications

work: Tomcat's working directory, storing jsp compiled class files, used to clear Tomcat cache and my wrok directory

 

JVM 

JVM is the virtual machine of java, which is the core component of java application running on computing. The jvm load will compile the iava bytecode

(.class file) is interpreted or compiled into local machine code, and the computer can execute it. JVM also provides good memory management, garbage

Recycling, thread management; ensuring that java programs can have consistent functions on different platforms.
 

JVM optimization configuration

First install the Java programming package, Tomcat

 Set JDK environment variables

 

Set user password to log in to manager

 Allow all users to access

 

 

Heap memory Non-heap memory

PS Eden Space: Heap memory: Store newly created objects.
Psold Gen: Heap memory, storing long-lived objects
Ps Survivor Space: Heap memory: Objects with a short life cycle after creation
Code Cache: Non-heap memory: Store compiled code
CompressedClass Space: Non-heap memory, store compiled code The compressed class
Catalina.sh is the servlet code and the configuration of the container

ajp-nio-8009

ajp-nio: connector type

ajp: use the ajp protocol; connect the tomcat server with the front-end web server. Provides load balancing and efficient request forwarding

nio: perform asynchronous blocking

8009: The port the connector is listening on. connect

http-nio-8080

Port for handling http requests and client communication

JVM optimization configuration

 increase memory

Explanation of the configuration commands:

AVA OPTS="$/AVA OPTS -server -Xms2048m -Xmx2048m -Xmn768m -xx:ParallelGCThreads=2 -Xx:PermSize=1024m.XX:MaxPermSize=1024m -Djava.awt.headless=true -XX:+DisableExplicitGC"

-server: The first parameter specifies the memory pool, which must be placed first.
-Xms2048m: the size of the initial java heap, the minimum memory allocated to the JVM depends on the high performance of the cpu, this value can be set higher
-Xmx2048m: the maximum size of the JAVA heap, the maximum memory of the jvm. Linked to hardware memory. According to the official documentation, the starting value and the maximum value are consistent. XMS and XMX are set to be the same, and the memory can be set to the general physical memory.
-Xmn768m : The memory size of the new generation. The official recommendation is for 3/8 of the entire heap size.
-XX:ParallelGCThreads=2: Configure the number of threads for the parallel collector, and how many threads can be garbage collected at the same time
-XX:PermSize: Set the size of the persistent generation memory, the default is physical 1/4 of memory.
-XX:MaxPermSize=1024m : The size of the largest non-heap memory, which is also 1/4 of the physical memory by default;
-XX:PermSize=1024m -XX:MaxPermSize=1024m
The non-heap memory will not be processed by the garbage collection mechanism,- XX:PermSize persistent generation memory and maximum non-heap memory cannot exceed the memory available to the operating system. Setting it to the same size can reduce the pressure on the scaling heap size.
-Djava.awt.headless=true: To prevent the web from being opened normally in the linux environment to display pictures normally.
-XX:+DisableExplicitGC" : Avoid jvm space ups and downs, affecting the response time of the system, and the response speed will be very slow

heap area

The heap area is divided into: Cenozoic, Mesozoic, Old, and Permanent

  • New generation: is the space occupied by each new object
  • Mesozoic: The Java garbage collection mechanism recycles resources in the heap area. The resources that have not been recovered in the new generation are the Mesozoic
  • Old generation: It is the resources that have not been recovered in the Mesozoic generation

JVM size: Cenozoic + Mesozoic + Old + Permanent

 

Tomcat virtual host configuration

Multiple virtual machine servers will be deployed in one tomcat on one server, in order to reduce the resource consumption of the system

 

Command analysis:

<Host  name="www.kgc.com" appBase="webapps” unpackWARs="true" autoDeploy="true" xmlValidation="false" xmINamespaceAware="false">

            <Context docBase="/usr/local/tomcat/webapps/kgc" path=""  reloadable="true" />

</Host> 

<Host  name="www.benet.com" appBase="webapps” unpackWARs="true" autoDeploy="true" xmlValidation="false" xmINamespaceAware="false">

            <Context docBase="/usr/local/tomcat/webapps/benet" path=""  reloadable="true" />

</Host>

hostname: host name
appBase: the directory where web programs are stored
unpackWARs: expand war files first, and the default is true.
autoDeploy: As long as tomcat is running, the files in webapps will be automatically deployed. The default is also true
xmIValidation: Whether to verify the validity of the xml file. The default is false
xmINamespaceAware: Whether to verify the namespace
<Context docBase="/usr/local/tomcat/webapps/kgc:
www.kgc.com is to visit /usr/local/tomcat/webapps/kgc/index.jsp

 

Tomcat optimization

The default configuration under the default installation of Tomcat is not suitable for the production environment, it may frequently appear suspended animation and needs to be restarted

Only through continuous stress testing and optimization can it run at the highest efficiency and stably

Optimization mainly includes three aspects, namely, operating system optimization (kernel parameter optimization), Tomcat configuration file parameter optimization, and Java virtual machine (JVM) tuning

Optimization of startup speed:

Modify JDK parameters 

[root@gsn5 bin]# vim /usr/java/jdk1.8.0 201-amd64/jre/lib/security/java.security
 
117 securerandom.source=file:/dev/urandom
# urandom 表示异步非阻塞

 optimization of server.xml to the main configuration file

redirectPort: If the protocol supported by a certain connector is http, this port will be enabled when a request from the client is accepted - 8443
maxthreads: Tomcat uses threads to process requests, and the maximum number of threads that can be created is concurrent Number of connections; default 200
minsparethreads: the minimum number of idle threads, how many threads there will be when tomcat is started. The default is 10
maxsparethreads: maximum spare threads. Once the number of threads created exceeds this value, tomcat will close port threads that are no longer needed. The default is -1, which means no limit. Generally do not specify
URIEncoding: URL format encoding, default utf-8. It needs to be specified separately, so generally do not change
connectionTimeout: network connection timeout, unit: milliseconds. 0, never timeout, generally 20000 milliseconds.
enablelookups: Whether to reverse resolve the domain name, in order to be able to obtain the remote host name. Generally set to false, directly return the IP address. Improved processing power.
disableUploadTimeout: upload timeout time, set to true.
connectionUploadTimeout: varies from person to person, there is no standard answer, 1 minute, 30 seconds
acceptCount: all available threads are occupied, the maximum queue length for incoming connection requests. The default is 100. 101 out of range, directly discarded, no longer processed.
compression: Whether to enable the compression of page and response data. 0ff off n on force (compression is performed in all cases.) The default is of Suggestion on
compressionMinSize: The minimum value of the compression response, equal to or less than this value, no compression will be performed. 2048
compressableMimeType: The type of MIME that can be compressed; the protocol for processing files between browsers and other Internet applications and the nature and format of documents that need to be processed

Text: text/html, text/plain
Image: image/jpgimage/gif
Audio: audio/wav audio/mp3
Video: video/mp4 video/avi
Application: application/pdf application/json
  

Guess you like

Origin blog.csdn.net/ZWH9991/article/details/132295227