Detailed configuration and tuning Tomcat

Service.xml

Server.xml associated profile for the entire container configuration.

<Server> element: root element is the entire configuration file. Catalina represents the entire container.

Properties: className: name org.apache.catalina.Server class that implements the interface, the standard implementation class is org.apache.catalina.core.StandardServer class. Port: Tomcat server listens for closing the Tomcat server commands (must) Shutdown: Send to shut down the port for the Tomcat server's command.

例:<Serverport=”8005” shutdown=”SHUTDOWN”>

<Connector> element: the connector is responsible for receiving client requests, and send to the client side a response message back.

Connector HTTP: Properties:

  • allowTrace: whether to allow HTTP TRACE method, the default is false
  • emptySessionPath: If set to true, all paths users will be set to /, the default is false.
  • enableLookups: call request, getRemoteHost () performs a DNS query to return the remote host's host name, if set to false, returns the IP address directly.
  • maxPostSize: Specifies the maximum amount of a POST request, does not specify the default is 2097152.
  • protocol: value must be the HTTP1.1, AJP processor if used, the value must be AJP / 1.3
  • proxyName: As the connector is a configuration agent is used, this attribute is returned (time) at request.getServerName
  • redirectPort: The connector does not support SSL requests, as received an SSL request, the container will be automatically redirected Catalina specified port number, let it processed.
  • scheme: Set the name of the agreement, return (when) in request.getScheme, SSL connector is set to "https", the default is "http"
  • secure: the SSL connection may be set to true, the default is false
  • URIEncoding: URL decoding the character encoding used, there is no default value is ISO-8859-1
  • useBodyEncodingForURI: Tomcat4.1.x mainly used in indicating whether the encoding specified in contentType the URIEncoding be substituted, for decoding the URI query parameters, defaults to false
  • xpoweredBy: is true, Tomcat Use the recommended header indicates that support Servlet specification version, the default is false
  • acceptCount: When all the possible processing threads are in use, the maximum number of queued requests in the queue. When the queue is full, any received requests are denied, the default value is 10
  • bufferSize: Create a size of the input stream buffer provided by the connector, in bytes. By default, a large buffer size is 2048 bytes
  • compressableMimeType: MIME list of default separated by commas. The default value is text / html, text / xml, text / plain
  • compression: Specifies whether response data is compressed. off: indicates prohibition of compression, on: Permits compression (the text to be compressed), force: represents all cases are compressed, a default is off
  • connectionTimeout: setting the timeout value of the connection, in milliseconds. The default value is 60000 = 60 seconds
  • disableUploadTimeOut: Allow Servlet container, is performed using a longer connection time-out value, so Servlet longer time to complete its execution, the default is false
  • maxHttpHeaderSize: maximum amount of HTTP request and response header, in bytes, the default value is 4096 bytes
  • maxKeepAliveRequest: before the server shuts down, the maximum number of lines sent by the client. The default value is 100
  • maxSpareThreads: Maximum number of idle threads exist, the default value is 50
  • minSpareThreads: set when the connector is first started co-create the number of threads, ensure that at least so many idle threads available. The default value is 4
  • port: server socket listening on TCP port number, the default is 8080 (must)
  • socketBuffer: Socket set output buffer size (in bytes) - ban 1 represents a buffer, the default value is 9000 bytes
  • toNoDelay: is true, you can improve performance. The default value is true
  • threadPriority: set the JVM thread priority request processing. The default value is NORMAL-PRIORITY

Example:

<Connector port="8080" address="localhost"maxThreads="250" maxHttpHeaderSize="8192"emptySessionPath="true" protocol="HTTP/1.1"enableLookups="false" redirectPort="8181" acceptCount="100"connectionTimeout="20000" disableUploadTimeout="true" />

AJP connectors: for integration with the Apache Tomcat together when the dynamic content request received by Apache, in the configuration specified by the request to the port number on this port to listen AJP connector assembly.

Attributes:

  • backlog: when the request a maximum number of requests of all possible processing threads are in use, the queue queuing. The default is 10, when the queue is full, any request will be denied
  • maxSpareThread: Maximum number of idle threads exist, the default value is 50
  • maxThread: maximum number of threads, the default value is 200
  • minSpareThreads: set when the number of threads created when the connector is started for the first time, ensure that at least so many idle threads available, the default is 4
  • port: server socket TCP port number, the default is 8089 (must)
  • topNoDelay: is true, can improve performance, the default is true
  • soTimeout: time-out value

