首页  >  文章  >  后端开发  >  nginx tomcat集群配置实现无痛重启服务教程

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

WBOY
WBOY原创
2016-08-08 09:22:20855浏览

随着最代码用户量的增大,对高可用服务的依赖度日益增强,单点的服务无法避免重启给用户带来的影响,所以采用了如下方案来实现无痛重启服务。

nginx+2tomcat(8080,8081)

思路:其中正常服务的tomcat只有1个,如果重启时,启动另外一个,服务正常后,执行nginx -s reload,最后在关闭上一个服务的tomcat。

shell代码如下:

nginx_root_path="/usr/local/nginx/"
nginx_c/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_c>        mv_stop_c>        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_c>    mv_stop_c>    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的include指令实现只加载需要的conf文件:<br>

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

以上就介绍了nginx tomcat集群配置实现无痛重启服务教程,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn