1. Overview
1.概述
Apache Tomcat is an open-source Java servlet web container server that is used to deploy Java-based applications. Tomcat server primarily executes Java servlets and JSP for dynamic web applications. In configuring the Tomcat server, we can use CATALINA_OPTS and JAVA_OPTS env variables for JVM settings.
Apache Tomcat 是一个开源 Java servlet Web 容器服务器,用于部署 基于 Java 的应用程序。Tomcat 服务器主要执行动态 Web 应用程序的 Java servlet 和 JSP。在配置 Tomcat 服务器时,我们可以使用 CATALINA_OPTS 和 JAVA_OPTS env 变量进行 JVM 设置。
In this tutorial, we’ll look at the use of CATALINA_OPTS and JAVA_OPTS in the Tomcat server.
在本教程中,我们将介绍如何在 Tomcat 服务器中使用 CATALINA_OPTS 和 JAVA_OPTS 。
2. Importance of CATALINA_OPTS and JAVA_OPTS
2.CATALINA_OPTS 和 JAVA_OPTS 的重要性
Tomcat servers use the CATALINA_OPTS and JAVA_OPTS env variables for customized configuration. Both these env variables allow us to customize the JVM options for the Tomcat server, but they are used for slightly different purposes. Customization of JVM options is vital to achieving the high performance of a web application.
Tomcat 服务器使用 CATALINA_OPTS 和 JAVA_OPTS 环境变量进行自定义配置。这两个 env 变量都允许我们自定义 Tomcat 服务器的 JVM 选项,但它们的用途略有不同。自定义 JVM 选项对于实现 网络应用程序的高性能。
We can use these env variables for scalability and security in the server. Additionally, we can use these env variables for global JVM configurations, performance tuning, and configuration standardization.
我们可以使用这些 env 变量来提高可扩展性和安全性 在服务器中。此外,我们还可以将这些 env 变量用于全局 JVM 配置、性能调整和配置标准化。
The key difference between both the env variables is that any change in JAVA_OPTS applies to all the running Tomcat instances, whereas CATALINA_OPTS works only on a single Tomcat instance. Hence, we can use JAVA_OPTS to set global JVM configurations for all the Tomcat instances running on the same JVM.
这两个 env 变量的主要区别在于,JAVA_OPTS 中的任何更改都适用于所有正在运行的 Tomcat 实例,而 CATALINA_OPTS 仅适用于单个 Tomcat 实例。因此,我们可以使用 JAVA_OPTS 为在同一 JVM 上运行的所有 Tomcat 实例设置全局 JVM 配置。
3. Use of JAVA_OPTS
3.使用 JAVA_OPTS
JAVA_OPTS is a key env variable to configure customized JVM settings. We can manage memory, GC configurations, and system properties using JAVA_OPTS. In order to understand the working of JAVA_OPTS. Let’s first run a Tomcat server using the docker run command:
JAVA_OPTS 是配置自定义 JVM 设置的关键环境变量。我们可以使用 JAVA_OPTS 管理内存、GC 配置和系统属性。为了了解 JAVA_OPTS 的工作原理。让我们先使用 docker run 命令运行 Tomcat 服务器:
$ docker run -d -p 8080:8080 --name baeldung tomcat:latest
In the above command, we run a Tomcat server with an HTTP port exposed on 8080 using a docker container baeldung. Let’s also check out the default JVM settings in this Tomcat process:
在上面的命令中,我们使用 docker 容器 baeldung 运行了一个 Tomcat 服务器,其 HTTP 端口为 8080。我们也来看看 Tomcat 进程中的默认 JVM 设置:
$ ps -ef | grep tomcat
root 1 0 7 14:42 ? 00:00:01 /opt/java/openjdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027 --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
-classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat
-Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
In the above command, we can see that the Tomcat server started with some default configurations. Now, if we need to make some changes to this Tomcat process, we would need to add that customized configuration in JAVA_OPTS. Let’s make changes to the min/max heap of a Tomcat server process:
在上述命令中,我们可以看到 Tomcat 服务器以一些默认配置启动。现在,如果我们需要对 Tomcat 进程进行一些更改,就需要在 JAVA_OPTS 中添加自定义配置。让我们对 Tomcat 服务器进程的最小/最大堆进行更改:
$ export JAVA_OPTS="-Xmx512m -Xms256m"
The above command will simply update the max 512m and min 256m memory on the server. In the Docker container, we can provide the env variables in the run command itself:
上述命令将简单地更新服务器上的 max 512m 和 min 256m 内存。在 Docker 容器中,我们可以在运行命令中提供环境变量:
$ docker run -d -p 8080:8080 -e JAVA_OPTS="-Xmx512m -Xms256m" --name baeldung tomcat:latest
To verify, let’s check the process details of the Tomcat server again:
为了验证,让我们再次检查 Tomcat 服务器的进程详情:
$ ps -ef | grep tomcat
root 1 0 8 14:49 ? 00:00:01 /opt/java/openjdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xmx512m -Xms256m -Djdk.tls.ephemeralDHKeySize=2048
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED
--add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
-classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat
-Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
In the above output, we can see that both the max and min heap are set to 512m and 256m, respectively. Similarly, we can use JAVA_OPTS for garbage collection configurations as well.
在上述输出中,我们可以看到 max 和 min 堆分别设置为 512m 和 256m, 。同样,我们也可以使用 JAVA_OPTS 进行垃圾回收配置。
In short, we can use JAVA_OPTS to customize the JVM configurations as per server requirements. Additionally, It permits us to tune memory management, server profiling, GC tasks, and resource utilization of a Tomcat process.
简而言之,我们可以使用 JAVA_OPTS 根据服务器要求自定义 JVM 配置。此外,它还允许我们调整内存管理、服务器剖析、GC任务以及 Tomcat 进程的资源利用率。
4. Use of CATALINA_OPTS
4.CATALINA_OPTS 的使用
CATALINA_OPTS is an env variable that allows us to customize Apache server-related configurations. Unlike JAVA_OPTS, which is primarily used to configure JVM-related options, CATALINA_OPTS mainly configure Tomcat server-related configurations. To demonstrate, let’s run the command to change the HTTP port of the Tomcat server:
CATALINA_OPTS 是一个 env 变量,允许我们自定义 Apache 服务器相关的配置。与主要用于配置 JVM 相关选项的 JAVA_OPTS 不同,CATALINA_OPTS 主要配置与 Tomcat 服务器相关的配置。为了演示,让我们运行命令来更改 Tomcat 服务器的 HTTP 端口:
$ docker run -d -p 8080:8082 -e CATALINA_OPTS="-Dcatalina.http.port=8082" --name baeldung tomcat:latest
In the above command, we updated the default HTTP port of the Tomcat server from 8080 to 8082. To verify, let’s check out the command to see the updated HTTP port of the Tomcat server:
在上述命令中,我们将 Tomcat 服务器的默认 HTTP 端口从 8080 更新为 8082。为了验证,让我们查看一下命令,看看 Tomcat 服务器更新后的 HTTP 端口:
$ ps -ef | grep tomcat
root 1 0 1 15:12 ? 00:00:02 /opt/java/openjdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027 --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
-Dcatalina.http.port=8082 -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat
-Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
In the output of the above command, we can see that the HTTP port is updated to 8082. One of the main uses of CATALINA_OPTS is to add custom system properties. To demonstrate, let’s look at the command:
在上述命令的输出中,我们可以看到 HTTP 端口已更新为 8082。CATALINA_OPTS 的主要用途之一是添加自定义系统属性。为了演示,让我们看看该命令:
$ export CATALINA_OPTS="$CATALINA_OPTS -Dcustom.property=baeldung-example-value"
In the above command, we added a custom property to the Tomcat server. Upon setting this environment and restarting the Tomcat server, the Java application and Tomcat server will access this custom property value. We can also provide a whole configuration property using this command. To illustrate, let’s look at the command:
在上述命令中,我们向 Tomcat 服务器添加了一个自定义属性。设置该环境并重新启动 Tomcat 服务器后,Java 应用程序和 Tomcat 服务器将访问该自定义属性值。我们还可以使用此命令提供整个配置属性。为了说明这一点,让我们来看看这条命令:
$ export CATALINA_OPTS="$CATALINA_OPTS -Dbaeldungapp.config=/usr/local/tomcat/config/config.properties"
In the above command, we provided the config file for the baeldung application using the baeldungapp.config property file. Just restarting the server makes this configuration work.
在上述命令中,我们使用 baeldungapp.config 属性文件为 baeldung 应用程序提供了配置文件。只要重启服务器,配置就会生效。
5. Conclusion
5.结论
In this article, we explored the applications of CATALINA_OPTS and JAVA_OPTS in a Tomcat server. First, we changed the JVM configuration of the Tomcat server using JAVA_OPTS. After that, we updated the HTTP port of the Tomcat server using CATALINA_OPTS.
在本文中,我们探讨了 CATALINA_OPTS 和 JAVA_OPTS 在 Tomcat 服务器中的应用。首先,我们使用 JAVA_OPTS 更改了 Tomcat 服务器的 JVM 配置。然后,我们使用 CATALINA_OPTS 更新了 Tomcat 服务器的 HTTP 端口。