Mak*_*iuk 5 java tomcat config war
我有带有REST API的Web项目。我想在tomcat服务器上部署5个副本。例如:
test1.war => URL:http:// localhost:8080 / test1 / api
test2.war => URL:http:// localhost:8080 / test2 / api
test3.war => URL:http:// localhost:8080 / test3 / api
...
问题是每个war文件应使用不同的配置文件。我知道我可以使用export设置环境变量CATALINA_OPTs="Dparam1=/usr/config1.txt"。然后,我需要更改每个war文件中的源代码,以便为test1.war读取param1,为test2.war读取param2。但是每个war文件都应该相同(只有不同的名称)。理论上,完美的解决方案是这样的:
deploy test1.war -conf <path1>
deploy test2.war -conf <path2>
deploy test3.war -conf <path3>
Run Code Online (Sandbox Code Playgroud)
可以在雄猫中做到吗?还有其他选择吗?
我决定在运行时为应用程序获取适当的配置文件。
1)使用以下代码获取当前运行的MainApp.class在war(例如warname.war )文件中的路径:
String path = MainApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = java.net.URLDecoder.decode(path, "UTF-8");
// decodedPath - "D:/apache-tomcat-7.0.81/apache-tomcat-7.0.81/webapps/warname/WEB-INF/classes/com/gieseckedevrient/rsp/servertester/MainApp.class"
Run Code Online (Sandbox Code Playgroud)
2)解析此解码路径以仅获取“ warname ”:
String parentName = "";
java.io.File parentFile = null;
while (!"WEB-INF".equals(parentName)) {
File file = new File(decodedPath);
parentFile = file.getParentFile();
parentName = parentFile.getName();
decodedPath = parentFile.getAbsolutePath();
}
String realWarName = parentFile.getParentFile().getName();
Run Code Online (Sandbox Code Playgroud)
3)在 TOMCAT_HOME}/bin/ 中创建文件“setenv.bat”并在其中插入此代码(对于warname.war为warname.config.file,对于warname2.war为warname2.config.file):
set CATALINA_OPTS=-Dwarname.config.file=D:\app.properties -Dwarname2.config.file=D:\app2.properties
Run Code Online (Sandbox Code Playgroud)
4)用这样的代码读取适当的环境变量:
String configFile = System.getProperty(realWarName + ".config.file");
// configFile - "D:\app.properties" for warname.war
// configFile - "D:\app2.properties" for warname2.war
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1638 次 |
| 最近记录: |