HTTP/1.0, send a request, create a connection, get a Web resource, and disconnect. HTTP/1.1, send a request, create a connection, get multiple Web resources, maintain the connection.

HTTP

request

Common request headers describe
Referer The browser informs the server where the current request is coming from. If you are accessing directly, you will not have this header. Often used for: anti-theft chain
If-Modified-Since The browser notifies the server of the last change in the local cache. The combination with another response header controls the caching of the browser page.
Cookie Session-related technology used to store cookie information cached by the browser.
User-Agent The browser notifies the server, the client browser, and the operating system
Connection Keep the connection state. Close is closed in the keep-alive connection. Procedure
Host The host name of the requested server
Content-Length The length of the request body
Content-Type If it is a POST request, it will have this header, which defaults to Application/X-www-form-urlencoded, indicating that the request body content is URL-encoded
Accept MIME types supported by browsers. A description of a file type.
MIME format Large type/small type [; parameter] e.g. Text/HTML, HTML file; Text/CSS, CSS files; Text/javascript, js files; Image /*, all image files.
Accept-Encoding The browser notifies the server of the data compression format supported by the browser. For example, GZIP compression
Accept-Language The browser notifies the server of the language the browser supports. National Languages (Internationalization I18N)

The response

Common request headers describe
Location The response path must be used together with the status code 302 to complete the jump.
Content-Type The response body type (MIME type) can be text or HTML. charset=UTF-8,Content-Disposition
Set-Cookie Session-related technologies. The server writes cookies to the browser
Content-Encoding Compression format used by the server Value: gzip
Content-length The length of the response body
Refresh Periodic refresh, format: seconds; Url = path. The URL can be omitted; the default value is the current page. Value: 3;url=www.itcast.cn // Refresh the page to www.itcast.cn in three seconds
Server This is the server name. Default: apache-coyote /1.1. This can be modified through the conf/server.xml configuration. <Connector port=”8080″ … server=”itcast”/>
Last-Modified The server notifies the browser when the file was last modified. Used with if-modified-since.

tomcat

The directory structure

  • Bin: script directory startup script: startup.bat Stop script: shutdown.bat
  • Conf: Configuration file directory (config /configuration) Core configuration file: server. XML User permission configuration file: tomcat-users. XML Default configuration file for all Web projects: web.xml
  • Lib: Dependency libraries, jar packages used in Tomcat and Web projects
  • Logs: log file localhost_access_logTXT Tomcat Records user access information, starRepresents time. For example: localhost_access_log. 2016-02-28. TXT
  • Temp: indicates a temporary file directory. Contents in the folder can be deleted at will.
  • Webapps: Directories where WEB projects are published by default.
  • Work: Tomcat handles the working directory and session passivation files for the JSPS

Directory structure of Web applications

WEB. XML file (WEB configuration)Copy the code

Tomcat optimization

There are three operating modes of Tomcat:

  • Bio Bio (Blocking I/O), as the name implies, means that Tomcat uses traditional Java I/O operations (the java.io package and its subpackages). Tomcat runs in BIO mode by default. Unfortunately, in general, BIO mode is the least performing of the three running modes. You can check the current status of the server using Tomcat Manager.

  • Nio is a new way of doing I/O operations (that is, the java.niO package and its subpackages) provided with Java SE 1.4 and later. Java NIO is a buffer-based Java API that provides non-blocking I/O operations, so NIO is also known as an acronym for non-blocking I/O. It has better concurrency performance than traditional I/O operations (BIO).

  • Apr (Apache Portable Runtime/Apache Portable Runtime) is a support library for the Apache HTTP server. You can think of it simply as Tomcat calls the Apache HTTP server’s core dynamic link library as JNI to handle file reads or network transfers, greatly improving Tomcat’s performance for static files. Tomcat APR is also the preferred mode for running highly concurrent applications on Tomcat.

1. Enable the NIO mode

Modify server. The Connector in the XML node, and modify the protocol for org. Apache. Coyote. Http11. Http11NioProtocol

<Connector port="8080" protocal="org.apache.coyote.http11.Http11NioProtocol" 
  connectionTimeout="20000" redirectPort="8443"/>
Copy the code

2. Enable a thread pool

<! -- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->
Copy the code

Open thread pools in server.xml and configure them for use in Connector

<! -- <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP / 1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
Copy the code
  • Parameters that
parameter instructions
MaxThreads (Maximum number of threads) (int) The max number of active threads in this pool, default is 200
MinSpareThreads (Minimum number of active threads) (int) The minimum number of threads always kept alive, default is 25
MaxQueueSize (maximum number of waiting queues, above which the request is rejected) (int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is Integer.MAX_VALUE
PrestartminSpareThreads (Whether minSpareThreads are generated at startup) (boolean) Whether minSpareThreads should be started when starting the Executor or not, the default is false

– Best Practices

<Executer name="tomcatThread Pool" namePrefix="catalina-exec-" 
maxThreads="800" minSpareThreads="100" maxQueueSize="100" prestartminSpareThreads="true"/>
Copy the code

3. Connector

Connector is the gateway for Tomcat to receive requests. Each Connector has its own listening port. There are two types of Connector: HTTP Connector and AJPConnector

  • General properties
attribute instructions
enableLookups Set to true if you want the call to request.getremotehost () to perform DNS queries that return the actual host name of the long distance client. When set to false, DNS lookups are skipped and IP addresses are returned as strings (to improve performance). By default, DNS lookup is disabled.
MaxPostSize will be formed by the container The maximum length (in bytes) of POST processed as a URL parameter. You can disable this restriction by setting the value of this property to be less than or equal to 0. If not specified, the property is set to 2097152 (2 megabytes).
port The TCP port number with which the connector will create a server socket and wait for incoming connections. Your operating system will only allow a server application to listen for a specific port number at a specific IP address. If the special value 0 (zero) is used, Tomcat randomly selects a free port for the connector. This is usually only used in embedded and test applications.
protocol Set up the protocol to handle incoming traffic. Org. Apache. Coyote. Http11. Http11Protocol – Java connector block type; Org. Apache. Coyote. Http11. Http11NioProtocol – don’t block the Java connector; Org. Apache. Coyote. Http11. Http11AprProtocol – APR/native connector. User – defined implementations are also available. Take a look at our connector comparison chart. The Configuration of the Java connector, HTTP and HTTPS is the same. For more information about the APR connector and APR-specific SSL Settings, visit the APR documentation
URIEncoding This specifies the character encoding to use to decode URI characters. If not specified, ISO-8859-1 will be used.
xpoweredBy Setting this property to true causes Tomcat to support notifications using the Servlet specification, where the header field is recommended. The default value is false.
  • Standards implementation
attribute instructions
acceptCount The maximum queue length of incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be rejected. The default value is 100.
acceptorThreadCount The number of threads used to accept connections. On a multi-CPU machine, increase this value, although you probably won’t really need more than 2. In addition, there are many non-keepalive connections, and you may need to increase this value. The default value is 1.
compression To save server bandwidth, the connector can use HTTP/1.1 GZIP compression. Acceptable parameter values are “off” (compression is disabled), “on” (compression is allowed, which causes text data to be compressed), “force” (compression is forced in all cases), or an integer value (this is equivalent to “on” but specifies the minimum amount of data to be compressed before output). If the content length is not known but is set to “on” or more aggressively compressed, the output data will also be compressed. If not specified, the property is set to off.
connectionUploadTimeout Specifies the timeout in milliseconds during data upload. This parameter is valid only when disableUploadTimeout is set to false.
disableUploadTimeout This flag allows the servlet container to use different connection timeouts, usually longer, for data upload. If not specified, this property is set to true to disable upload timeout.
executor A reference to an Executor element. If this property is set and a named executor exists, the connector will use this executor and all other thread-related properties will be ignored. Note that shared executors that are not specified with a connector will use a private, internal executor to provide thread pools.
maxConnections For BIO, the default is the value of maxThreads, unless Executor is used, in which case the default is Executor’s maxThreads value. The default value for NIO is 10000. The default APR /native value is 8192. Note that for Windows systems, the configured APR/native value is reduced to a maximum value less than or equal to a multiple of 1024 for maxConnections. This is done for performance reasons. If the value is set to -1, the maxConnections function is disabled and the connection count is not counted.
maxThreads Maximum number of simultaneous connections Tomcat uses threads to process each request it receives. This value represents the maximum number of threads that Tomcat can create. If not specified, the property is set to 200. This property of the connector is ignored if execute is used, and the connector uses EXECUTE instead of an internal thread pool to process requests.
minSpareThreads The minimum number of threads always kept running. If not specified, the default of 10 is used. Always keep the minimum number of threads running. If not specified, the default is 10.
SSLEnabled Use this property on the connector to enable SSL encrypted transport. Set true to enable SSL handshake/encryption/decryption. The default value is false. When setting this value to true, you need to set the Scheme and Secure properties in order to pass the correct Request.getScheme () and Request.issecure () to the Servlets. See SSL Support for more information.
  • Disable AJP connectors

AJP (Apache JServerProtocol) AJPv13 protocol is package-oriented. The WEB server and Servlet container interact over a TCP connection; To save on costly SOCKET creation, the WEB server tries to maintain a permanent TCP connection to the servlet container and reuse the connection over multiple request and response cycles.

We generally use the Nginx+ Tomcat architecture, so we don’t need the AJP protocol, so we disable the AJP connector.

<! --<Connector port="8009" protocol="AJP / 1.3" redictPort="8443" />-->
Copy the code
  • Best practices
<Connector executor="tomcatThreadPool" 
protocal="org.apache.coyote.http11.Http11NioProtocol" 
          port="8080" 
          connectionTimeout="20000"
          redirectPort="8443" 
          enableLookups="false"
          maxPostSize="10485760"
          URIEncoding="UTF-8"
          acceptCount="100"
          acceptorThreadCount="2"
          disableUploadTimeout="ture"
          maxConnections="10000"
          SSLEnabled="false"
			   />
Copy the code

4. Optimization of JVM parameters

  • Java stack

The Java stack is associated with each thread, and the JVM allocates stack space to each thread when it is created. It is mainly used to store local variables during thread execution, method return values, and method call context. Stack space is freed as the thread terminates.

  • The Java heap

In Java, the heap is an area of memory shared by all threads. The heap is used to hold various Java objects, such as arrays, thread objects, and so on.

Set appropriate JVM parameter optimizations, note the JDK version