最代码官方
2014-06-21 16:32:26
原证精
nginx tomcat集群配置实现无痛重启服务教程python语言版本
上一次分享的是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_conf_path=nginx_path+"conf/vhost/" nginx_bin_path=nginx_path+"sbin/nginx" zuidaima_conf_name="www.zuidaima.com_%s.conf" tomcat_path="/usr/local/apache-tomcat-_" zuidaima_conf_name_bak_suffix=".bak" 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_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(_port) 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_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(running_tomcat_port) if(not os.path.exists(running_tomcat_conf_name)): return -1 stoped_tomcat_conf_name=nginx_vhost_conf_path+zuidaima_conf_name%(stoped_tomcat_port) 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()
有图有真相:
另外阿里云服务器自带的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集群配置实现无痛重启服务教程
- java通过ftp和sftp上传war包上传到Linux服务器实现自动重启tomcat的脚本代码
- 【小C出品】Tomcat实现自动重启脚本
- java重启多个tomcat,可打包成jar文件直接运行
- java编写一个迷你版的Tomcat服务器,适合初学者
- Java端口监听tomcat脚本程序代码分享
- Linux一键启动、停止、重启Tomcat sh脚本
- tomcat7单点登录安装证书配置教程
- 【小C出品】应学员的要求,java实现基于eclipse插件杀死TOMCAT进程的代码
- ZooKeeper伪分布集群安装及使用 RMI+ZooKeeper实现远程调用框架
- Spring整合Quartz实现分布式集群实例
- Spring MVC+Mybatis+shiro整合实现适配移动端的家政服务平台
请下载代码后再发表评论



dearxo2014 LV1
2024年11月30日
lee123321 LV22
2023年12月19日
2292250314 LV2
2023年5月28日
浪里格朗 LV4
2023年1月31日
青灯呀 LV10
2022年11月22日
stonemachen LV3
2022年10月31日
hekewen1 LV7
2022年5月13日
ssy552 LV10
2022年4月28日
12522425 LV13
2022年3月20日
jackcio LV9
2021年12月28日