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

How to set the BIO, NIO and APR modes of Tomcat on Linux servers

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to set the BIO, NIO and APR modes of Tomcat on the Linux server. I hope you will get something after reading this article. Let's discuss it together.

1. BIO, NIO, AIO

Learn about four concepts first:

Synchronization: personally take the bank card to the bank to withdraw money (when using synchronous IO, Java handles IO read and write).

Async: entrusts a younger brother to take a bank card to the bank to withdraw money, and then give it to you (when using asynchronous IO, Java entrusts IO read and write to OS, and the data buffer address and size need to be passed to OS (bank card and password). OS needs to support asynchronous IO operation API).

Blocking: ATM queued to withdraw money, you can only wait (when using blocking IO, the Java call will block until the read and write is complete).

Non-blocking: withdraw money from the counter, take a number, and then sit on the chair to do something else. The equal sign broadcast will inform you to deal with it. You can't go until the number arrives. You can keep asking the lobby manager if you haven't arrived yet. If you haven't arrived yet, you can't go. (when using non-blocking IO, if you can't read and write, the Java call will return immediately, and continue reading and writing when the IO event distributor notifies you to read and write. Continue to cycle until the read and write is complete.

Java supports BIO, NIO and AIO:

Java BIO: synchronize and block, the server implementation mode is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread for processing. If the connection does nothing, it will cause unnecessary thread overhead, which can of course be improved through the thread pool mechanism.

Java NIO: synchronous non-blocking, the server implements one thread per request, that is, all connection requests sent by the client are registered with the multiplexer, and the multiplexer starts a thread for processing only when the multiplexer polls that the connection has an Icano request.

Java AIO (NIO.2): asynchronous and non-blocking. The server implements a valid request for a thread. OS completes all the client's Icano requests and then informs the server application to start the thread for processing.

Analysis of applicable scenarios for BIO, NIO and AIO:

The BIO method is suitable for the architecture with a small number of connections and a fixed number of connections, which requires high server resources, and concurrency is limited to the application. JDK1.4 is the only choice before, but the program is intuitive, simple and easy to understand.

NIO is suitable for architectures with a large number of connections and short connections (light operation), such as chat servers, where concurrency is limited to applications, programming is more complex, and JDK1.4 begins to support it.

AIO is used in architectures with a large number of connections and long connections (heavy operations), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, and JDK7 begins to support it.

2. Brief introduction of three modes of tomcat

BIO

BIO (blocking blocking O), as the name implies, means that Tomcat uses the traditional Java Imax O operation (that is, the java.io packet and its subpackets).

By default, Tomcat runs in bio mode. Unfortunately, in general, bio mode is the lowest performing of the three operating modes. We can view the current status of the server through Tomcat Manager.

NIO

It is a new mode of operation (i.e. java.nio package and its subpackages) provided by Java SE 1.4 and later versions. Java nio is a buffer-based Java API that provides non-blocking Imax O operations, so nio is also regarded as an acronym for non-blocking Imax O. It has better concurrency performance than the traditional Iram O operation (bio).

APR

(Apache Portable Runtime/Apache Portable Runtime), which is a support library for Apache HTTP servers. You can simply understand that Tomcat will call the core dynamic link library of the Apache HTTP server in the form of JNI to handle file reading or network transfer operations, thus greatly improving the performance of Tomcat in dealing with static files. Tomcat apr is also the preferred mode for running highly concurrent applications on Tomcat.

Third, the performance comparison of three modes of tomcat

Here I quote the test results given by netizens.

IV. Tomcat mode setting

What I'm demonstrating here is tomcat7, which defaults to BIO mode. Tomcat8 is the default NIO mode.

Before that, let's configure the tomcat management interface so that we can more easily observe our bio, nio and apr modes later (if you don't want to set this, you can also check the tomcat log file later)

Pull to the bottom, copy the contents in the red box, paste outside the comments, and add manager/status users (please don't change the user name here, it doesn't seem to work if you change it)

After configuring the restart tomcat, you can see the tomcat status through ip:port/manager/status, which contains server information and tomcat information.

Here are the configuration steps for three modes: BIO mode

Tomcat7 defaults to. If you want to set it to BIO mode for tomcat8 or 9, modify it here

Find the port=8080 port setting item and restart tomcat in BIO mode as long as it is in the red box. This is what tomcat7 looks like by default

NIO mode

In the same location as setting BIO mode, change the contents of the red box to this, and restart tomcat will use NIO mode. This is what tomcat8 looks like by default

APR mode

Enabling this mode is a little more troublesome, and some dependent libraries need to be installed. Here are the conditions required for installation:

1, the latest apr

2, the latest apr-util

3Gz (there is a corresponding installation tar package under tomcat/bin/)

However, you also need to change the configuration file, first change the configuration file, or in the same location, change the contents of the red box like this (note that it is very similar to NIO mode), save and exit, and then install the dependent library.

Before installing those three dependencies, check to see if apr is installed on the server

I have installed it here (it seems that tomcat installed it himself, I don't remember), so uninstall it first, ignore the dependent uninstall, and reinstall the latest one.

1.apr (http://apr.apache.org/download.cgi), upload and decompress

Install to / usr/local/apr

. / configure-- prefix=/usr/local/apr & & make & & make install

2.apr-util (http://apr.apache.org/download.cgi), upload and decompress

Install to / usr/local/apr-util and specify the directory of apr

. / configure-- with-apr=/usr/local/apr/-- prefix=/usr/local/apr-util & & make & & make install

3.tomcat-native (there is a corresponding installation of tar package under tomcat/bin/), decompress

Install, specify the directory of apr and the directory of JAVA_HOME

. / tomcat-native-1.2.14-src/native/configure-- with-apr=/usr/local/apr-- with-java-home=/usr/share/jdk1.8 & & make & & make install

After installation, remember to add an additional APR environment variable after the JAVA_HOME environment variable in the / etc/profile file (pay attention to the apr directory you installed)

Export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib

Use the source / etc/profile command to make the environment variable configuration take effect immediately

Start tomcat, visit (the ip of the server where your tomcat is located) 192.168.25.128:8080/manager/status, and then ok

If you do not want to set up the tomcat management interface above, you can also view it through the log.

After reading this article, I believe you have a certain understanding of "how to set the BIO, NIO and APR modes of Tomcat on the Linux server". If you want to know more about it, welcome to follow the industry information channel, 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