修改所有微服務配置
注冊中心配置(*)
server: port: 10086 spring: application: name: eureka # 指定profile=eureka-server1 # profiles: eureka-server1 eureka: instance: # 指定當profile=eureka-server1時,主機名是eureka-server1 hostname: 192.168.66.103 client: service-url: # 將自己注冊到eureka-server1、eureka-server2這個Eureka上面去 defaultZone: http://192.168.66.103:10086/eureka/ #,http://192.168.66.104:10086/eureka/ register-with-eureka: false fetch-registry: false #--- #server: # port: 10086 #spring: # profiles: eureka-server2 #eureka: # instance: # hostname: 192.168.66.104 # client: # service-url: # defaultZone: http://192.168.66.103:10086/eureka/,http://192.168.66.104:10086/eureka/
在啟動微服務的時候,加入參數 : spring.profiles.active 來讀取對應的配置
其他微服務配置
除了Eureka注冊中心以外,其他微服務配置都需要加入所有Eureka服務
# Eureka配置 eureka: client: service-url: defaultZone: http://192.168.66.103:10086/eureka,http://192.168.66.104:10086/eureka # Eureka訪 問地址 instance: prefer-ip-address: true
把代碼提交到Gitlab中
設計Jenkins集群項目的構建參數
1)安裝Extended Choice Parameter插件
支持多選框
2)創建流水線項目
3 )添加參數
字符串參數:分支名稱
多選框:項目名稱
最后效果:
完成微服務構建鏡像,上傳私服
//git憑證ID def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e" //git的url地址 def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back_ha.git" //鏡像的版本號 def tag = "latest" //Harbor的url地址 def harbor_url = "192.168.1.120:8001" //鏡像庫項目名稱 def harbor_project = "tensquare" //Harbor的登錄憑證ID def harbor_auth = "a45cb8c3-49f5-4394-976b-1d5d9825ca5f" node { //獲取當前選擇的項目名稱 def selectedProjectNames = "${project_name}".split(",") //獲取當前選擇的服務器名稱 def selectedServers = "${publish_server}".split(",") stage('拉取代碼') { checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) } stage('代碼審查') { for(int i=0;i<selectedProjectNames.length;i++){ //tensquare_eureka_server@10086 def projectInfo = selectedProjectNames[i]; //當前遍歷的項目名稱 def currentProjectName = "${projectInfo}".split("@")[0] //當前遍歷的項目端口 def currentProjectPort = "${projectInfo}".split("@")[1] //定義當前Jenkins的SonarQubeScanner工具 def scannerHome = tool 'sonarQube-scanner' //引用當前JenkinsSonarQube環境 withSonarQubeEnv('sonarQube6.7.4') { sh """ cd ${currentProjectName} ${scannerHome}/bin/sonar-scanner """ } } } stage('編譯,安裝公共子工程') { sh "mvn -f tensquare_common clean install" } stage('編譯,打包微服務工程,上傳鏡像') { for(int i=0;i<selectedProjectNames.length;i++){ //tensquare_eureka_server@10086 def projectInfo = selectedProjectNames[i]; //當前遍歷的項目名稱 def currentProjectName = "${projectInfo}".split("@")[0] //當前遍歷的項目端口 def currentProjectPort = "${projectInfo}".split("@")[1] sh "mvn -f ${currentProjectName} clean package dockerfile:build" //定義鏡像名稱 def imageName = "${currentProjectName}:${tag}" //對鏡像打上標簽 sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}" withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { //登錄到Harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //鏡像上傳 sh "docker push ${harbor_url}/${harbor_project}/${imageName}" sh "echo 鏡像上傳成功" } //遍歷所有服務器,分別部署 for(int j=0;j<selectedServers.length;j++){ //獲取當前遍歷的服務器名稱 def currentServerName = selectedServers[j] //加上的參數格式:--spring.profiles.active=eureka-server1/eureka-server2 def activeProfile = "--spring.profiles.active=" //根據不同的服務名稱來讀取不同的Eureka配置信息 if(currentServerName=="121"){ activeProfile = activeProfile+"eureka-server1" }else if(currentServerName=="118"){ activeProfile = activeProfile+"eureka-server2" } //部署應用 sshPublisher(publishers: [sshPublisherDesc(configName: "${currentServerName}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deployCluster.sh $harbor_url $harbor_project $currentProjectName $tag $currentProjectPort $activeProfile", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
完成微服務多服務器遠程發布
1)配置遠程部署服務器
拷貝公鑰到遠程服務器
ssh-copy-id 192.168.1.121
系統配置 ->添加遠程服務器
2 )修改Docker配置信任Harbor私服地址
[root@k8s-master ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://lara9y80.mirror.aliyuncs.com"], "insecure-registries":[ "192.168.1.120:8001" ] } [root@k8s-master ~]#
3)編寫deployCluster.sh部署腳本
[root@servcie01 jenkins_shell]# cat deployCluster.sh #! /bin/sh #接收外部參數 harbor_url=$1 harbor_project_name=$2 project_name=$3 tag=$4 port=$5 profile=$6 imageName=$harbor_url/$harbor_project_name/$project_name:$tag echo "$imageName" #查詢容器是否存在,存在則刪除 containerId=`docker ps -a | grep -w ${project_name}:${tag} | awk '{print $1}'` if [ "$containerId" != "" ] ; then #停掉容器 docker stop $containerId #刪除容器 docker rm $containerId echo "成功刪除容器" fi #查詢鏡像是否存在,存在則刪除 imageId=`docker images | grep -w $project_name | awk '{print $3}'` if [ "$imageId" != "" ] ; then #刪除鏡像 docker rmi -f $imageId echo "成功刪除鏡像" fi # 登錄Harbor docker login -u eric -p Eric123456 $harbor_url # 下載鏡像 docker pull $imageName # 啟動容器 docker run -di -p $port:$port $imageName $profile echo "容器啟動成功" [root@servcie01 jenkins_shell]#
集群效果
Nginx+Zuul集群實現高可用網關
3 )重啟Nginx: systemctl restart nginx
4)修改前端Nginx的訪問地址
流水線
node { stage('拉取代碼'){ checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '979cf306-0c46-4ccd-ab6b-8f4f0c7e03da', url: 'git@192.168.1.120:dalianpai_group/tensquare_font.git']]]) } stage('打包,部署網站'){ nodejs('121NodeJs') { sh ''' npm i npm run build ''' } sshPublisher(publishers: [sshPublisherDesc(configName: '121', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } }
所有服務結果