Tomcat Multi Instance로 설정 하려면, 기동관련을 위해서 Shell도 만져야 하고, server.xml 도 만져야 한다.
그것을 Script에서만 하면 좋을것 같아서 고민 중, Tomcat에 적용해 보았다.
[env.sh]
#!/bin/sh DATE=`date +%Y%m%d_%H%M%S` export JAVA_HOME=/usr/local/java8 export PATH=$PATH:$JAVA_HOME/bin # Apache Balance Workers 값(WAS별 유니크) HTTPD_LB_WORKER_NM=testcluster HTTPD_WORKER_NM=$HTTPD_LB_WORKER_NM'11' PROJECT_NM_PREPIX= PROJECT_NM=test-cluster WAR_PATH=/usr/local/tomcat/webroot/$PROJECT_NM_PREPIX$PROJECT_NM WEB_ROOT=$WAR_PATH/webapp export HTTPD_WORKER_NM export PROJECT_NM export WAR_PATH export WEB_ROOT # 동일 머신에서 중복되지 않게(01~99: 최대 90개) export PORT_OFFSET=1 let SHUTDOWN_PORT=9100+$PORT_OFFSET let HTTP_PORT=9200+$PORT_OFFSET let HTTPS_PORT=9300+$PORT_OFFSET let AJP_PORT=9400+$PORT_OFFSET export SHUTDOWN_PORT export HTTP_PORT export HTTPS_PORT export AJP_PORT export CATALINA_HOME=/usr/local/tomcat/apache-tomcat export CATALINA_BASE=/usr/local/tomcat/domains/$HTTPD_LB_WORKER_NM # JVM Options : Server export JAVA_OPTS="-server $JAVA_OPTS" # JVM Options : Memory export JAVA_OPTS="$JAVA_OPTS -Xms2048m -Xmx2048m -Xss256k" # jdk7 이하 #export JAVA_OPTS="$JAVA_OPTS -XX:PermSize=256m -XX:MaxPermSize=256m" export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps " export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails " export JAVA_OPTS="$JAVA_OPTS -Xloggc:$CATALINA_BASE/logs/gc_$DATE.log " export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC " export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelOldGC " export JAVA_OPTS="$JAVA_OPTS -XX:+ExplicitGCInvokesConcurrent " export JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError " export JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=$CATALINA_BASE/logs/heapDump-$DATE.hprof " export JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true" export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.client.gcInterval=0x7FFFFFFFFFFFFFFE" export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.server.gcInterval=0x7FFFFFFFFFFFFFFE" export JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true" export JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8" export JAVA_OPTS="$JAVA_OPTS -DjvmRoute=$HTTPD_WORKER_NM" export JAVA_OPTS="$JAVA_OPTS -Dtomcat.web.root=$WEB_ROOT" export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.shutdown=$SHUTDOWN_PORT" export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.http=$HTTP_PORT" export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.https=$HTTPS_PORT" export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.ajp=$AJP_PORT" # WEB JK_MOD 정보 export HTTPD_HOSTS=( "192.0.0.100" ) export LB_WORKER=$HTTPD_LB_WORKER_NM export WORKER=$HTTPD_WORKER_NM #HTTPD_HOSTS_STR="" for i in "${HTTPD_HOSTS[@]}" do HTTPD_HOSTS_STR="${HTTPD_HOSTS_STR}${i}," done echo "=============================================================" echo " Apache Tomcat 8.x ddakker@naver.com " echo "-------------------------------------------------------------" $CATALINA_HOME/bin/./version.sh echo "-------------------------------------------------------------" echo "PROJECT_NM=$PROJECT_NM" echo "WORKER_NM=$HTTPD_WORKER_NM" echo "WEB_ROOT=$WEB_ROOT" echo "SHUTDOWN_PORT=$SHUTDOWN_PORT" echo "HTTP_PORT=$HTTP_PORT" echo "HTTPS_PORT=$HTTPS_PORT" echo "AJP_PORT=$AJP_PORT" echo "HTTPD_HOSTS=$HTTPD_HOSTS_STR" echo "============================================================="
[start.sh]
#!/bin/sh . ./env.sh # 임시 파일 삭제 rm -Rf $CATALINA_BASE/temp/* # 로그 파일 관리 rm -Rf $CATALINA_BASE/logs/catalina.out if [ "`ps -eaf | grep java | grep "$CATALINA_BASE "`" != "" ]; then echo "Tomcat SERVER - $PROJECT_NM is already RUNNING..." exit; fi if [ ! -d "$WEB_ROOT" ]; then mkdir -p $WAR_PATH mkdir -p $WEB_ROOT mkdir -p $WAR_PATH/../logs/$HTTPD_LB_WORKER_NM fi cd $CATALINA_HOME/bin ./startup.sh if [ -z "`ps -eaf | grep java | grep "$CATALINA_BASE "`" ]; then echo "Error!!! Cannot start tomcat server." fi IS_WAIT=$1 IS_JK_CTL=$2 if [ "$IS_WAIT" == "wait" ]; then while [ `netstat -an | grep :$HTTP_PORT | grep LISTEN | wc | awk '{print $1}'` != 1 ]; do echo -ne "." sleep 1 done echo "Check WAS Context Ready http://localhost:$HTTP_PORT ..." until [ "`curl --silent -o --show-error --connect-timeout 1 -w "%{http_code}\n" http://localhost:$HTTP_PORT | egrep '200|301|302|500'`" != "" ]; do echo -ne "." sleep 1 done fi if [ "$IS_JK_CTL" == "update" ]; then for HTTPD_HOST in "${HTTPD_HOSTS[@]}" do echo "<<< Turn-ON Worker $WORKER in $LB_WORKER [$HTTPD_HOST]" if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=0\" | egrep '404|500|503'`" != "" ] then echo "<<< Error" else echo "<<< Done" fi done fi
[shutdown.sh]
#!/bin/sh . ./env.sh IS_WAIT=$1 IS_JK_CTL=$2 JK_CTL_AFTER_DELAY=$3 PID=`ps -ef | grep java | grep "$CATALINA_BASE " | awk '{print $2}'` if [ e$PID == "e" ] then echo "NOT RUNNING!!!" exit; fi if [ "$JK_CTL_AFTER_DELAY" == "" ]; then JK_CTL_AFTER_DELAY=0 fi if [ "$IS_JK_CTL" == "update" ]; then for HTTPD_HOST in "${HTTPD_HOSTS[@]}" do echo "<<< Turn-OFF Worker $WORKER in $LB_WORKER [$HTTPD_HOST]" #if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=1\" | egrep '403|404|500|503'`" != "" ] if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=2\" | egrep '403|404|500|503'`" != "" ] then echo "<<< Error" else echo "<<< Done" fi done echo "+sleep $JK_CTL_AFTER_DELAY" sleep $JK_CTL_AFTER_DELAY fi $CATALINA_HOME/bin/./shutdown.sh if [ "$IS_WAIT" == "wait" ]; then I=0 until [ "`ps -eaf | grep java | grep "$CATALINA_BASE "`" == "" ]; do if [ $I == 120 ]; then ps -ef | grep java | grep "$CATALINA_BASE " | awk {'print "kill -9 " $2'} | bash -x break; fi let I=$I+1 echo -ne "." sleep 1 done fi echo "*** Tomcat process ($PID) received SHUTDOWN signal ***" >> $CATALINA_BASE/logs/catalina.out
[setting.sh]
#!/bin/sh . ./env.sh if [ ! -d "$WEB_ROOT" ]; then mkdir -p $WAR_PATH mkdir -p $WEB_ROOT mkdir -p $WAR_PATH/../logs/$HTTPD_LB_WORKER_NM fi cd $CATALINA_BASE find ./ -type d -exec chmod 750 {} \; find ./ -type f -exec chmod 740 {} \; chmod -Rf 640 $CATALINA_BASE/logs/* cd $WEB_ROOT find ./ -type d -exec chmod 755 {} \; find ./ -type f -exec chmod 640 {} \; if [ ! -e "$WEB_ROOT/index.html" ]; then echo "Hello World" >> $WEB_ROOT/index.html fi if [ ! -e "$WEB_ROOT/info.jsp" ]; then echo "<%@ page language=\"java\" contentType=\"text/html; charset=EUC-KR\" pageEncoding=\"EUC-KR\"%><%@page import=\"java.net.InetAddress\"%><%String serverIP = \"\";try{serverIP = InetAddress.getLocalHost().getHostAddress();serverIP = serverIP.substring(serverIP.lastIndexOf(\".\")+1);}catch(Exception e){}String info = request.getServerName() + \", \" + serverIP + \", \" + System.getProperty(\"jvmRoute\");out.print(info);// test%>" >> $WEB_ROOT/info.jsp fi if [ $LANG = "ko_KR.eucKR" ]; then cd $CATALINA_BASE FILE_ENC=`file -i env.sh ` UTF8_IDX=`awk -v a="$FILE_ENC" -v b="utf-8" 'BEGIN{print index(a,b)}' ` if [ $UTF8_IDX -gt 0 ]; then cd $CATALINA_BASE echo "----- encoding convertor -----" find . -name '*.sh' -exec iconv -f utf-8 -t euc-kr {} -o {} \; fi
[top.sh]
#!/bin/sh . ./env.sh PID=`ps -ef | grep java | grep "$CATALINA_BASE " | awk '{print $2}'` if [ e$PID == "e" ] then echo "NOT RUNNING!!!" exit; fi $CATALINA_BASE/../monitor_agent/./jvmtop.sh $PID
[dump.sh]
#!/bin/sh . ./env.sh for count in 1 2 3 4 5; do echo "Thread Dump : $count" for i in `ps -ef | grep java | grep "base=$CATALINA_BASE " | awk '{print $2}'`;do date #echo "+kill -3 $i" echo "+jstack -l $i >> $CATALINA_BASE/logs/thread_dump.$i" #kill -3 $i jstack -l $i >> $CATALINA_BASE/logs/thread_dump.$i #echo "sleep 1 sec" #sleep 1 done echo "done" sleep 3 done
[dump_cpu.sh]
#!/bin/sh . ./env.sh for count in 1 2 3 4 5; do echo "Thread Dump : $count" for i in `ps -ef | grep java | grep "base=$CATALINA_BASE " | awk '{print $2}'`;do top -H -b -n1 >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt date #kill -3 $i echo "+jstack -l $i >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt" jstack -l $i >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt echo "cpu snapshot and thread dump done. #" done echo "done" sleep 3 done
[httpd.sh]
#!/bin/sh . ./env.sh if [ "$1" == "stop" ]; then for HTTPD_HOST in "${HTTPD_HOSTS[@]}" do echo "<<< Turn-OFF Worker $WORKER in $LB_WORKER [$HTTPD_HOST]" if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=2\" | egrep '403|404|500|503'`" != "" ] then echo "<<< Error" else echo "<<< Done" fi done fi if [ "$1" == "start" ]; then for HTTPD_HOST in "${HTTPD_HOSTS[@]}" do echo "<<< Turn-ON Worker $WORKER in $LB_WORKER [$HTTPD_HOST]" if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=0\" | egrep '404|500|503'`" != "" ] then echo "<<< Error" else echo "<<< Done" fi done fi
[server.xml]