2017-10-25 9 views
3

웹 프로젝트에 REST API가 있습니다. 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
...
문제는 각 전쟁 파일 다른 사용해야된다 설정 파일. 수출 CATALINA_OPTs="Dparam1=/usr/config1.txt"을 사용하여 env 변수를 설정할 수 있다는 것을 알고 있습니다. 그런 다음 test1.war의 param1, test2.war의 param2를 읽으려면 각 war 파일 내부의 소스 코드를 변경해야합니다. 그러나 각 전쟁 파일은 동일해야합니다 (다른 이름 만).
Deploy * 매개 변수가있는 .war 파일

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


는 바람둥이에서 그것을 할 수 있는가 : 이론적으로 완벽한 솔루션은 다음과 같이인가? 이 작업을 수행 할 수있는 대안이 있습니까?

+1

당신 마일 ght는 [tomcat 메일 링리스트] (http://tomcat.apache.org/lists.html#tomcat-users)에서도이 질문을하고 싶어합니다. – zack6849

답변

0

런타임에 앱에 적합한 구성 파일을 가져 오기로 결정했습니다.

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) 구문 분석이 순서대로 경로를 디코딩 :
1)
(예 : warname.war) 등의 코드로 파일을 전쟁에 내 현재 실행중인 MainApp.class의 경로를 가져옵니다 "을 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에서 파일 "setenv.bat"를 만들기}/빈 /와 warname.warwarname2.war에 대한 warname2.config.file 거기 (warname.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