Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Discussion on installation and Optimization of Tomcat Server

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Blog outline:

I. Overview of Tomcat

II. Tomcat components and their characteristics

Third, install Tomcat

4. Server.xml file structure in Tomcat

5. Multiple instances (one host running multiple Tomcat)

VI. Custom catalogue

Use Tomcat to monitor the use of JVM virtual machine resources in real time

Enable JMX remote debugging function

9. Configure to access the tomcat server using https

10. Open the Manager management page

11. Open the host-mangent management page

12. Split the Tomcat operation log

XIII. Define the page returned by Tomcat 404 error

14. Memory optimization of Tomcat service

15. Optimization of Tomcat thread pool

Change the way Tomcat services are requested

I. Overview of Tomcat

Tomcat is a core project of the apache Software Foundation project, is a free and open source web application server, is a lightweight application server, in small and medium-sized enterprises and access is not very high scenarios are very popular, widely used, and in the development and debugging of JSP programs of choice!

It can be considered that when a server deploys an Apache server, it can use the Apache server to respond to the client's static page access requests, and Tomcat is an extension of the Apache server. When running Tomcat, it is actually run as an independent process of Apache. The Apache server provides services for static pages, while Tomcat parses JSP pages and servlet, and Tomcat also has the ability to parse static pages. But there are some differences compared with Apache.

About the content of Tomcat can not be finished in one or two words, it is recommended to visit the official website of Tomcat to learn more!

2. What is the Tomcat component and its characteristics 1.Servlet?

Servlet (Server Applet): is the abbreviation of Java Servlet, called mini-service program or service connector. The server-side program written in Java is independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content. In the narrow sense, Servlet refers to an interface implemented by the Java language; in a broad sense, Servlet refers to any class that implements the Servlet interface; in general, Servlet is understood as the latter.

Servlet runs in an application server that supports Java. In principle, Servlet can respond to any type of request, but in most cases Servlet is only used to extend Web servers based on the HTTP protocol.

The process of dealing with requests and responses in Tomcat is completed by servlet programs, and servlet is to solve the content derived from dynamic pages, just like the process that Nginx and Apache need to communicate with PHP servers to achieve dynamic pages, servlet can be regarded as PHP in LAMP or LNMP. As shown in the figure:

Tomcat is a web application server, is a servlet/jsp container, Tomcat as a servlet container, is responsible for handling client requests and transmitting the response of servlet to customers, while servlet is a component running on a server in Java language. The most common use of servlet is to extend Java web server functions and provide a very secure and easy-to-use cgi alternative.

Servlet workflow:

The servlet program is called by the Web server, and when the Web server receives the Servlet access request from the client, it performs the following procedures:

The ① Web server first checks to see if the instance object of the Servlet has been loaded and created. If so, perform step ④ directly; otherwise, step ② will be performed

② loads and creates an instance object of the servlet

③ calls Servlet instance

④ creates a HttpServletRequest object that encapsulates the HTTP request and a HttpServletResponse object that represents the HTTP response message, then calls the service () method of Servlet and passes the request and response objects as parameters

Before the ⑤ WEB application is stopped or restarted, the Servlet engine uninstalls Servlet and calls the destroy () method of Servlet before uninstalling

The flow of data processing in 2.Tomcat

As shown in the figure:

The ① client sends request data through the browser

② Tomcat receives the request data from the client, which is processed and parsed by the servlet container, and changes the client's data request request to servlet request to the backend servlet instance.

The ③ servlet instance receives the request object of the client's request data, processes the client's request, and responds the data to the servlet container after processing, which is called servlet response to the servlet container.

The ④ servlet container delivers the received response data to the client

The Servlet container communicates with the Servlet instance using the Java servlet api interface, which is defined or written by the developer when writing the code.

3.Servlet and JSP

As an extension of Servlet technology, jsp is often confused. Here's a brief introduction:

The difference between Servlet and JSP:

Servlet can dynamically output HTML content through HttpServletResponse objects in Java code.

JSP embeds Java code in static HTML content, and then Java code generates a HTML page after it is executed dynamically

The respective characteristics of Servlet and JSP:

Although Servlet can organize the business logic code well, in the Java source file, because the HTML content is generated by string concatenation, it can easily lead to difficult code maintenance and poor readability.

Although JSP avoids the disadvantage of Servlet in generating HTML content, it mixes a large number of complex business logic into HTML.

Characteristics of 4.Tomcat

