eureka发布期间优雅上下线
1、编写eureka下线脚本
vim biz_out_of_service-eureka.pyimport sys
import requests
APP_NAME = sys.argv[ 1 ]
INSTANCE_ID = sys.argv[ 2 ]
eureka_url = "https://eureka_url/eureka/apps"
instance_out_of_service_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=OUT_OF_SERVICE"
response = requests.put( instance_out_of_service_url)
print( response.status_code)
if response.status_code == 200 :print( '服务实例成功下线' )
else:print( '服务实例下线失败,状态码:' , response.status_code)
[ root@continuous-deployment script]
2、编写eureka上线脚本
vim xlm-biz_up-eureka.pyimport sys
import requests
APP_NAME = sys.argv[ 1 ]
INSTANCE_ID = sys.argv[ 2 ]
eureka_url = "https://eureka_url/eureka/apps"
instance_up_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=UP"
response = requests.put( instance_up_url)
print( response.status_code)
3、配置nginx下线脚本
vim nginx/nginx-biz-service-down.sh
nginx_ip = $1
remote_ip = $2
port = $3
ssh -p 22 root@${nginx_ip} "sed -i 's/server ${remote_ip} :${port} ;/server ${remote_ip} :${port} down;/' /usr/local/nginx/conf/vhosts/upstream.conf"
vim nginx/remote-nginx-reload.sh
nginx_ip = $1
ssh root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"
4、配置nginx上线脚本
vim nginx/nginx-biz-service-up.sh
nginx_ip = $1
remote_ip = $2
port = $3
ssh -p 22 root@${nginx_ip} "sed -i 's/server ${remote_ip} :${port} down;/server ${remote_ip} :${port} ;/' /usr/local/nginx/conf/vhosts/upstream.conf"
vim nginx/remote-nginx-reload.sh
nginx_ip = $1
ssh root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"
5、编写服务健康接口检查脚本
vim check-health.py
import json
import sys
import requestsip = sys.argv[ 1 ]
port = sys.argv[ 2 ]
service_name = sys.argv[ 3 ]
url = "http://" +ip+":" +port+"/actuator/health"
print( url)
response = requests.get( url)
print( response.text) data = json.loads( response.text)
health_status = data[ "status" ]
if health_status != "UP" : raise ValueError( "发布异常" )
else:print( "服务:{} ip:{} 发布成功" .format( service_name, ip ))
6、配置jenkins发布脚本
app_name = SERVICE_NAME
instance_id = serviceIP:9796
port = 9796
remote_ip = 172.19 .38.133
nginx_biz_1_ip = 172.19 .38.93
nginx_biz_2_ip = 172.19 .38.124
project_path = service
package = servicename-web.jar
release_path = /usr/local/servicename/
mvn clean package -DskipTests= true
rsync -av --progress ${project_path} /target/${package} root@${remote_ip} :/data/jenkins/package/
echo "nginx_biz_1服务下线"
/data/script/nginx/nginx-service-down.sh ${nginx_biz_1_ip} ${remote_ip} ${port}
/data/script/nginx/nginx-remote-nginx-reload.sh ${nginx_biz_1_ip}
echo "nginx_biz_2服务下线"
/data/script/nginx/nginx-service-down.sh ${nginx_biz_2_ip} ${remote_ip} ${port}
/data/script/nginx/nginx-remote-nginx-reload.sh ${nginx_biz_2_ip}
echo "eureka服务下线"
python3 /data/script/eureka_biz_out_of_service-eureka.py ${app_name} ${instance_id}
sleep 35s
ssh root@${remote_ip} "source /etc/profile && /usr/bin/cp -p /data/jenkins/package/${package} ${release_path} && ${release_path} web.sh restart"
echo "正在重启服务,等待40s"
sleep 40s
python3 /usr/local/script/check_http_code.py ${remote_ip} ${port} ${app_name}
echo "eureka服务上线"
python3 /usr/local/script/eureka_biz_up-eureka.py ${app_name} ${instance_id}
echo "nginx_biz_1服务上线"
/data/script/nginx/xlm-service-up.sh ${nginx_biz_1_ip} ${remote_ip} ${port}
/data/script/nginx/xlm-remote-nginx-reload.sh ${nginx_biz_1_ip}
echo "nginx_biz_2服务上线"
/data/script/nginx/xlm-service-up.sh ${nginx_biz_2_ip} ${remote_ip} ${port}
/data/script/nginx/xlm-remote-nginx-reload.sh ${nginx_biz_2_ip}
nacos发布期间优雅上下线
1、配置nacos上下线脚本
vim nacos-service-update.py
import json
import requests
import sys# 目标URL
url = 'http://nacos地址:8848/nacos/v2/ns/instance'# PUT请求的数据
service_name=sys.argv[1]
ip=sys.argv[2]
port=sys.argv[3]
weight=sys.argv[4]
namespace_id=sys.argv[5]
data = {'serviceName': service_name,'ip':ip,'port':port,"weight":weight,"ephemeral":"true","namespaceId":namespace_id}# 发送PUT请求
response = requests.put(url, data=data)# 输出响应内容
print(response.text)
json_data=json.loads(response.text)
print(json_data)
response_code=json_data["code"]
if response_code != 0:# 抛出一个常规的ValueError异常raise ValueError("下线异常")
if weight=="0":print("服务:{} ip:{} 下线成功".format(service_name,ip))
else:print("服务:{} ip:{} 上线成功".format(service_name,ip))
2、配置jenkins发布上下线
mvn clean package -U -DskipTests= true
remote_ip = service-ip
service_name = service-name
port = 8085
down_weight = 0
up_weight = 1
namespaceId = namespaceId
rsync -av --progress service-name/target/service-name.jar root@${remote_ip} :/data/jenkins/package/
python3 /usr/local/script/nacos-service-update.py ${service_name} ${remote_ip} ${port} ${down_weight} ${namespaceId}
sleep 35
ssh root@${remote_ip} "source /etc/profile && /data/service-name/service-name-release.sh"
echo "-------------等待启动-------------"
sleep 40
python3 /data/script/check-health.py ${remote_ip} ${port} ${service_name}
python3 /data/script/nacos-service-update.py ${service_name} ${remote_ip} ${port} ${up_weight} ${namespaceId}