欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > jenkins配置eureka、nacos发布优雅上下线服务

jenkins配置eureka、nacos发布优雅上下线服务

2024/10/25 18:34:52 来源:https://blog.csdn.net/weixin_45112997/article/details/142641184  浏览:    关键词:jenkins配置eureka、nacos发布优雅上下线服务

eureka发布期间优雅上下线

1、编写eureka下线脚本

vim biz_out_of_service-eureka.pyimport sys
import requests#服务名,脚本第一个参数
APP_NAME=sys.argv[1]
# 需要置为OUT_OF_SERVICE的服务实例的ID,脚本第二个参数
INSTANCE_ID=sys.argv[2]# Eureka服务器的URL
eureka_url = "https://eureka_url/eureka/apps"# 构建Eureka中实例的置为OUT_OF_SERVICE的URL
instance_out_of_service_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=OUT_OF_SERVICE"# 使用PUT请求将实例置为OUT_OF_SERVICE
response = requests.put(instance_out_of_service_url)
#使用delete请求将实例从eureka上删除
#response = requests.delete(instance_delete_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]
# 需要置为OUT_OF_SERVICE的服务实例的ID,脚本第二个参数
INSTANCE_ID=sys.argv[2]# Eureka服务器的URL
eureka_url = "https://eureka_url/eureka/apps"# 构建Eureka中实例的置为OUT_OF_SERVICE的URL
instance_up_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=UP"# 使用PUT请求将实例置为OUT_OF_SERVICE
response = requests.put(instance_up_url)
print(response.status_code)

3、配置nginx下线脚本

#upstram后端服务设置为down
vim nginx/nginx-biz-service-down.sh 
#!/bin/bashnginx_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"
#热加载nginx
vim  nginx/remote-nginx-reload.sh 
#!/bin/bashnginx_ip=$1
ssh  root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"

4、配置nginx上线脚本

#upstram后端服务设置为down
vim  nginx/nginx-biz-service-up.sh 
#!/bin/bashnginx_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"
#热加载nginx
vim  nginx/remote-nginx-reload.sh 
#!/bin/bashnginx_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
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":# 抛出一个常规的ValueError异常raise ValueError("发布异常")
else:print("服务:{} ip:{} 发布成功".format(service_name, ip))

6、配置jenkins发布脚本

#eureka实例APPNAME
app_name=SERVICE_NAME
#eureka实例ID
instance_id=serviceIP:9796
#服务器端口
port=9796
#服务器ip
remote_ip=172.19.38.133
#nginxIP
nginx_biz_1_ip=172.19.38.93
nginx_biz_2_ip=172.19.38.124
#jar包目录
project_path=service
#jar包名字
package=servicename-web.jar
#服务器上程序所在加目录
release_path=/usr/local/servicename/#编译打包
mvn clean package -DskipTests=true#同步jar包到远程服务器
rsync -av --progress ${project_path}/target/${package}  root@${remote_ip}:/data/jenkins/package/#nginx服务下线
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}#eureka服务下线
echo "eureka服务下线"
python3 /data/script/eureka_biz_out_of_service-eureka.py ${app_name} ${instance_id}#eureka服务下线后等待30s
sleep 35s#重启服务
ssh root@${remote_ip} "source /etc/profile && /usr/bin/cp -p /data/jenkins/package/${package}  ${release_path}  && ${release_path}web.sh restart"#重启服务后等待40s
echo "正在重启服务,等待40s"
sleep 40s#服务健康检查
python3 /usr/local/script/check_http_code.py ${remote_ip} ${port} ${app_name}#eureka服务上线
echo "eureka服务上线"
python3 /usr/local/script/eureka_biz_up-eureka.py ${app_name} ${instance_id}#nginx服务上线
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
#设置下线权重0
down_weight=0
#设置上线权重1
up_weight=1
#设置namespaceID
namespaceId=namespaceId
rsync -av --progress  service-name/target/service-name.jar  root@${remote_ip}:/data/jenkins/package/
#nacos服务下线
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
#check health
python3 /data/script/check-health.py ${remote_ip} ${port} ${service_name}
#nacos服务上线
python3 /data/script/nacos-service-update.py ${service_name} ${remote_ip} ${port} ${up_weight} ${namespaceId}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com