微服務前後端結合(她拼死拼活,想抓住一縷光。從此以後,卑微刻苦,但是不想哭。)
阿新 • • 發佈:2022-03-03
部署其他微服務
註冊中心eureka服務配置指定生產伺服器地址
服務閘道器配置指定生產伺服器地址
認證中心配置,資料庫使用Jenkins中的資料庫
活動微服務配置
修改完成進行提交
資料庫指令碼匯入Jenkins伺服器中的MySQL
mysql> create database tensquare_user;
mysql> use tensquare_user;
mysql> source /opt/tensquare_user.sql;
mysql> create database tensquare_gathering;
mysql> use tensquare_gathering;
mysql> source /opt/tensquare_gathering.sql;
------------------------授權遠端登入-------------------------------------
grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
mysql> flush privileges;
準備完畢,可以直接部署所有微服務
生產伺服器中容器列表
使用postman測試後臺資料連線
微服務持續整合(7)-部署前端靜態web網站
安裝Nginx伺服器
在生產伺服器上web_server
yum install epel-release -y
yum -y install nginx 安 裝
修改nginx的埠,預設80,改為9090:
vi/etc/nginx/nginx.conf
server {
listen 9090default_server;
listen [::]:9090default_server;
server_name _;
root /usr/share/nginx/html;
還需要關閉selinux,將SELINUX=disabled
setenforce0先臨時關閉
vi /etc/selinux/config 編輯檔案,
永久關閉 SELINUX=disabled
啟動Nginx
systemctl enable nginx 設定開機啟動
systemctl start nginx 啟動
systemctl stop nginx 停止
systemctl restart nginx 重啟
訪問:http://192.168.66.103:9090/
安裝NodeJS外掛
Jenkins配置Nginx伺服器
Manage Jenkins->Global Tool Configuration
建立前端流水線專案
tensquare_front
這裡直接在專案裡用指令碼式
//harbor的憑證
def git_auth="1be38991-873b-4a68-8eb6-312347fdc0a4"
node {
stage('pull code') {
//切換成變數,字串符號使用雙引號
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: '[email protected]:kgc_group/tensquare_front.git']]])
}
stage('make package,deploy') {
//使用nodejs的npm打包
nodejs('nodejs12'){
sh '''
npm install
npm run build
'''
}
//遠端部署
sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', 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)])
}
}
找D盤的tensquareadmin
先git自己的創庫
安裝nodejs等待時間較長
構建成功
生產伺服器驗證檢視/usr/share/nginx/html根目錄
網站成功部署!!!
5、Jenkins+Docker+SpringCloud微服務持續整合(下)
Jenkins+Docker+SpringCloud部署方案優化
上面部署方案存在的問題:
1)一次只能選擇一個微服務部署
2)只有一臺生產者部署伺服器
3)每個微服務只有一個例項,容錯率低
優化方案:
1)在一個Jenkins工程中可以選擇多個微服務同時釋出
2)在一個Jenkins工程中可以選擇多臺生產伺服器同時部署
3)每個微服務都是以叢集高可用形式部署
Jenkins+Docker+SpringCloud叢集部署流程說明
修改所有微服務配置
註冊中心配置(*)
再去啟動一臺docker2伺服器(安裝部署docker環境)
在啟動微服務的時候,加入引數: spring.profiles.active 來讀取對應的配置
# 叢集版 spring: application: name: EUREKA-HA --- server: port: 10086 spring: # 指定profile=eureka-server1 profiles: eureka-server1 eureka: instance: # 指定當profile=eureka-server1時,主機名是eureka-server1 hostname: 192.168.195.184 client: service-url: # 將自己註冊到eureka-server1、eureka-server2這個Eureka上面去 defaultZone: http://192.168.195.184:10086/eureka/,http://192.168.195.182:10086/eureka/ --- server: port: 10086 spring: profiles: eureka-server2 eureka: instance: hostname: 192.168.195.182 client: service-url: defaultZone: http://192.168.195.184:10086/eureka/,http://192.168.195.182:10086/eureka/其他微服務配置 除了Eureka註冊中心以外,其他微服務配置都需要加入所有Eureka服務 # Eureka配置eureka: client: service-url: defaultZone: http://192.168.195.182:10086/eureka,http://192.168.195.184:10086/eureka # Eureka 訪問地址 instance: prefer-ip-address: true 把程式碼提交到Gitlab中 設計Jenkins叢集專案的構建引數 1)安裝Extended Choice Parameter外掛支援多選框 2)建立流水線專案 tensquare_back_cluster 3)新增引數 字串引數:分支名稱 多選框:專案名稱 tensquare_eureka_server@10086,tensquare_zuul@10020,tensquare_admin_service@9001,tensquare_gathering@9002 最後效果(注意用英文逗號): 檢查程式碼迴圈構建
//git的憑證 def git_auth="1be38991-873b-4a68-8eb6-312347fdc0a4" //git的URL def git_url="[email protected]:kgc_group/tensquare_back.git" //映象標籤 def tag="latest" //harbor的url地址 def harbor_url="192.168.195.183:85" //映象倉庫名 def harbor_name="tensquare" //harbor的憑證 def harbor_auth="e8b4bf42-2a87-4611-90f7-4b4a75479b5c" node { //獲取當前選擇專案名稱 def selectedProjectNames="${project_name}".split(",") stage('pull code') { //切換成變數,字串符號使用雙引號 checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) } stage('check code') { 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] //定義SonarQubeScanner工具 def scannerHome = tool 'sonar-scanner' //引用SonarQube系統環境 withSonarQubeEnv('sonarqube') { sh """ cd ${currentProjectName} ${scannerHome}/bin/sonar-scanner """ } } } //新增公共子工程 stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //打包微服務,製作映象,上傳映象 stage('make package images,push images') { sh "mvn -f ${project_name} clean package dockerfile:build" //定義映象名稱 def imageName="${project_name}:${tag}" //對映象打標籤 sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}" //映象推送到harbor 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_name}/${imageName}" sh "echo 映象上傳成功" } //部署應用 sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deploy.sh ${harbor_url} ${harbor_name} ${project_name} ${tag} ${port}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } }
迴圈打包編譯製作映象 //git的憑證 def git_auth="1be38991-873b-4a68-8eb6-312347fdc0a4" //git的URL def git_url="[email protected]:kgc_group/tensquare_back.git" //映象標籤 def tag="latest" //harbor的url地址 def harbor_url="192.168.195.183:85" //映象倉庫名 def harbor_name="tensquare" //harbor的憑證 def harbor_auth="e8b4bf42-2a87-4611-90f7-4b4a75479b5c" node { //獲取當前選擇專案名稱 def selectedProjectNames="${project_name}".split(",") stage('pull code') { //切換成變數,字串符號使用雙引號 checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) } stage('check code') { 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] //定義SonarQubeScanner工具 def scannerHome = tool 'sonar-scanner' //引用SonarQube系統環境 withSonarQubeEnv('sonarqube') { sh """ cd ${currentProjectName} ${scannerHome}/bin/sonar-scanner """ } } } //新增公共子工程 stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //打包微服務,製作映象,上傳映象 stage('make package images,push images') { 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_name}/${imageName}" //映象推送到harbor 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_name}/${imageName}" sh "echo 映象上傳成功" } } //部署應用 sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deploy.sh ${harbor_url} ${harbor_name} ${project_name} ${tag} ${port}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } }希望和悲傷,都是一縷光。總有一天,我們會再相遇。