在将Tomcat安装为Windows服务时设置JAVA_OPTS



我在同一主机上运行多个Tomcat实例,并将它们作为Windows服务安装。当然,这是每个Tomcat实例都有不同的端口。现在,我正在尝试从文件中提取端口号server.xml并尝试将它们作为 JVM 选项传递,以便所有Tomcat实例的server.xml文件看起来都相同。目前,我的server.xml文件中每个实例的连接器端口如下所示:

实例 1

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

实例 2

<Connector port="8180" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

我试图让它看起来像

<Connector port="${port.http}" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

如本答案所述,我可以手动编辑选项以添加参数-Dport.http=8080-Dport.http=8180并且工作正常,但我需要的是当Tomcat作为 Windows 服务安装时设置此 JVM 选项。

以下是我正在运行的.bat文件的内容,用于将实例 1Tomcat安装为 Windows 服务(除了 CATALINA_BASE、端口和服务名称外,实例 2 相同)。如您所见,我也在尝试在将其安装为服务之前设置JAVA_OPTS,但我似乎对此没有任何运气。我也尝试过用双引号,如CALL SET "JAVA_OPTS=-Dport.http=8080"CALL SET JAVA_OPTS="-Dport.http=8080"

CALL SET JAVA_HOME=D:Java
CALL SET CATALINA_BASE=D:instance1
CALL SET JAVA_OPTS=-Dport.http=8080
CALL CD %CATALINA_HOME%bin
service install instance1

谁能帮忙?

如果要将-D选项设置为标题中的状态,则--JvmOptionshttps://tomcat.apache.org/tomcat-7.0-doc/windows-service-howto.html

例如

Install the service named 'Tomcat7'
C:> tomcat7 //IS//Tomcat7 --DisplayName="Apache Tomcat 7" ^
--Install="C:Program FilesTomcatbintomcat7.exe" --Jvm=auto ^
--StartMode=jvm --StopMode=jvm ^
--JvmOptions -DwhateverHere
--StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start ^
--StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop

最终,我可以通过在用于将"Tomcat"安装为 Windows 服务的批处理文件中设置JvmArgs来解决它。我的.bat文件的内容如下所示:

CALL SET JAVA_HOME=D:Java
CALL SET CATALINA_BASE=D:instance1
CALL SET JAVA_OPTS=-Dport.http=8080
CALL SET JvmArgs=-Dport.http=8080;-Dport.shutdown=8005 // This line did the trick
CALL CD %CATALINA_HOME%bin
service install instance1

当我浏览文件中的代码以检查 JVM 选项是如何设置和找到%JvmArgs%附加到末尾时service.bat我发现了这一点,如下所示:

--JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-D%ENDORSED_PROP%=%CATALINA_HOME%endorsed;-Djava.io.tmpdir=%CATALINA_BASE%temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%conflogging.properties;%JvmArgs%" ^

希望这有助于寻找类似配置的人:)

如果你仔细阅读 https://tomcat.apache.org/tomcat-7.0-doc/windows-service-howto.html 中有关jvmoptions的详细信息,你会看到它说它在exe模式下不起作用。 我已经确认了这一点。 即使该值出现在 tomcat6w 中,它也不能在 catalina.properties 中使用。事实上,即使是雄猫8和9也陈述了同样的事情。 如果有人能证明我是错的,我会很高兴。 任务仍在继续。

最新更新