nginx tomcat集群配置实现无痛重启服务教程

阅读更多

代码下载地址: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"
fi

echo "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 40

rm share.htm -v
curl -v "${url}" -o "share.htm"
sleep 10

echo "nginx reload"
${nginx_root_path}sbin/nginx -s reload

sleep 2
echo "stop tomcat ${stop_tomcat_port}"
/usr/local/apache-tomcat-6.0.39_${stop_tomcat_port}/${tomcat_shutdown_path}
sleep 10
ps -ef|grep tomcat-6.0.39_${stop_tomcat_port}|awk '{print $2}'|xargs kill

脚本有些冗余,但实现功能没问题了,大家可以自行发挥优化下代码。

相关截图如下:

nginx tomcat集群配置实现无痛重启服务教程_第1张图片

关键点在利用了nginx的include指令实现只加载需要的conf文件:

include /usr/local/nginx/conf/vhost/*.conf;

 

你可能感兴趣的:(nginx,tomcat,集群,重启)