tomcat common interview questions 1

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

The default port number for Tomcat is 8080.

Modify the Tomcat port number:

1. Find the conf folder in the Tomcat directory

2. Go to 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

maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″

enableLookups=”false” redirectPort=”8443″ acceptCount=”100″

connectionTimeout=”20000″ disableUploadTimeout=”true” />

5. Change port=”8080” to port=”8888” and save

6. Start Tomcat, and enter http://127.0.0.1:8888/ in the address bar in the IE browser

7. The BIO model adopted by tomcat by default will have a serious performance drop under hundreds of concurrency. Tomcat comes with a NIO model, and it can also call the APR library to implement operating system level control.

The NIO model is built-in, and it is very convenient to call. Just modify the protocol in the above configuration file to org.apache.coyote.http11.Http11NioProtocol, and restart it to take effect. As the following parameter configuration, the default is HTTP/1.1.

<Connector port=”8080″

protocol=”org.apache.coyote.http11.Http11NioProtocol”

connectionTimeout=”20000″

redirectPort=”8443″

maxThreads=”500″

minSpareThreads=”20″

acceptCount=”100″

disableUploadTimeout=”true”

enableLookups=”false”

URIEncoding=”UTF-8″ />

2. How to optimize tomcat?

1. Optimize the connection configuration. Taking the parameter configuration of tomcat7 as an example, you need to modify the conf/server.xml file, modify the number of connections, and close the client dns query.

Parameter explanation:

URIEncoding=”UTF-8”: enables tomcat to parse the url of files with Chinese names, which is really convenient, unlike apache, where there is a mod_encoding, which needs to be compiled manually

maxSpareThreads : If the number of idle threads is more than the set number, these threads will be aborted, reducing the total number of threads in this pool.

minSpareThreads : The minimum number of spare threads, the number of initialized threads when tomcat starts.

enableLookups : This function is the same as HostnameLookups in Apache, set to off.

connectionTimeout : connectionTimeout is the network connection timeout time in milliseconds.

maxThreads : maxThreads Tomcat uses threads to process each request it receives. This value represents the maximum number of threads that Tomcat can create, that is, the maximum number of concurrency.

acceptCount : acceptCount is that when the number of threads reaches maxThreads, subsequent requests will be put into a waiting queue. The acceptCount is the size of the queue. If the queue is also full, the connection will be rejected directly.

maxProcessors and minProcessors: In Java, a thread is the path of the program runtime, and it is a code segment in a program that is independent of other control threads and can run independently. They share the same address space. Multithreading helps programmers write efficient programs that maximize CPU utilization, keep idle time to a minimum, and accept more requests.

Usually Windows is about 1000, Linux is about 2000.

useURIValidationHack:

Let's take a look at a piece of source code in tomcat:

【security】

if (connector.getUseURIValidationHack()) {

String uri = validate(request.getRequestURI());

if (uri == null) {

res.setStatus(400);

res.setMessage(“Invalid URI”);

throw new IOException(“Invalid URI”);

} else {

req.requestURI().setString(uri);

// Redoing the URI decoding

req.decodedURI().duplicate(req.requestURI());

req.getURLDecoder().convert(req.decodedURI(), true);

It can be seen that if useURIValidationHack is set to "false", it can reduce unnecessary checks on some urls and reduce overhead.

enableLookups=”false”: In order to eliminate the impact of DNS query on performance, we can disable DNS query by modifying the value of the enableLookups parameter in the server.xml file.

disableUploadTimeout : similar to keepalive in Apache

Configure gzip compression (HTTP compression) function for Tomcat

compression=”on” compressionMinSize=”2048″

compressableMimeType=”text/html,text/xml,text/javascript,text/css,text/plain”

HTTP compression can greatly improve the speed of browsing websites. Compared with the ordinary browsing process HTML, CSS, Javascript, Text, it can save about 40% of the traffic. More importantly, it can compress dynamically generated web pages including CGI, PHP, JSP, ASP, Servlet, SHTML, etc., with amazing compression efficiency.

1)compression=”on” to turn on the compression function

2)compressionMinSize=”2048″ Enable compressed output content size, which defaults to 2KB

