Home  >  Article  >  Backend Development  >  nginx tomcat cluster configuration to achieve painless restart service tutorial python language version

nginx tomcat cluster configuration to achieve painless restart service tutorial python language version

WBOY
WBOYOriginal
2016-08-08 09:22:211087browse

The last time I shared was the shell version: nginx Tomcat cluster configuration to achieve painless service restart tutorial

I feel that the weirdness of the shell syntax is really unbearable, but Java’s natural weakness in processing scripts and interacting with shells led me to finally choose python to implement various scripts in the most code. After implementing the script to restart tomcat painlessly, I found that in addition to the inconvenience of debugging, python is an artifact of script and shell interaction.

The following is the script implementation:

#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}'| _port
                                                                                                                                                                                                                                                                             break
if ret==200:
return 1
Return 0
#Request the share address with port
def request_share_url(tomcat_port):
socket.setdefaulttimeout(10)
url=zuidaima_domain+":"+tomcat_port+"/share.htm"
ret=-1
try: U Res = Urllib2.urlopen (url)
Ret = res.Code
Except Urlerror, E:
Print "Request Url#"+URL+"ERROR" Return RET
#Switch nginx's Tomcat port TDef Switch_nginx_conf (Running_tomcat_port, StOPED_TOMCAT_PORT ):
running_tomcat_c> if(not os.path.exists(running_tomcat_conf_name)):
return -1
stopped_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, stopped_tomcat_conf_name)
return 1
#Start the tomcat service on the specified port
def startup_tomcat(tomcat_port):
shutdown_tomcat (tomcat_port)
outputs=commands.getoutput(tomcat_path+tomcat_port+ tomcat_startup_bin_path)
time.sleep(5) # Sleep for 5 seconds
while(not is_tomcat_port_running(tomcat_port)):
print "start tomcat "+tomcat_port
time.sleep(5) # Sleep for 5 seconds return 1
#Stop specified port Tomcat service
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) # Sleep for 5 seconds
return 1
#Switch tomcat service
def switch_tomcat(running_tomcat_port, stopped_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, stopped_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_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()

There are pictures and the truth:

In addition, the Python version that comes with the Alibaba Cloud server is Python 2.4.3 (#1, Jan 9 2013, 06:49 :54), in the process of writing scripts, many syntaxes are only available in higher versions, so everyone should pay attention to version issues when learning python.

The implementation of this version is because Python is a high-level language, so the business implementation is very strict. For example, when starting up, you can use the suffix of the conf file to confirm who is running tomcat. After executing tomcat's startup.sh, go through urllib again. Request the port's share.htm to ensure that the startup is indeed successful.

enjoy it.


The above introduces the python language version of the nginx tomcat cluster configuration to achieve painless restart service tutorial, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn