代碼:http://www.zuidaima.com/share/1860227690056704.htm
原文:nginx tomcat叢集配置實現無痛重啟服務教程
隨著最代碼使用者量的增大,對高可用服務的依賴度日益增強,單點的服務無法避免重啟給使用者帶來的影響,所以採用了如下方案來實現無痛重啟服務。
nginx+2tomcat(8080,8081)
思路:其中正常服務的tomcat只有1個,如果重啟時,啟動另外一個,服務正常後,執行nginx -s reload,最後在關閉上一個服務的tomcat。
shell代碼如下:
nginx_root_path="/usr/local/nginx/"nginx_conf_path="${nginx_root_path}conf/vhost/"tomcat_running_8080_port="8080"tomcat_running_8081_port="8081"tomcat_startup_path="bin/startup.sh"tomcat_shutdown_path="bin/shutdown.sh"cnt=`ps -ef|grep "apache-tomcat-6.0.39_${tomcat_running_8080_port}"|wc -l`if [ "${cnt}" == "0" ] ;then stop_tomcat_port="${tomcat_running_8081_port}" start_tomcat_port="${tomcat_running_8080_port}" mv_start_conf_path="${nginx_conf_path}www.zuidaima.com_8080.conf" mv_stop_conf_path="${nginx_conf_path}www.zuidaima.com_8081.conf" url="http://www.zuidaima.com:8080/share.htm"else stop_tomcat_port="${tomcat_running_8080_port}" start_tomcat_port="${tomcat_running_8081_port}" mv_start_conf_path="${nginx_conf_path}www.zuidaima.com_8081.conf" mv_stop_conf_path="${nginx_conf_path}www.zuidaima.com_8080.conf" url="http://www.zuidaima.com:8081/share.htm"fiecho "start tomcat ${start_tomcat_port}"mv ${mv_start_conf_path}.bak ${mv_start_conf_path}mv ${mv_stop_conf_path} ${mv_stop_conf_path}.bak/usr/local/apache-tomcat-6.0.39_${start_tomcat_port}/${tomcat_startup_path}sleep 40rm share.htm -vcurl -v "${url}" -o "share.htm"sleep 10echo "nginx reload"${nginx_root_path}sbin/nginx -s reloadsleep 2echo "stop tomcat ${stop_tomcat_port}"/usr/local/apache-tomcat-6.0.39_${stop_tomcat_port}/${tomcat_shutdown_path}sleep 10ps -ef|grep tomcat-6.0.39_${stop_tomcat_port}|awk '{print $2}'|xargs kill
指令碼有些冗餘,但實現功能沒問題了,大家可以自行發揮最佳化下代碼。
相關如下:
關鍵點在利用了nginx的include指令實現只載入需要的conf檔案:
include /usr/local/nginx/conf/vhost/*.conf;