Apache Tomcat 中的 CATALINA_OPTS 與 JAVA_OPTS
1. 概述
Apache Tomcat 是一個開源 Java servlet Web 容器伺服器,用於部署基於 Java 的應用程式。 Tomcat 伺服器主要為動態 Web 應用程式執行 Java servlet 和 JSP。在配置 Tomcat 伺服器時,我們可以使用CATALINA_OPTS
和JAVA_OPTS
環境變數進行 JVM 設定。
在本教程中,我們將了解 Tomcat 伺服器中CATALINA_OPTS
和JAVA_OPTS
的使用。
CATALINA_OPTS
和JAVA_OPTS
的重要性
Tomcat 伺服器使用CATALINA_OPTS
和JAVA_OPTS
環境變數進行自訂配置。這兩個環境變數都允許我們自訂 Tomcat 伺服器的 JVM 選項,但它們的用途略有不同。 JVM 選項的客製化對於實現 Web 應用程式的高效能至關重要。
我們可以使用這些環境變數來實現伺服器的可擴展性和安全性。此外,我們可以使用這些環境變數進行全域 JVM 配置、效能調整和配置標準化。
這兩個環境變數之間的主要差異在於, JAVA_OPTS
中的任何變更都適用於所有正在執行的 Tomcat 實例,而CATALINA_OPTS
僅適用於單一 Tomcat 實例。因此,我們可以使用JAVA_OPTS
為同一 JVM 上執行的所有 Tomcat 實例設定全域 JVM 配置。
JAVA_OPTS
的使用
JAVA_OPTS
是用於配置自訂 JVM 設定的關鍵環境變數。我們可以使用JAVA_OPTS
管理記憶體、GC 配置和系統屬性。為了了解JAVA_OPTS
的工作原理。我們首先使用 docker run 指令來執行 Tomcat 伺服器:
$ docker run -d -p 8080:8080 --name baeldung tomcat:latest
在上面的命令中,我們使用 docker 容器 baeldung 來運行一個 Tomcat 伺服器,該伺服器在 8080 上公開了 HTTP 連接埠。我們也檢查一下 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
在上面的命令中,我們可以看到Tomcat伺服器以一些預設配置啟動。現在,如果我們需要對此 Tomcat 進程進行一些更改,我們需要在JAVA_OPTS
中新增自訂配置。讓我們更改 Tomcat 伺服器進程的最小/最大堆:
$ export JAVA_OPTS="-Xmx512m -Xms256m"
上面的命令將簡單地更新伺服器上的max 512m
和min 256m
記憶體。在 Docker 容器中,我們可以在執行命令本身提供環境變數:
$ docker run -d -p 8080:8080 -e JAVA_OPTS="-Xmx512m -Xms256m" --name baeldung tomcat:latest
為了驗證一下,我們再檢查一下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
在上面的輸出中,我們可以看到max
和min
堆分別設定為512m
和256m,
。同樣,我們也可以使用JAVA_OPTS
進行垃圾收集配置。
簡而言之,我們可以使用JAVA_OPTS
根據伺服器要求自訂 JVM 配置。此外,它還允許我們調整 Tomcat 進程的記憶體管理、伺服器分析、GC 任務和資源利用率。
4.CATALINA_OPTS的使用
CATALINA_OPTS
是一個環境變量,允許我們自訂 Apache 伺服器相關的配置。與JAVA_OPTS,
主要用於配置JVM相關選項不同, CATALINA_OPTS
主要用於配置Tomcat伺服器相關配置。為了演示,讓我們運行命令來更改 Tomcat 伺服器的 HTTP 連接埠:
$ docker run -d -p 8080:8082 -e CATALINA_OPTS="-Dcatalina.http.port=8082" --name baeldung tomcat:latest
在上面的命令中,我們將 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
在上述命令的輸出中,我們可以看到 HTTP 連接埠已更新為8082
。 CATALINA_OPTS
的主要用途之一是新增自訂系統屬性。為了演示,讓我們看一下命令:
$ export CATALINA_OPTS="$CATALINA_OPTS -Dcustom.property=baeldung-example-value"
在上面的命令中,我們為 Tomcat 伺服器新增了一個自訂屬性。設定此環境並重新啟動 Tomcat 伺服器後,Java 應用程式和 Tomcat 伺服器將存取此自訂屬性值。我們也可以使用此命令提供完整的配置屬性。為了說明這一點,讓我們看一下指令:
$ export CATALINA_OPTS="$CATALINA_OPTS -Dbaeldungapp.config=/usr/local/tomcat/config/config.properties"
在上面的命令中,我們使用baeldungapp.config
屬性檔為 baeldung 應用程式提供了設定檔。只需重新啟動伺服器即可使此配置生效。
5. 結論
在本文中,我們探討了CATALINA_OPTS
和JAVA_OPTS
在 Tomcat 伺服器中的應用。首先,我們使用JAVA_OPTS
更改了 Tomcat 伺服器的 JVM 配置。之後,我們使用CATALINA_OPTS
更新了 Tomcat 伺服器的 HTTP 連接埠。