自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Jenkins 業(yè)務(wù)發(fā)版平滑上線實(shí)戰(zhàn)

開發(fā) 架構(gòu)
原來發(fā)版必須等到晚上10點(diǎn)訪問量最少的時(shí)候,期間服務(wù)中斷產(chǎn)生大量告警。為了不影響業(yè)務(wù),現(xiàn)在需要修改jenkins使其平滑上線。

背景簡(jiǎn)介:

原來發(fā)版必須等到晚上10點(diǎn)訪問量最少的時(shí)候,期間服務(wù)中斷產(chǎn)生大量告警。為了不影響業(yè)務(wù),現(xiàn)在需要修改jenkins使其平滑上線。具體操作步驟如下:

第一步:拉取git指定分支

Jenkins默認(rèn)一個(gè)項(xiàng)目只能對(duì)應(yīng)一個(gè)git分支來構(gòu)建,通過git Parameter插件可以從項(xiàng)目中讀取GIT SCM 配置實(shí)現(xiàn)選擇分支或tag來構(gòu)建項(xiàng)目。

pipeline {
parameters{
gitParameter(name: 'BRANCH_TAG',type: 'PT_BRANCH_TAG',defaultValue: 'master')
}
stage('gitpull') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],gitTool: 'Default',submoduleCfg: [],
userRemoteConfigs: [[url: 'http://xxx.git',credentialsId: 'xxx',]]
])
}}}

圖1-1 拉取指定分支

第二步:編譯

通過mvn編譯java代碼,將編譯后得到的jar包推送至各個(gè)服務(wù)器,其中servers_ips為各個(gè)服務(wù)器ip地址(如: servers_ips = ” 172.1.1.100 172.1.1.101”)。

script {
sh " mvn clean package -DskipTests " #編譯java項(xiàng)目代碼
for (server_ip in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity',passphr aseVariable:'',usernameVariable:'userName')])
{
remote.user = userName
remote.identityFile = identity
sshPut remote: remote, from: "./target/xxx.jar", into: "/data/" #
將jar包推送至服務(wù)器
}}}

第三步:下線指定服務(wù)器

批量修改各個(gè)服務(wù)器上nginx配置,注釋upstream中指定的服務(wù)器,使其下線不提供服務(wù),為之后新代碼上線做準(zhǔn)備,在不影響業(yè)務(wù)的情況下平滑上線。

script {
for (server_ip2 in servers_ips.tokenize(' ')){
def remote = [:]
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true

withCredentials([sshUserPrivateKey(credentialsId:xxx,keyFileVariable:'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/.*'${指定服務(wù)器ip}'/#&/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}}

第四步:重啟服務(wù)運(yùn)行新代碼

在剛下線的服務(wù)器上重啟java項(xiàng)目使其運(yùn)行新的代碼。立即重啟會(huì)導(dǎo)致原有的連接返回5xx,所以在重啟之前sleep幾秒。

sleep 30
def remote = [:]
remote.name = root
remote.host = 指定服務(wù)器ip
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: xxx, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"./prodservice.sh restart"
}

第五步:健康檢查

訪問該服務(wù)器后端提供的接口判斷是否可以提供服務(wù)。狀態(tài)碼為200即為健康即可上線提供服務(wù),否則訪問3次之后拋出異常中斷Jenkins。服務(wù)啟動(dòng)過程中進(jìn)行檢查,jenkins會(huì)拋異常并中斷,這里需要加上異常處理繼續(xù)運(yùn)行jenkins。

health_times=0
health_status = ""
while(health_times < 3){
try {
health_status = sh(script: "curl -I -m 10 -o /dev/null -s -w %{http_code} -d '' http://${server_ip}:8080/xxx", returnStdout: true).trim()
} catch(Exception e1) {
echo "服務(wù)沒起來" }
if ( health_status == '200' ){
echo "健康"
break
}else if ( health_times < 2) {
sleep 30
echo "不健康,再次嘗試檢測(cè)..."
}else{
error "不健康" }
health_times = health_times +1 }

第六步:上線指定服務(wù)器

通過健康檢查后,即可批量修改各個(gè)服務(wù)器上的nginx配置,使得該服務(wù)器上線提供服務(wù)。

for (server_ip2 in servers_ips.tokenize(' ')){
remote.name = root
remote.host = server_ip2
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credentialsId: ssh_credentialsId, keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command:"""
sed -i 's/^#*\\(.*'${server_ip}'\\)/\\1/' /etc/nginx/conf.d/xxx.conf
nginx -s reload
"""
}}

第七步:檢查

在構(gòu)建過程中循環(huán)curl服務(wù),檢查是否會(huì)出現(xiàn)服務(wù)中斷的情況。下圖7-1為構(gòu)建過程中的日志。

圖7-1 平滑上線過程

至此,Jenkins平滑上線完成。


責(zé)任編輯:武曉燕 來源: 新鈦云服
相關(guān)推薦

2021-10-26 09:27:10

MySQL分表上線

2020-01-21 21:48:07

架構(gòu)平滑上云機(jī)房遷移

2020-07-21 15:13:58

騰訊云SQL Server數(shù)據(jù)庫(kù)

2009-12-29 16:25:14

全業(yè)務(wù)接入網(wǎng)

2020-07-21 15:52:01

騰訊云數(shù)據(jù)庫(kù)SQL Server

2024-08-22 14:16:08

2022-07-27 22:48:29

消息中間件RocketMQ架構(gòu)設(shè)計(jì)

2016-01-05 11:53:27

視頻監(jiān)控智能云平臺(tái)VCM502華為

2014-04-23 17:21:45

Cooliris豌豆莢

2014-03-28 10:50:07

微軟iPadOffice

2020-06-18 09:32:15

微信移動(dòng)應(yīng)用

2017-03-30 16:21:51

互聯(lián)網(wǎng)

2023-12-11 06:27:39

MySQL線上業(yè)務(wù)優(yōu)化后臺(tái)上傳文件

2011-12-09 18:14:38

中關(guān)村商城

2017-01-20 11:14:37

紅包

2015-07-17 17:39:07

微軟Windows 1應(yīng)用商店

2012-05-06 08:36:54

iOS

2021-01-21 05:57:00

測(cè)速工具Speedtest應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)