Example:

<!—Define an AJP1.3 Connector on port 8089--><Connector port=”8089” enableLookups=”false” redirectPort=”8443” protocol=”AJP/1.3” />

 

<Engine> element: handle all referrals for a particular Service. Each Service Engine can only contain one element, which is responsible for receiving and processing all this Service receives a request for connection, sends back a response to the connection, and finally displayed on the client. <Engine> at least one <Host> element, must have at least a name <Host> and defaultHost specified attribute name matches.

Properties: className: org.apache.catalina.Engine implement the interface, the default implementation class for class org.apache.catalina.core.StandardEngine defaultHost: default hostname, the value must match the <Service> value of the name name: Name of the Engine logical name (required) jvmRoute: used in load balancing shim identifier must be unique

例:<Engine name=”Cataline” defaultHost=”localhst”>

<Host> element: represents a virtual host to handle all requests for a particular virtual host

Attributes:

  • appBase: base directory application is set, with respect to an absolute path or a path name% CATALINA_HOME%
  • autoDeploy: indicates Tomcat runtime, if the opening plus the new WEB program appBase specified directory, whether the automatic deployment, the default is true
  • className: org.apache.catalina.Host class implements an interface, the standard implementation class for the class org.apache.catalina.core.StandardHost
  • deployOnStartup: When Tomcat starts, whether to automatically deploy all WEB applications under appBase attribute specifies the directory, the default value is true
  • name: network name of the virtual host (must)
  • Standard Host implementation class org.apahce.catalina.core.StandardHost support additional properties:
  • deployXML: context.xml is false will not resolve internal WEB applications, the default value is true
  • unPackWARs: Web Hosting designated temporary directory path name used to read and write, no, Tomcat will provide a suitable directory under the% CATALINA_HOME% / work directory.

例:<Host name=”localhst” appBase=”webapps” unpackWARs=”true” autoDeploy=”true” xmlValidation=”false” xmlNamespaceAware=”false”>配置虚拟主机:<Hostname=”xxx” appBase=”c:/test”> <Contentpath=”” docBase=”e:/abe”/></Hostname>

 

<Context> elements: a WEB application, handles all of the current requests WEB application, each <Context> must have a unique context path.

Attributes:

  • className: org.apache.catalina.Context class implements an interface, the standard implementation class org.apache.catalina.core.StandardContext class
  • cookies: Cookie whether to apply to Session, the default value is true
  • crossContext: whether to allow cross-domain access, is true, the call ServletContext.getContext () method within the program returns to other programs on a virtual web host requests scheduler. Default is false, by adjusting the getContext diameter () returns null
  • docBase: Host an absolute path or a relative path relative properties appBase
  • privileged: true, enables Web applications using Servlet container
  • path: the path specifying the context. A virtual host, the context path must be unique
  • reloadable: is true, Tomcat runtime, if the WEB-INF / classes and WEB-INF / lib directory has changed, Tomcat will automatically reload the WEB application. Although convenient, but cost is also large, the default value is false, we call can open, close and then release.
  • cacheMaxSize: maximum static resource cache, in KB, the default value is 10240KB
  • cachingAllowed: whether to allow caching static resources, the default is true
  • caseSensitive: The default is true, the resource file names are case-sensitive, if false case-insensitive
  • unpackWAR: The default is true
  • workDir: Specifies the temporary directory path for read and write internal WEB application Servlet. If not set, Tomcat is provided at an appropriate directory% CATALINA_HOME% / work directory

例:<Content path=”/abc” docBase=”d:/xyz” reloadable=”true” />

 

Tomcat Performance Optimization finishing

 

Consider this scenario, you develop an application, it has a very good layout design, the latest features and other outstanding features. But in this regard the lack of performance, regardless of how the client application would have been rejected. Customers should always expect their applications have better performance. If you are using Tomcat server in the product, then this article will give you several aspects to improve the performance of the Tomcat server. ITWorld article thanks to the resources provided herein. After meditation I already know and provide better stability and performance compared to earlier versions of the latest Tomcat. So always use the latest version of Tomcat. As used herein, the following steps now to improve the performance of the Tomcat server.

  1. Increase the JVM heap memory size
  2. JRE memory leak repair
  3. Thread pool settings
  4. compression
  5. Database Performance Tuning
  6. Tomcat native library
  7. Other options

 

