首頁  >  文章  >  後端開發  >  nginx tomcat叢集設定實作無痛重啟服務教學python語言版本

nginx tomcat叢集設定實作無痛重啟服務教學python語言版本

WBOY
WBOY原創
2016-08-08 09:22:211087瀏覽

上一次分享的是shell版本的:nginx tomcat叢集配置實現無痛重啟服務教程

感覺shell語法的怪異實在難以忍受,但java在處理腳本,和shell交互方面的天然弱勢導致我最終選擇了python來做最代碼的各種腳本實現,通過實現無痛重啟tomcat的腳本後發現除了調試不方便外,python作為腳本和shell交互簡直是神器。

下面是腳本實作:

#encoding=utf8
導入重新
導入作業系統
導入指令
導入時間
導入urllib2
from urllib2導入URLError
導入指令
導入套接字< "
埠=["8080", "8081"]
nginx_path="/usr/local/nginx/"
nginx_vhost_c/vhost/"
nginx_bin_path=nginx_path+sbin/vhost/"
nginx_bin_path=nginx_path+"sbin/nginx"
nginx_bin/cattomnginx_path+/sbin/nginx /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.sh" 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:
    name)):
             port= _port
             break
    if not is_tomcat_port_running(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)
=ofmain url 1
    try:
        res=urllib2.urlopen(url)
    ret=res.code
    except URLError, e     inx的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
_noto. name+zuidaima_conf_name_bak_suffix)):
        return -2
    os.rename(running_tomcat_conf_name 。 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)):
   # 休眠5秒
    return 1
#停止指定連接埠的tomcat服務
def shutdown_tomcat(tomcat_port):
    commands.getoutput(kill_tomcat_pid%(tomcat_port))
    while(is_tomcat_port_running(tomcat_port)
           time.sleep(5) # 休眠5秒
return 1
#切換tomcat服務
def switch_tomcat(running_tomcat_port, stoped_tomcat_port):
    startup_tomcat(stoped_tomcat_port)
 
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()  running_tomcat_port=find_running_tomcat_port()🠠 "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🠎)  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_!    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_conf8:" print "fail to reload_nginx_conf8:" print "fail to reload_nginx_return
    print "finish to switch tomcat"
start()

有圖有真相:

另外阿里雲伺服器自帶的python版本是Python 2.4.3 (#91, Jan :54)的,在編寫腳本的過程中很多語法都是高版本才有的,所以大家在學習python的過程中要注意版本的問題。

該版本的實作上因為python是高階語言,所以業務實作上很嚴密,例如啟動時根據conf檔案的後綴來確認誰是正在執行的tomcat,執行完tomcat的startup.sh後,再透過urllib去請求該連接埠的share.htm確保啟動確實成功。

enjoy it.


以上就介紹了nginx tomcat叢集配置實現無痛重啟服務教程python語言版本,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn