Performance testing - Tomcat monitoring and tuning: Tomcat structure system

Tomcat was originally developed by James Duncan Davidson, a software architect at Sun. He later helped turn it into an open source project and was contributed by Sun to the Apache Software Foundation. Tomcat server is a free and open source web application server. It is a lightweight application server and is commonly used in small and medium-sized systems and situations where there are not many concurrent access users. It is the first choice for developing and debugging JSP programs. For a beginner, you can think of it this way: when the Apache server is configured on a machine, Tomcat is an extension of the Apache server, but it runs independently when running, so when running Tomcat, it actually acts as an extension of the Apache server. Apache runs independently as a separate process.

This chapter mainly introduces the following parts:

  • Tomcat structure system.
  • Tomcat monitoring.
  • Tomcat tuning
  • JVM tuning
  • Log file analysis

[Tomcat structure system]

Before introducing Tomcat monitoring and tuning, let us first introduce the structural system of Tomcat. The structure of Tomcat usually includes: Context, Connector, Host, Engine, Service, Server and Listener . As shown in Figure 11-1.

Figure 11-1 Tomcat structural system

These components are mainly configured using the server.xml file. By default, the server.xml file mainly contains the following configuration content.

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost"? appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>

1) Context

A context is a set of internal elements, called a container. Representing a single web application, Tomcat automatically instantiates and configures some standardized information when loading the application. Properties defined in the \WEB-INF\web.xml file are also processed and used in the application as part of the configuration.

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

 

2) Connector

Connectors are used to handle communication with clients. Tomcat supports a variety of connectors, such as the HTTP connector and AJP connector that we use most.

By default, Tomcat has been configured with an HTTP connector, and the port number is 8080, which defaults to the URL address http://localhost:8080. It should be noted that all applications are completed through a single instance of this connection. When sending a request, a thread is instantiated and this thread remains active in the connector for the duration of the request.

connectionTimeout represents the connection timeout. The default value is 20000, which means that if there is no activity in the session within 20000 seconds, the connection will be terminated. redirectPort represents the port number 8443 for secure socket SSL transmission.

Usually you will also see AJP connectors in the configuration file. If you process some dynamic web pages and allow the pure HTML server to process requests for static pages, this can improve the efficiency of processing requests, but it is generally used less now because now The Tomcat server itself is very fast, so if you don't need this connector, you can comment out this connector.

3) Host

Host represents the defined host, which is used to associate with the Tomcat server. Of course, how to contact is mainly determined by the domain name and IP address mapping rules. A server can define multiple hosts. For example, if the domain http://chuansinfo.com has been registered , host names can be defined such as w1. http://chuansinfo.com and w2. http://chuansinfo.com .

The default host name of Tomcat is localhost. The association between localhost and your computer is achieved through a record. This record is written in the C:\\Windows\\System32\\drivers\\etc\\hosts file.

The "appBase" parameter in the Host defines the directory location where the Ixin publishing application is located. When we use the URL externally to access the server, we actually directly access the directory corresponding to appBase. Take the above server.xml file configuration as an example. If the entered URL address is http://localhost:8080, then the equivalent access address is the index file in the http://localhost:8080/ webapps directory.

The "unpackWARs" parameter in Host indicates the method of processing WAR files. If set to "true", it means that if the WAR file is placed in the appBase directory, Tomcat will automatically expand it into an ordinary file; if set to "false" ", the application will run directly from the WAR file, which will affect the running speed because the Tomcat server must first decompress the WAR file before running the file.

If the "autoDeploy" parameter in the Host is set to "true", it means that the program placed in the appBase directory will be automatically deployed.

4) Engine

Engine belongs to the global engine container. It mainly abstracts all applications in the global engine containers of different JVMs into clusters, allowing them to communicate with each other between different JVMs, enabling session synchronization and cluster deployment. Engine is used to process all requests in the connector and return the response results to the client. Of course, there may be multiple connectors.

The Engine container contains one or more Host components. The content of each Host component is introduced in the above content. Each Host component is equivalent to a virtual machine. The Engine engine also has Container, which is the parent interface of the container. Connector requests are passed in through this parent interface.

The main components of the Engine container include: Host component, AccessLog component, Pipeline component, Cluster component, Realm component, LifecycleLister component and Log component.

5) Service

The Service component can be seen as an abstraction of different services in Tomcat, binding one or more connectors to an engine. Tomcat's default configuration service is Catalina. Under normal circumstances, we will not modify or define the Service.

The Service component includes two types of components: several Connector components and Executor components. The Connector component is responsible for monitoring customer requests on a certain port. Different ports correspond to different Connectors. The Executor component provides a thread pool at the Service abstraction level, allowing components under the Service to share the thread pool.

6) Server

The Server component can be seen as an abstraction of the running instance of Tomcat. It is the top-level component and can contain one or more Services.

The Server component contains 6 listener components: AprLifecycleListener listener, JasperListener listener,
JreMemoryLeakPreventionListener listener, GlobalResourcesLifecycleListener listener, ThreadLocalLeakPreventionListener listener and NamingContextListener listener. Also includes GlobalNamingResources components, ServerSocket components and Service components.

7) Listener

The listener is a Java object that is
controlled by implementing the org.apache.catalina.LifecycleListener interface. The listener is mainly used to listen to the request information submitted by the client. It can handle the following specific events.

● AprLifecycleListener

This listener can listen to APR (Apahce Portable Runtime) very well, and the APR library can better improve the performance of Tomcat service back to the operating system.

● jasperlister

JSP Listener It is JSP engine. This listener can recompile updated JSP documents.

● JreMemoryLeakPreventionListener:

This listener is mainly used to handle situations that may lead to memory leaks.

● GlobalResourcesLifecycleListener:

Mainly responsible for listening and managing the association between instantiation and the global Java Naming and Directory Interface (JNDI).

● ThreadLocalLeakPreventionListener:

Similar to
the JreMemoryLeakPreventionListener listener, it is also used to handle memory leaks.

The above describes the structural system of Tomcat. Let's introduce the working principle of Tomcat. The working principle of Tomcat is shown in Figure 11-2.

Figure 12-2 Working principle of Tomcat

The specific steps of how Tomcat works are as follows:

1. When the user clicks on the web page, the client will send the request to the local port 8080, which will be obtained by the Coyote HTTP/1.1 Connector listening there.

2. The Connector passes the request to the Engine of the Service where it is located for processing, and waits for the Engine's response.

3. Engine obtains the requested resource, such as index.jsp, and matches all virtual hosts.

4. Engine matches the Host named localhost (even if it cannot match, the request will be handed over to the Host for processing, because the Host is defined as the default host of the Engine). The Host named localhost obtains the information of the requested resource, such as /test/index.jsp, matches all Contexts it owns. The Host matches the Context with the path /test (if it cannot match, the request is handed over to the Context with the path name " " for processing).

5. The Context with path="/test" obtains the request/index.jsp and finds the corresponding Servlet in its mapping table. Context matches the Servlet whose URL PATTERN is *.jsp, corresponding to the JspServlet class.

6. Construct the HttpServletRequest object and HttpServletResponse object, and call doGet() or doPost() of JspServlet as parameters. Execute business logic, data storage and other procedures.

7. Context returns the HttpServletResponse object after execution to the Host.

8. Host returns the HttpServletResponse object to Engine.

9. Engine returns the HttpServletResponse object to the Connector.

10. Connector returns the HttpServletResponse object to the client Browser.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/133102090