The first step - to improve the JVM stack memory Increase JVM heap memory

Tomcat you have used it, simply means that "out of memory". Typically, this problem occurs in the actual production environment. The reason for this problem is to use less memory tomcat process, through the configuration of Tomcat profiles (catalina.sh catalina.bat under Linux or under Windows) can solve this problem. this solution is by increasing the JVM stack memory to achieve. that is, JVM usually do not call the garbage collector, so the server can handle web requests more attention, and asked to complete as soon as possible. To change the file (catalina.sh) located in "\ tomcat server folder \ bin \ catalina.sh", below, gives information about the configuration file,

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8-server -Xms1024m -Xmx1024m-XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m-XX:MaxPermSize=512m -XX:+DisableExplicitGC"

-Xms - Specifies initialization of stack memory

-Xms - Specifies initialization of stack memory -Xmx - Specifies the maximum stack memory after restart your Tomcat server, these changes will be effective configuration. Here's how to deal with JRE memory leak.

 

The second step - to solve the JRE memory leaks

Another main reason for poor performance is a memory leak, as I said before: always use the latest tomcat server for better performance and scalability. Now, these words become true. If we use the latest tomcat version 6.0.26 and above can solve this mistake, because it contains a listener to handle JRE and PermGen memory leaks. Use listener is

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

You can find this listener configuration, server.xml position in the "tomcat project folder / conf / server.xml" in the server.xml file. Next, we will look at how to adjust the connection properties "maxThreads".

 

The third step - the thread pool settings

Thread Pool load specified number of Web requests, therefore, in order to obtain better performance this section should be handled with care. Setting can be done by adjusting the connection properties "maxThreads". MaxThreads value should be based on the size of the flow, and if the value is too low, there will not have enough threads to handle all the requests, the request will enter a waiting state only after the release of a processing thread to be processed; if set too large, Tomcat startup will take more time. So it is up to us to maxThreads set a correct value.

<Connector port="8080" address="localhost"maxThreads="250" maxHttpHeaderSize="8192"emptySessionPath="true" protocol="HTTP/1.1"enableLookups="false" redirectPort="8181" acceptCount="100"connectionTimeout="20000" disableUploadTimeout="true" />

In the above configuration, the maxThreads value is set to "250", which may be specified by the request server concurrently processed the maximum number. If not specified, the default value of this property is "200." Any extra concurrent requests will receive the error message "Connection refused" until another process the request process is released. Error looks like,

org.apache.tomcat.util.threads.ThreadPool logFull SEVERE: All threads (250) arecurrently busy, waiting. Increase maxThreads (250) or check the servlet status

If the application prompts the above error, be sure to check whether the above error is due to a single request to take too long due to the reason of the problem is that, sometimes if the database connection is not released, then the process will not process other requests.

Note: If the number of requests exceeds "750", which would mean that the property value is set maxThreads "750", it is best to use multiple instances of an accident with "Tomcat clusters". That is, if there is "1000" request, two examples provided Tomcat "maxThreads = 500", without setting maxThreads = 1000 in the case of a single Tomcat instance. In my experience, the exact value can be set by applying the test results in various environments. Next, we look at how to compress MIME type.

Step 4 - Compression

There is a setting in the server.xml Tomcat configuration file through compression options. Compression can be accomplished as follows is provided in the connector,

​​​​​​
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8181" compression="500" compressableMimeType="text/html,text/xml,text/plain,application/octet-stream" />

In the foregoing configuration, when the file size is greater than or equal 500bytes before compression. If and when the file reaches the size but not compressed, then set the properties of compression = "on". Otherwise Tomcat default setting is "off". Next we will look at how to tune the database.

Step 5 - Database Performance Tuning

Tomcat performance is reduced while waiting for database query is executed. Today, most applications are likely to include the use of "named query" relational database. If that's the case, Tomcat will be loaded by default at startup named queries, this may improve performance. Another important thing is to ensure that all database connections properly closed. Set the correct value to the database connection pool is very important. I said value is the maximum number of idle Resource elements (maxIdle), the maximum number (maxActive) connection, the maximum waiting time to establish a connection (maxWait) value of the property. Because the configuration-dependent and application requirements, I can not specify the correct value in this article. You can find the correct value by calling database performance testing.

