欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

nginx tomcat集群配置实现无痛重启服务教程python语言版本

程序员文章站 2024-04-04 13:48:59
...
上一次分享的是shell版本的:nginx tomcat集群配置实现无痛重启服务教程

感觉shell语法的怪异实在难以忍受,但java在处理脚本,和shell交互方面的天然弱势导致我最终选择了python来做最代码的各种脚本实现,通过实现无痛重启tomcat的脚本后发现除了调试不方便外,python作为脚本和shell交互简直是神器。

下面是脚本实现:

#encoding=utf8
import re
import os
import commands
import time
import urllib2
from urllib2 import URLError
import socket
tomcat_ps_name="apache-tomcat-6_"
flag=tomcat_ps_name+"\d{4}"
ports=["8080", "8081"]
nginx_path="/usr/local/nginx/"
nginx_vhost_c/vhost/"
nginx_bin_path=nginx_path+"sbin/nginx"
zuidaima_c>tomcat_path="/usr/local/apache-tomcat-_"
zuidaima_c>tomcat_startup_bin_path="/bin/startup.sh"
tomcat_shutdown_bin_path="/bin/shutdown.sh"
ps_grep_tomcat="ps -ef|grep "
zuidaima_domain="http://www.zuidaima.com"
kill_tomcat_pid=ps_grep_tomcat+tomcat_ps_name+"%s|awk '{print $2}'|xargs kill"
#通过conf文件是否是bak来确认正在运行的tomcat端口
def find_running_tomcat_port():
for _port in ports:
tomcat_c> if(os.path.exists(tomcat_conf_name)):
port=_port
break
if not is_tomcat_port_running(port):
return -1
return port
#判断指定的tomcat端口是否在运行
def is_tomcat_port_running(tomcat_port):
ret=request_share_url(tomcat_port)
if ret==200:
return 1
return 0
#请求带端口的share地址
def request_share_url(tomcat_port):
socket.setdefaulttimeout(10)
url=zuidaima_domain+":"+tomcat_port+"/share.htm"
ret=-1
try:
res=urllib2.urlopen(url)
ret=res.code
except URLError, e:
print "request url#"+url+" error"
return ret
#切换nginx的tomcat端口
def switch_nginx_conf(running_tomcat_port, stoped_tomcat_port):
running_tomcat_c> if(not os.path.exists(running_tomcat_conf_name)):
return -1
stoped_tomcat_c> if(not os.path.exists(stoped_tomcat_conf_name+zuidaima_conf_name_bak_suffix)):
return -2
os.rename(running_tomcat_conf_name, running_tomcat_conf_name+zuidaima_conf_name_bak_suffix)
os.rename(stoped_tomcat_conf_name+zuidaima_conf_name_bak_suffix, stoped_tomcat_conf_name)
return 1
#启动指定端口的tomcat服务
def startup_tomcat(tomcat_port):
shutdown_tomcat(tomcat_port)
outputs=commands.getoutput(tomcat_path+tomcat_port+tomcat_startup_bin_path)
time.sleep(5) # 休眠5秒
while(not is_tomcat_port_running(tomcat_port)):
print "start tomcat "+tomcat_port
time.sleep(5) # 休眠5秒
return 1
#停止指定端口的tomcat服务
def shutdown_tomcat(tomcat_port):
commands.getoutput(kill_tomcat_pid%(tomcat_port))
while(is_tomcat_port_running(tomcat_port)):
print "stop tomcat "+tomcat_port
time.sleep(5) # 休眠5秒
return 1
#切换tomcat服务
def switch_tomcat(running_tomcat_port, stoped_tomcat_port):
startup_tomcat(stoped_tomcat_port)
shutdown_tomcat(running_tomcat_port)
return 1
#reload nginx conf
def reload_nginx_conf():
commands.getoutput(nginx_bin_path+" -s reload")
return 1
def start():
print "start to switch tomcat"
running_tomcat_port=find_running_tomcat_port()
if running_tomcat_port==-1:
print "running tomcat & conf is invalid"
return
ports.remove(running_tomcat_port)
stoped_tomcat_port=ports[0]
print "start to switch tomcat from "+running_tomcat_port+" to "+stoped_tomcat_port
ret=switch_tomcat(running_tomcat_port, stoped_tomcat_port)
if(ret!=1):
print "fail to switch_tomcat,ret:", ret
return
print "start to switch nginx conf"
ret=switch_nginx_conf(running_tomcat_port, stoped_tomcat_port)
if(ret!=1):
print "fail to switch_nginx_conf,ret:", ret
return
print "start to reload nginx conf"
ret=reload_nginx_conf()
if(ret!=1):
print "fail to reload_nginx_conf,ret:", ret
return
print "finish to switch tomcat"
start()

有图有真相:

nginx tomcat集群配置实现无痛重启服务教程python语言版本

另外阿里云服务器自带的python版本是Python 2.4.3 (#1, Jan 9 2013, 06:49:54)的,在编写脚本的过程中很多语法都是高版本才有的,所以大家在学习python的过程中要注意版本的问题。

该版本的实现上因为python是高级语言,所以业务实现上很严密,比如启动时根据conf文件的后缀来确认谁是正在运行的tomcat,执行完tomcat的startup.sh后,再次通过urllib去请求该端口的share.htm确保启动确实成功。

enjoy it.


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