The runtime takes up small resources, has strong expansibility, and supports the functions commonly used in the development of application systems, such as load balancing and mail servers.

Third, install Tomcat

Download the software required for Tomcat

Installation of Tomcat requires the system Java environment version 1.8 or above, Centos 7 has been supported by default Java environment version 1.8, as most of the current use of Centos 6 system, because I use the Centos 7 system, so first need to uninstall the JDK environment.

[root@localhost ~] # rpm-qa | grep jdk / / query native default JDK environment java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64copy-jdk-configs-3.3-2.el7.noarchjava-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64java-1.7.0-openjdk-1.7.0 .171-2.6.13.2.el7.x86_64java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64 [root@localhost ~] # rpm-e java-1.8.0-openjdk-headless-- nodeps [root@localhost ~] # rpm-e java-1.7.0-openjdk-headless-- nodeps / / Uninstall the original jdk-headless package [root@localhost ~] # tar zxf jdk-8u211-linux-x64.tar .gz-C / usr/local / / decompress the provided JDK software package [root@localhost ~] # vim / etc/profile / / write system environment variables Add the following at the end: export JAVA_HOME=/usr/local/jdk1.8.0_211 / / specify the Java installation path export JRE_HOME=/usr/local/jdk1.8.0_211/jre / / Java runtime environment export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar / / declare the classes in Java Class is the property of a kind of transaction export PATH=$JAVA_HOME/bin/:$JRE_HOME/bin/:$PATH / / add the defined variable to the system environment variable [root@localhost ~] # source / etc/profile / / reload the system environment variable file [root@localhost ~] # java-version / / query support Java version information java version "1.8.0U211" / / meets the requirements Java (TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot (TM) 64-Bit Server VM (build 25.211-b12) Mixed mode) [root@localhost ~] # tar zxf apache-tomcat-8.5.35.tar.gz [root@localhost ~] # mv apache-tomcat-8.5.35 / usr/local/tomcat / / extract the tomcat package And move to the corresponding location [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / start the Tomcat service [root@localhost ~] # netstat-anpt | grep 8080 / / you can see that the access port of Tomcat is 8080tcp6 0: 8080: * LISTEN 2186/java can also use another method Write a good script to control tomcat in the link to the network disk. [root@localhost ~] # mv tomcat / etc/init.d/ [root@localhost ~] # chmod + x / etc/init.d/tomcat [root@localhost ~] # / etc/init.d/tomcat start / / this is also possible

Client access test:

An introduction to the directory in the Tomcat service installation directory:

Bin: store commands and script files related to the Tomcat service; conf: store the configuration file of the Tomcat service; lib: store the library files needed by the Tomcat service to run; logs: store the log files of the Tomcat service; temp: store the temporary files generated by the Tomcat service; work:Tomcat 's working directory, store the compiled configuration files of the Tomcat runtime; webapps: store the network files of the Tomcat service

Commonly used configuration files:

The main configuration file of the Server.xml:Tomcat service; web.xml: to implement functions that are not defined in the main configuration file; tomcat-users.xml: to configure and manage the users and permissions of Tomcat; IV. Server.xml file structure in Tomcat

As shown in the figure:

Server: represents the entire container, the top-level element of the instance. There can be only one server element in a configuration file, which can contain multiple service. For example:

/ / means to turn off Tomcat, connect to 8005 using telnet, and enter the shutdown instruction

Service: an independent component that provides a complete jvm. Jvm represents a Java virtual machine, which is a fictional virtual machine that can contain an engine containing multiple connector.

/ / accept all requests accepted by tomcat. If Service name= "apache", it means to accept all requests forwarded by apache.

Connector: interface definition, which is responsible for accepting requests from the client and returning the response structure to the client

/ / port: the port that accepts data; protocol: set the http protocol; / / Connection Timeout: the connection timeout with the client is timed out.-1 means there is no limit on the connection time of the client; / / Redirectport: forward the request to 8443 when the client request is https

Engine: handles client requests and can contain multiple hots

/ / name: is the name of the engine. Default is to look for localhost when processing customer requests.

Host: a defined virtual host that handles all client requests for a specific virtual host

Context: a single web application running in a virtual host

Tomcat processes the http request, as shown in the figure:

Specific process:

① users click on the content of the web page, and the request is sent to local port 8080, which is obtained by the Coyote HTTP/1.1 Connector listening there.

② Connector hands the request to the Engine of its Service and waits for a response from Engine

③ Engine gets the request localhost/test/index.jsp, which matches all virtual host Host

The ④ Engine matches to a Host named localhost (even if it doesn't match, the request is handed over to the Host for processing, because the Host is defined as the default host for the Engine), and the Host named localhost gets the request / test/index.jsp, matching all the Context it owns. Host matches to Context with path / test (if not, the request is handed over to Context with path name "" to be processed)

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

⑥ constructs HttpServletRequest objects and HttpServletResponse objects, calling JspServlet's doGet () or doPost () as parameters. Execute business logic, data storage, etc.

⑦ Context returns the HttpServletResponse object after execution to Host

⑧ Host returns the HttpServletResponse object to Engine

⑨ Engine returns the HttpServletResponse object to Connector

⑩ Connector returns the HttpServletResponse object to the customer Browser

5. Multiple instances (one host running multiple Tomcat)

To put it bluntly, more virtual hosting has almost the same meaning. The methods are as follows:

[root@localhost ~] # cp-ra / usr/local/tomcat/ / usr/local/tomcat2 [root@localhost ~] # vim / usr/local/tomcat2/conf/server.xml 22 / / change the original 8005 to 8006 69 / / change the original 8443 to 8444116 / / change the original 8009 to 8010, 8443 to 8444amp / line number, and modify the configuration file Avoid port conflicts [root@localhost ~] # / usr/local/tomcat2/bin/startup.sh / / start the second tomcat instance [root@localhost ~] # netstat-anpt | grep 8081 / / through the modification of the configuration file, the listening port is 8081tcp6 0 0: 8081:: * LISTEN 57572/java

Client access test:

VI. Custom catalogue

To put it bluntly, it is to access the file of the specified path. The methods are as follows:

[root@localhost ~] # vim / usr/local/tomcat/conf/server.xml / / Edit the tomcat service master configuration file 148150151 / / add 150lines and 151lines! / / path: indicates the access path; docBase: specifies the path where the file is stored; / / debug: indicates that the debug level is the least output information, 9 provides the most information; / / reloadable: it will automatically reload when the web.xml file changes when it is true, and the service is not allowed to restart / / crossContext: when true, different context share a session (session pool) [root@localhost ~] # mkdir / data/project-p [root@localhost ~] # echo "hello word" > > / data/project/index.jsp / / create a test page [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the tomcat service

Client access test:

Tomcat forbids directory traversal by default. The test is as follows:

[root@localhost ~] # mv / data/project/index.jsp / data/project/index1.jsp / / change the default home page file name

Client access, as shown in figure:

If you need to turn on directory traversal, you need to do the following:

[root@localhost ~] # vim / usr/local/tomcat/conf/web.xml / / Note: this file is not the tomcat main configuration file 112true / / change the original false to true [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the tomcat service

Client access test, as shown in the figure:

7. Use Tomcat to monitor the usage of JVM virtual machine resources in real time [root@localhost ~] # vim / usr/local/tomcat/conf/server.xml / / Edit tomcat service master configuration file 152 / / has been explained above This does not explain [root@localhost ~] # mkdir / data/jiankong [root@localhost ~] # vim / data/jiankong/index.jsp / / write a monitoring script [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the tomcat service under the specified directory

Access test, as follows:

8. Enable JMX remote debugging function [root@localhost ~] # vim / usr/local/tomcat/bin/catalina.sh / / write this script 307CATALINANGOPTS = "$CATALINA_OPTS-Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.port=8888-Dcom.sun.management.jmxremote.ssl=false-Dcom.sun.management.jmxremote.authenticate=true" 308309 #-Execute The Requested Command- -/ / the content entered must be on this line / / port: listening port Ssl: turn off ssl secure transfer authenticate: enable user authentication [root@localhost ~] # cd / / usr/local/jdk1.8.0_211/jre/lib/management/ [root @ localhost management] # cp jmxremote.password.template jmxremote.password [root@localhost management] # vim jmxremote.password / / define a user's file and add the following 64 lzj 123456 / / 64 to the end of the file as the line number Define the user as lzj, password 123456, and use "wq!" Force save exit [root@localhost management] # vim jmxremote.access / / File defining user permissions 77 lzj readwrite\ / / define the original user as the user lzj 78 create javax.management.monitor.* just defined Javax.management.timer.*\ 79 unregister [root@localhost management] # chmod 600 jmxremote.access [root@localhost management] # chmod 600 jmxremote.password / / change the permissions of these two profiles to 600 [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the tomcat service [root@localhost ~] # netstat -anpt | grep 8888tcp6 0 0: 8888: * LISTEN 58760/java / / confirm that port 8888 just defined is listening

Because it is a graphical interface, you need to execute the following commands in the desktop environment:

[root@localhost ~] # jconsole / / call up the graphical terminal

9. Configure to use https to access the tomcat server [root@localhost ~] # keytool-genkey-v-alias tomcat-keyalg RSA-keystore / usr/local/tomcat/conf/tomcat.keystore-validity 36500//alias: alias, set here to tomcat;//keyalg: certificate algorithm, RSA / / validity: certificate validity period (in days) / / keystore: specify the certificate storage path. You can enter the KeyStore password customized: / / Experimental environment. Enter the new password again as you wish: what is your first name and last name? [Unknown]: lv what is the name of your organizational unit? [Unknown]: lv what is the name of your organization? [Unknown]: lv\ what is the name of your city or region? [Unknown]: lv what is the name of your province / municipality / autonomous region? [Unknown]: lv what is the two-letter country code for this unit? [Unknown]: is lvCN=lv, OU=lv, O = "lv\", L=lv, ST=lv, C=lv correct? [no]: y is generating a 2048-bit RSA key pair and self-signed certificate (SHA256withRSA) (valid for 36500 days) for the following objects: CN=lv, OU=lv, O = "lv\\", L=lv, ST=lv, C=lv enter the key password (if it is the same as the KeyStore password, press enter): enter the new password again: [storing / usr/local/tomcat/conf/tomcat.keystore] Warning:JKS KeyStore uses a special format. It is recommended that you use "keytool-importkeystore-srckeystore / usr/local/tomcat/conf/tomcat.keystore-destkeystore / usr/local/tomcat/conf/tomcat.keystore-deststoretype pkcs12" to migrate to the industry standard format PKCS12. [root@localhost ~] # vim / usr/local/tomcat/conf/server.xml / / content can be written by yourself, or the original configuration can be modified Note to uncomment 87 / / specify the password 91 [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the tomcat service entered to generate the certificate

Client access test:

10. Open the Manager management page

When accessing the default home page interface of Tomcat, there is a "Manager App", as shown in the figure:

Next, open the manager management page of Tomcat by configuring as follows:

[root@localhost ~] # vim / usr/local/tomcat/conf/tomcat-users.xml 45 46 / / Custom user name and password 47 / / Note to write in this closure [root@localhost ~] # vim / usr/local/tomcat/webapps/manager/META-INF/context.xml 20 allow= "192.168.1.253 | 192.168.1.8 | 127\.\ d +\.\ d +\. \ d + |:: 1 | 0 IP address to be accessed after "allow=" Restart the Tomcat service by separating [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / with "|"

Client access test:

Prepare the war package to be added to the custom directory!

[root@localhost ~] # echo "hello word" > > index.jsp [root@localhost ~] # jar-vcf index.war index.jsp / / make the war package and name it index.war

Add the war package on the web graphics page:

11. Open the host-manager management page

The method is similar to opening the manger management page, as follows:

Modify the configuration file for the server:

[root@localhost ~] # vim / usr/local/tomcat/conf/tomcat-users.xml 45 46 47 / / add the above to the closing tag on the last line User name and password definition [root@localhost ~] # vim / usr/local/tomcat/webapps/host-manager/META-INF/context.xml 19 / / add allowed access IP address [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh / / restart the service

Client access test:

12. Split the Tomcat operation log

Log segmentation of Tomcat is similar to that of Apache, but Tomcat itself does not have a log segmentation tool, so you need to use a third-party log segmentation tool-- cronolog log segmentation tool

Do the following:

[root@localhost ~] # tar zxf cronolog-1.6.2.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/cronolog-1.6.2/ [root@localhost cronolog-1.6.2] #. / configure & & make & & make install / / compile and install the cronolog tool [root@localhost cronolog-1.6.2] # which cronolog / / query the absolute path to the command Path / usr/local/sbin/cronolog [root@localhost ~] # vim / usr/local/tomcat/bin/catalina.sh 471 org.apache.catalina.startup.Bootstrap "$@" start 2 > & 1 | / usr/local/sbin/cronolog / usr/local/tomcat/logs/test.%Y-%m-%d.out > > / dev/null & / / modify the original content to 472 473 # > > "$CATALINA_OUT" 2 > & 1 "&" / / comment on this line [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh [root@localhost ~] # ll / usr/local/tomcat/logs/ Total usage 380khowashr-1 root root 159033 December 8 16:51 catalina.2019-12-08.logMurray r-1 root root 140209 December 8 16:51 catalina.out-rw-r- 1 root root 162 December 8 15:10 host-manager.2019-12-08.Log root root r-1 root root 8722 December 8 16:51 localhost.2019-12-08.Log Mustang r-1 root root 6071 December 8 15:10 localhost_access_log.2019-12-08.txtMuir r-1 root root 1719 December 8 14:41 manager.2019-12-08. LogMurray root root r-1 root root 18824 December 8 16:51 test.2019-12-08.out / / confirm the existence of this file. Define the page returned by the Tomcat 404 error

By default, when the client accesses a directory that does not exist on the Tomcat server, it will prompt a 404 error and display the version information of our Tomcat, which is very dangerous. The default 404 returns the following error page:

The solution is also very simple, which is to redirect the 404 status code so that we can customize the returned page information as follows:

[root@localhost ~] # echo "Access Error..." > > / usr/local/tomcat/webapps/ROOT/error.jsp// Custom error Page content [root@localhost ~] # vim / usr/local/tomcat/conf/web.xml 22 version= "3.1" > / / add the following to the bottom of this line 23 24 404 25 / error.jsp 26 [root@localhost ~] # / usr/local/tomcat / bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh

The client access is as follows:

14. Memory optimization of Tomcat service

The methods are as follows:

[root@localhost ~] # vim / usr/local/tomcat/bin/catalina.sh1 #! / bin/sh / / add the following below the first line: 2 JAVA_OPTS= "- Dfile.encoding=UTF-83-server4-Xms1023M5-Xmx1024M6-XX:NewSize=512M7-XX:MaxNewSize=1020M8-XX:NewRatio=29-XX:ParallelGCThreads=1510-XX:+DisableExplicitGC" [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh// after restarting the Tomcat service Real-time monitoring of JVM virtual machine resources for verification

If you are using a previous version of Tomcat8.0, you need to add two more configuration items, and the version after 8.0 will be removed:

-XX:PermSize=256M-XX:MaxPermSize=356M

Each configuration item added in the configuration file is explained as follows:

-Xms: indicates the initial memory size of JVM, or the size of the Java initialization heap.-Xms is set to the same value as-Xmx to prevent JVM from repeatedly re-applying for memory, resulting in great ups and downs in performance. The default value is 1x64 of physical memory. -Xmx: indicates the maximum Java heap size. When the memory required by the application exceeds the maximum value of the heap, the virtual machine will prompt for memory overflow and cause the application service to crash. Therefore, it is generally recommended that the maximum value of the heap be set to 80% of the maximum value of available memory: set JVM minimum memory (128256 is enough, generally not set);-XX:NewSize: set the new generation memory size. In the new generation, the memory space allocated when the object is newly created, and if the object is not reclaimed by the garbage collector in the old generation, the object will be moved to the old generation;-XX:PermSize: set the memory size of persistent generation to store static files, such as Java classes, methods, and so on. Persistent generation has no significant effect on garbage collection, but some applications may dynamically generate or call some class, also known as method zone, which stores structural information about each java class, such as runtime constant pools, fields and method data, bytecode contents of constructors and common methods, and special methods that need to be used for class, instance, interface initialization, etc. JDK8 has killed the persistent generation (PermGen Space) and replaced the metaspace (Metaspace). Metaspace occupies local memory and no longer takes up virtual machine memory;-XX:MaxPermSize: sets the maximum persistent generation memory size, permanent generation does not belong to heap memory, heap memory only contains new generation and old age;-XX:NewRatio: changes the ratio of new and old space, meaning that the size of the new space is 1x8 of the old space (default is 8);-XX:ParallelGCThreads can be used to increase parallelism and requires more cpu -XX:+DisableExplicitGC: turn off System.gc (), which simply means to turn off the memory recovery mechanism in java. 15. Optimization of Tomcat thread pool

The methods are as follows:

[root@localhost ~] # vim / usr/local/tomcat/conf/server.xml 53 / / add the following to this line 54 70 116

Description of important parameters of Excutor:

Name: the name of the shared thread pool, which is the name to be referenced by connector in order to share the thread pool. This name must be unique namePrefix: on JVM, each running thread can have a name string. This property sets a prefix for the name string of each thread in the thread pool. Tomcat will append the thread number to this prefix. The default value is tomcat-exec-maxThreads: the maximum number of threads that the thread pool can hold. The minimum number of inactive threads that the default 200minSpareThreads:Tomcat should always open, which defaults to 25

Connector parameter description: executor: indicates the thread pool corresponding to the parameter value.

AJP is a protocol customized for communication between Tomcat and HTTP server, which can provide high communication speed and efficiency. This AJP connector will be used if the Apache is placed on the Tomcat front end.

Change the way Tomcat services are requested

There are three request methods supported by Tomcat:

BIO: blocking Tomcat7 O operation. The following versions of Tomcat7 run in bio mode by default, and each request needs to be processed by creating a thread. The thread overhead is relatively high, which is not suitable for high concurrency scenarios, and the performance is the lowest. NIO: based on buffer, it can provide non-blocking iUnip O operation, which has better concurrency performance than traditional BIO. APR (Apache portable run-time libraries): simple understanding is to solve the asynchronous IO problem from the operating system level, greatly improve server processing and corresponding performance, and is also the preferred mode for Tomcat to run with high concurrency.

The main functional modules provided by APR include memory allocation and memory pool, atomic operation, file Imax O, lock, memory mapping, hash table, network Imax O, polling, process and thread operation, etc. APR,Tomcat can achieve high scalability and superior performance, and can be better integrated with local server technology, so that Tomcat can be used as a general Web server. Not just as a lightweight application server. In this case, Java will no longer be a back-end programming language, but can also be used more for mature Web server platforms.

1. Change the request method to NIO (above Tomcat8, default is NIO mode) [root@localhost ~] # vim / usr/local/tomcat/conf/server.xml [root@localhost ~] # / usr/local/tomcat/bin/shutdown.sh [root@localhost ~] # / usr/local/tomcat/bin/startup.sh

To view its "catalina.out" log, the following words indicate that the request received in nio is handled as follows (since the Tomcat log is split above, you need to check the test.2019-12-08.out log file):

two。 The request method should be APR.

Enabling this is usually a little more troublesome, and you need to install some dependencies (the version of CentOS 7.3 that I use).

Download apr package, apr-util package

[root@localhost ~] # yum-y install apr-devel openssl-devel gcc make / / installation depends on [root@localhost ~] # tar zxf apr-1.5.2.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/apr-1.5.2/ [root@localhost apr-1.5.2] #. / configure-- prefix=/usr/local/apr & & make & & make install// compilation Install apr [root @ localhost ~] # tar zxf apr-util-1.5.4.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/apr-util-1.5.4/ [root@localhost apr-util-1.5.4] #. / configure-- prefix=/usr/local/apr-util-- with-apr=/usr/local/apr & & make & & make install// compile and install apr-util [root@localhost apr-util-1 .5.4] # cd / usr/local/tomcat/bin/ [root@localhost bin] # tar zxf tomcat-native.tar.gz [root@localhost bin] # cd tomcat-native-1.2.18-src/native/ [root@localhost native] #. / configure-- with-apr=/usr/local/apr-- with-java-home=/usr/local/jdk1.8.0_211 & & make & & make install// compiles and installs tomcat's own tomcat-native package [root @ localhost ~] # vim / etc/profile / / write environment variables Add the following export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/ lib [root @ localhost ~] # source / etc/profile [root@localhost ~] # cd / usr/local/tomcat/ [root@localhost tomcat] # openssl genrsa-out ca.key 4096 / / generate the required secret key [root@localhost tomcat] # openssl req-new-x509-days 7304-key ca.key-out ca.crt / / pass the required secret Key generation digital certificate [root@localhost ~] # vim / usr/local/tomcat/conf/server.xml / / write tomcat main configuration file 104 106 / / upgrade to http 2.0 protocol 107 108 111 112 [root@localhost logs] # / usr/local/tomcat/bin/shutdown.sh [root@localhost logs] # / usr/local/tomcat/ Bin/startup.sh / / restart the tomcat service [root@localhost ~] # netstat-anpt | grep 443 / / confirm that port 443 is listening tcp6 0 0: 443:: * LISTEN 17206/java

The client accesses using https protocol, as shown in the figure:

[root@localhost ~] # cat/ usr/local/tomcat/logs/test.2019-12-09.out | grep apr// understands the working mode of tomcat by viewing tomat logs.

As shown in the figure:

With regard to the BIO pattern, it is rarely used now, so I won't cover it here!

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report