Step 6 - Tomcat Native Library

Tomcat native library (Apache Portable Runtime referred to as APR) Apache Portable Runtime-based programmer to provide superior scalability and performance, help native integration server technology to show the best performance in product operation. Friends want to know the installation instructions, refer to the Tomcat Native Library - (APR) Installation.

Step 7 - Other Options

These options are:

  • Open your browser's cache, so read stored in the webapps folder of static content will be faster, greatly promote the overall performance.
  • Whenever the boot, Tomcat server should be automatically restarted.
  • Generally HTTPS requests slower than HTTP request. If you want better security, even though we still have to choose a little slower HTTPS.

~~~~~~~~~~~~~~~~Tidied up by Travis.Tang on 2013.11.07~~~~~~~~~~~~~~

Enable GZIP compression set TOMCAT

 

Introduction Principles

HTTP compression can greatly improve the speed of browsing the site, its principle is, after the client requests the server corresponding resource from the server-side resource file compression, and then output to the client, the client's browser is responsible for decompressing and browsing. Relative to normal browsing HTML, CSS, Javascript, Text, it can save about 40% of the traffic. More importantly, it can dynamically generated, including CGI, PHP, JSP, ASP, Servlet, SHTML pages and other output can be compressed, the compression efficiency is also high.

 

Configuration Shenzhen surrogate prestige 15,023,219,993  Kunming surrogate prestige 15,023,219,993  Shanghai surrogate prestige 15,023,219,993  Tianjin surrogate prestige 15,023,219,993  Chengdu surrogate prestige

Tomcat5.0 later version is support for output content compressed using gzip compression format. Beijing surrogate prestige 15023219993  Guangzhou surrogate prestige 15,023,219,993  15,023,219,993  Beijing surrogate prestige 15023219993  Chongqing surrogate prestige 15023219993  Chongqing surrogate prestige 15023219993

 

Modify% TOMCAT_HOME% / conf / server.xml, node amended as follows:

<Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" executor="tomcatThreadPool" URIEncoding="utf-8"compression="on"compressionMinSize="50" noCompressionUserAgents="gozilla, traviata"compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />

  As can be seen from the properties of the above node, to use the gzip compression, you need to add the node attribute Connector

  • compression = "on" Open compression
  • compressionMinSize = "50" to enable the compressed output content size, default 2KB
  • noCompressionUserAgents = "gozilla, traviata" for the following browsers, do not enable compression
  • compressableMimeType = "text / html, text / xml, text / javascript, text / css, text / plain" which resource types required compression

Test Methods

TOMCAT after this compression enabled us to test how effective compression is it?

Tomcat is the first browser request accept-encoding header according to judge whether the browser supports compression, if the value contains gzip, to show that the browser supports gzip compression browse content, we can use two methods to verify whether the compression effect.

We directly through the browser to access server-enabled compression configuration, and then view the captured data packets through packet capture tool, there is a lot of content if you can not read, it indicates that the compression is enabled.

 

By simulation program requests

We httpclient write a simple test program code is as follows:

@Test  public void testGzip() {          HttpClient httpClient = new HttpClient();          GetMethod getMethod = new GetMethod("http://localhost/admin.jsp");          try {                  getMethod.addRequestHeader("accept-encoding", "gzip,deflate");                  getMethod.addRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");                  int result = httpClient.executeMethod(getMethod);                  if (result == 200) {                          System.out.println(getMethod.getResponseContentLength());                          String html = getMethod.getResponseBodyAsString();                          System.out.println(html);                          System.out.println(html.getBytes().length);                  }          } catch (HttpException e) {                  e.printStackTrace();          } catch (IOException e) {                  e.printStackTrace();          } finally {                  getMethod.releaseConnection();          }  }  

 Junit implementation of this program and see what it is output, if the output is gibberish, and print content length is much less than the actual length, it shows our configuration to take effect, and by some other verification tools, you will find the site browsing speed will be improved significantly.

NOTE: If you find that content has not been compressed, you can consider adjusting compressionMinSize size if the requested resource is less than this value, it will not enable compression.

Guess you like

Origin www.cnblogs.com/ipengrui2/p/12306735.html