Carefully sort out the most comprehensive Tomcat interview topics and answers in the entire network. This is enough to read this tomcat interview!

1. What is the default port of Tomcat and how to modify it?

1) Find the conf folder under the Tomcat directory
2) Enter the conf folder and find the server.xml file
3) Open the server.xml file
4) Find the following information in the server.xml file

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" uriEncoding="utf-8"/>

port="8080" to the port you want

2. What kinds of connector operating modes (optimization) does tomcat have?

bio: Traditional Java I/O operations, synchronous and blocking IO. maxThreads=”150”//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. It can be adjusted according to the period performance and memory size of the machine, generally 400-500. The maximum can be around 800. minSpareThreads=”25”—The number of threads created when Tomcat is initialized. The default value is 4. If there are currently no idle threads and maxThreads is not exceeded, the number of idle threads created at one time. The number of threads created when Tomcat is initialized is also set by this value. maxSpareThreads=”75”-Once the created threads exceed this value, Tomcat will shut down socket threads that are no longer needed. The default value is 50. Once the number of threads created exceeds this value, Tomcat will shut down threads that are no longer needed. The number of threads can be roughly calculated by "the number of concurrent users operating per second, the average operating time of the system". acceptCount=”100”—Specify the number of requests that can be placed in the processing queue when all available threads for processing requests are used. Requests exceeding this number will not be processed. The default value is 10. If the number of currently available threads is 0, the request is placed in the processing queue. This value limits the size of the request queue. Requests exceeding this value will not be processed. connectionTimeout=”20000”-The network connection timed out, the default value is 20000, unit: milliseconds. Setting it to 0 means never timeout, so setting has hidden dangers. Usually can be set to 30000 milliseconds.

nio: JDK1.4 began to support, synchronous blocking or synchronous non-blocking IO. Specify the NIO model to accept HTTP requests protocol=”org.apache.coyote.http11.Http11NioProtocol” Specify the NIO model to accept HTTP requests. The default is BlockingIO, and the configuration is protocol=”HTTP/1.1” acceptorThreadCount=”2” The number of receiving threads when using the NIO model aio(nio.2): JDK7 starts to support, asynchronous non-blocking IO.

apr: Tomcat will call the core dynamic link library of the Apache HTTP server in the form of JNI to process file reading or network transmission operations, thereby greatly improving Tomcat's processing performance for static files.

<!--
 <Connector connectionTimeout="20000" port="8000"
protocol="HTTP/1.1" redirectPort="8443" uriEncoding="utf-8"/>
 -->
 <!-- protocol 启用 nio 模式,(tomcat8 默认使用的是 nio)(apr 模式利用系统级异步 io) -->
 <!-- minProcessors 最小空闲连接线程数-->
 <!-- maxProcessors 最大连接线程数-->
 <!-- acceptCount 允许的最大连接数,应大于等于 maxProcessors-->
 <!-- enableLookups 如果为 true,requst.getRemoteHost 会执行 DNS 查找,反向解析 ip 对应域名或主机名-->
 <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443 maxThreads=“500” minSpareThreads=“100” maxSpareThreads=“200” acceptCount="200" enableLookups="false"/>

Other configurations maxHttpHeaderSize=“8192” The maximum extent of http request header information, the part exceeding this length will not be processed. Generally 8K. URIEncoding="UTF-8" specifies the URL encoding format of the Tomcat container. disableUploadTimeout=“true” Whether to use the timeout mechanism when uploading. enableLookups=“false” – Whether to reverse check the domain name, the default value is true. In order to improve the processing capacity, it should be set to false compression="on" Turn on the compression function compressionMinSize="10240" Enable the compressed output content size, the default is 2KB noCompressionUserAgents="gozilla, traviata" For the following browsers, do not enable compression compressableMimeType= "Text/html,text/xml,text/javascript,text/css,text/plain"

3. How many deployment methods does Tomcat have?

1) Put the Web project directly under webapps, and Tomcat will automatically deploy it.
2) Configure the node on the server.xml file and set the relevant attributes.
3) Configure through Catalina: enter the conf\Catalina\localhost file Next, create an xml file whose name is the name of the site. The way to write XML to set.

4. How does the tomcat container create an instance of the servlet class? What principle was used?

When the container starts, it will read the web.xml file in all web applications under the webapps directory, then parse the xml file and read the servlet registration information. Then, load the servlet class registered in each application and instantiate it through reflection. (Sometimes it is also instantiated on the first request) In the servlet registration, if it is a positive number, it will be instantiated at the beginning. If it is not written or a negative number, it will be instantiated for the first time.

5. Memory tuning

