加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用参数部署* .war文件

发布时间:2020-12-15 02:20:36 所属栏目:Java 来源:网络整理
导读:我有REST API的Web项目.我想在tomcat服务器上部署它的5个副本.例如: test1.war =网址: http://localhost:8080/test1/api test2.war =网址: http://localhost:8080/test2/api test3.war =网址: http://localhost:8080/test3/api … 问题是每个war文件应该
我有REST API的Web项目.我想在tomcat服务器上部署它的5个副本.例如:
test1.war =>网址: http://localhost:8080/test1/api
test2.war =>网址: http://localhost:8080/test2/api
test3.war =>网址: http://localhost:8080/test3/api

问题是每个war文件应该使用不同的配置文件.我知道我可以使用export CATALINA_OPTs =“Dparam1 = /usr/config1.txt”设置env变量.然后我需要更改每个war文件中的源代码,以便为test1.war读取param1,为test2.war读取param2.但每个war文件应该是相同的(只有不同??的名称).从理论上讲,完美的解决方案是这样的:

deploy test1.war -conf <path1>
    deploy test2.war -conf <path2>
    deploy test3.war -conf <path3>

是否可以在tomcat中执行此操作?这样做有什么选择吗?

解决方法

我决定在运行时为app获取适当的配置文件.
1)使用以下代码获取war(例如warname.war)文件中当前运行的MainApp.class的路径:

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"

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();

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

4)使用以下代码读取适当的env变量:

String configFile = System.getProperty(realWarName + ".config.file");
// configFile - "D:app.properties" for warname.war
// configFile - "D:app2.properties" for warname2.war

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读