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

eureka发布期间优雅上下线

1、编写eureka下线脚本

bash 复制代码
vim biz_out_of_service-eureka.py

import 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上线脚本

bash 复制代码
vim xlm-biz_up-eureka.py

import 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下线脚本

bash 复制代码
#upstram后端服务设置为down
vim nginx/nginx-biz-service-down.sh 
#!/bin/bash

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"
#热加载nginx
vim  nginx/remote-nginx-reload.sh 
#!/bin/bash

nginx_ip=$1
ssh  root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"

4、配置nginx上线脚本

bash 复制代码
#upstram后端服务设置为down
vim  nginx/nginx-biz-service-up.sh 
#!/bin/bash

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"
#热加载nginx
vim  nginx/remote-nginx-reload.sh 
#!/bin/bash

nginx_ip=$1
ssh  root@${nginx_ip} "/usr/local/nginx/sbin/nginx -s reload"

5、编写服务健康接口检查脚本

bash 复制代码
vim check-health.py 
import json
import sys
import requests


ip=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发布脚本

bash 复制代码
#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发布上下线

bash 复制代码
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}
相关推荐
c语言中的小小白13 分钟前
Linux之进程概念
linux·运维·服务器
去往火星15 分钟前
ubuntu22.04取消开机输入密码(实测)
linux·运维·服务器
极客先躯19 分钟前
Hadoop-HDFS-core-site.xml,hdfs-site.xml,yarn-site.xml,krb5.conf 都是做什么的?
xml·运维·hadoop·hdfs
万界星空科技30 分钟前
MES系统实现制造业生产自动化、智能化与透明化
运维·经验分享·科技·5g·能源·制造·业界资讯
DuoRuaiMiFa33 分钟前
Replit Agent:AI驱动的全自动化软件开发革命
运维·人工智能·自动化
day3ZY1 小时前
怎么查看网站是否被谷歌收录,查看网站是否被搜索引擎收录5个方法与步骤
运维·前端·搜索引擎
Java追光着1 小时前
Linux防火墙配置绿色端口,解决无法访问java服务的问题
java·linux·运维
江南剑雨1 小时前
jenkinsfile实现镜像构建、发布
linux·运维·jenkins
不烦下雨c2 小时前
[网络]NAT、代理服务、内网穿透、内网打洞
运维·服务器·网络
陌殇殇殇2 小时前
Prometheus监控MySQL主从数据库
运维·数据库·mysql·prometheus