The setting of the memory mode is in catalina.sh, just adjust the JAVA_OPTS variable, because the subsequent startup parameters will treat JAVA_OPTS as the startup parameters of the JVM. The specific settings are as follows:

JAVA_OPTS="$JAVA_OPTS -Xmx3550m -Xms3550m -Xss128k -
XX:NewRatio=4 -XX:SurvivorRatio=4"

The parameters are as follows: -Xmx3550m: Set the maximum available memory of the JVM to 3550M.

-Xms3550m: Set the JVM to cause the memory to be 3550m. This value can be set the same as -Xmx to avoid the JVM reallocating memory after each garbage collection is completed.

-Xmn2g: Set the young generation size to 2G. The entire heap size = young generation size + old generation size + persistent generation size. The permanent generation generally has a fixed size of 64m, so after increasing the young generation, the size of the old generation will be reduced. This value has a great impact on system performance. Sun officially recommends a configuration of 3/8 of the entire heap.

-Xss128k: Set the stack size of each thread. After JDK5.0, the stack size of each thread is 1M, and the stack size of each thread is 256K before. Adjust the memory size required by the application thread. Under the same physical memory, reducing this value can generate more threads. However, the operating system still has a limit on the number of threads in a process, which cannot be generated indefinitely. The experience value is around 3000~5000.

-XX:NewRatio=4: Set the ratio of the young generation (including Eden and two Survivor areas) to the old generation (excluding the permanent generation). Set to 4, the ratio of the young generation to the old generation is 1:4, and the young generation accounts for 1/5 of the entire stack

-XX:SurvivorRatio=4: Set the ratio of the size of the Eden area to the Survivor area in the young generation. Set to 4, the ratio of two Survivor areas to one Eden area is 2:4, and one Survivor area occupies 1/6 of the entire young generation -XX:MaxPermSize=16m: Set the persistent generation size to 16m.

-XX:MaxTenuringThreshold=0: Set the maximum age of garbage. If it is set to 0, the young generation object will enter the old generation directly without passing through the Survivor area. For applications with more old generations, efficiency can be improved. If this value is set to a larger value, the young generation object will be copied multiple times in the Survivor area, which can increase the survival time of the object in the young generation and increase the general theory of being recycled in the young generation.

6. Shared session processing

The current processing methods are as follows:
1). Use Tomcat's own Session replication function to refer to http://ajita.iteye.com/blog/1715312 (Session replication configuration). The solution is that the configuration is simple, but the disadvantage is that it is a cluster When the number is large, the session copying time will be longer, which affects the efficiency of response

2). Use a third party to store shared sessions. At present, memcached is used to manage shared sessions. With the help of memcached-sesson-manager, Tomcat's session management refers to http://ajita.iteye.com/blog/ 1716320 (Use MSM to manage Tomcat cluster session)

3). In situations where the sticky session strategy is not very demanding for the session (no billing is involved, re-request is allowed if it fails, etc.), the same user's session can be processed by nginx or apache to the same Tomcat , This is the so-called session sticky strategy, and there are more references for current applications: http://ajita.iteye.com/blog/1848665 (tomcat session sticky) nginx does not include session sticky modules by default, and it needs to be recompiled. The advantage is to deal with it. The efficiency is much higher, the disadvantage is that it is not suitable for occasions with strong conversation

7. Add JMS remote monitoring

For Tomcat deployed on other machines in the LAN, you can open the JMX monitoring port, and other machines in the LAN can view some commonly used parameters through this port (but some more complex functions are not supported). The same is configured in the JVM startup parameters. Yes, the configuration is as follows: -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.71.38 Set the IP address of the JVM monitoring of the JVM , The main purpose is to prevent the erroneous monitoring of the intranet address of 127.0.0.1 -Dcom.sun.management.jmxremote.port=1090 Set the port of JVM monitoring by JMS -Dcom.sun.management.jmxremote.ssl=false Set the JVM JMS monitoring is not practical SSL -Dcom.sun.management.jmxremote.authenticate=false Set JVM monitoring without authentication

8. There are professional analysis tools

IBM ISA, JProfiler, probe, etc., just go to the Internet to search for specific monitoring and analysis methods

9. About the number of Tomcat sessions

This can be viewed directly from Tomcat's web management interface; or with the help of a third-party tool, Lambda Probe, which has slightly more functions than Tomcat's own management, but not much;

10. Tomcat working mode?

Tomcat is a JSP/Servlet container. As a servlet container, it has three working modes: independent servlet container, in-process servlet container and out-of-process servlet container.
Requests to enter Tomcat can be divided into the following two categories according to the working mode of Tomcat: Tomcat as an application server: The request comes from the front-end web server, which may be Apache, IIS, Nginx, etc.; Tomcat as an independent server: the request comes from web browsing Device

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/112781048