3) noCompressionUserAgents=”gozilla, traviata” For the following browsers, compression is not enabled

4) compressableMimeType=”text/html,text/xml” compression type

Finally, don't forget to add the same configuration to port 8443, because if we go with the https protocol, we will use the configuration of port 8443, right?

<!–enable tomcat ssl–>

<Connector port=”8443″ protocol=”HTTP/1.1″

URIEncoding=”UTF-8″ minSpareThreads=”25″ maxSpareThreads=”75″

enableLookups=”false” disableUploadTimeout=”true” connectionTimeout=”20000″

acceptCount=”300″ maxThreads=”300″ maxProcessors=”1000″ minProcessors=”5″

useURIValidationHack=”false”

compression=”on” compressionMinSize=”2048″

compressableMimeType=”text/html,text/xml,text/javascript,text/css,text/plain”

SSLEnabled=”true”

scheme=”https” secure=”true”

clientAuth=”false” sslProtocol=”TLS”

keystoreFile=”d:/tomcat2/conf/shnlap93.jks” keystorePass=”aaaaaa”

/>

Well, all the Tomcat optimizations have been added.

2. Optimize JDK

The default memory that Tomcat can use is 128MB. Under Windows, in the file {tomcat_home}/bin/catalina.bat, under Unix, in front of the file $CATALINA_HOME/bin/catalina.sh, add the following settings:

JAVA_OPTS=”'$JAVA_OPTS” -Xms[initialized memory size] -Xmx[maximum memory that can be used]

or

Set environment variables: export JAVA_OPTS=””$JAVA_OPTS” -Xms[initialized memory size] -Xmx[maximum memory that can be used]”

In general, you should use 80% of physical memory as the heap size. If there is an Apache server on the local machine, you can convert the memory required by Apache first, and then modify the heap size. It is recommended to set it to 70%; it is recommended to set [[initialized memory size] equal to [maximum memory that can be used], which can reduce trivial allocation of heap and reduce performance.

This example uses the method of adding environment variables:

# vi /etc/profile

Add: export JAVA_OPTS=””$JAVA_OPTS” -Xms700 —Xmx700

# source /etc/profile

【Parameter Description】

-Xms means to set the memory size occupied when the program starts. Generally speaking, if it is larger, the program will start faster, but it may also cause the machine to temporarily slow down.

-Xmx means to set the maximum size of memory that can be occupied during the running of the program. If the program needs to take up more memory than this set value, an OutOfMemory exception will be thrown.

-Xss refers to setting the stack size for each thread. This depends on your program to see how much memory a thread needs to take up, how many threads may be running at the same time, etc.

-XX:PermSize sets the initial value of non-heap memory, the default is 1/64 of physical memory.

-XX:MaxPermSize sets the maximum non-heap memory size, the default is 1/4 of the physical memory.

3. Which Connector operation modes does tomcat have?

There are 3 operating modes of tomcat. Modify their operating modes. Whether the operation of the 3 modes is successful, you can see his startup console or startup log. Or log in to their default page http://localhost:8080/ to view them server status.

1)bio

The default mode, the performance is very low, without any optimization processing and support.

2)nio

Use java's asynchronous IO care technology, no blocking IO technology.

If you want to run in this mode, directly modify the Connector node in server.xml, and modify the protocol to

<Connector port=”80″ protocol=”org.apache.coyote.http11.Http11NioProtocol”

connectionTimeout=”20000″

URIEncoding=”UTF-8″

useBodyEncodingForURI=”true”

enableLookups=”false”

redirectPort=”8443″ />

After startup, it will take effect.

3)apr

It is the most difficult to install, but it solves the asynchronous IO problem from the operating system level and greatly improves the performance.

Apr and native must be installed, and apr is supported by direct startup. The following modifications are purely redundant, only for everyone to expand their knowledge, but still need to install apr and native

If nio modifies the mode, modify the protocol to org.apache.coyote.http11.Http11AprProtocol

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324902879&siteId=291194637