优化方案
接上一篇:https://www.cnblogs.com/wwjj4811/p/14534847.html
上面部署方案存在的问题:
一次只能选择一个微服务部署
只有一台生产者部署服务器
每个微服务只有一个实例,容错率低
优化方案:
在一个Jenkins工程中可以选择多个微服务同时发布
在一个Jenkins工程中可以选择多台生产服务器同时部署
每个微服务都是以集群高可用形式部署
集群部署流程说明
修改微服务配置
eureka配置:
# 集群版
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.1.73
client:
service-url:
# 将自己注册到eureka-server1、eureka-server2这个Eureka上面去
defaultZone: http://192.168.1.73:10086/eureka/,http://192.168.1.74:10086/eureka/
---
server:
port: 10086
spring:
profiles: eureka-server2
eureka:
instance:
hostname: 192.168.1.74
client:
service-url:
defaultZone: http://192.168.1.73:10086/eureka/,http://192.168.1.74:10086/eureka/
其他服务的注册中心配置也需要修改
jenkins集群项目的多选类型参数
安装插件Extended Choice Parameter,安装完成后,参数化项目中就有多选选项了
分支参数:
多选:
多项目代码审查
stage('代码审查') {
for(int i= 0 ; i < selectedProjectNames.length; i++){
def projectInfo = selectedProjectNames[i];
def currentProjectName = "${projectInfo}".split("@")[0];
def currentProjectPort = "${projectInfo}".split("@")[1];
//引入全局工具配置中的sonarqube-scanner名
def scannerHome = tool 'sonar-scanner'
//引入系统配置中Sonarqube server的名
withSonarQubeEnv('sonarqube'){
sh """
cd ${currentProjectName}
${scannerHome}/bin/sonar-scanner
"""
}
}
}
多项目打包及构建上传镜像
stage('编译,打包微服务工程, 上传镜像') {
for(int i= 0 ; i < selectedProjectNames.length; i++){
def projectInfo = selectedProjectNames[i];
def currentProjectName = "${projectInfo}".split("@")[0];
def currentProjectPort = "${projectInfo}".split("@")[1];
def imageName = "${currentProjectName}:${tag}"
//打包、构建镜像
sh "mvn -f ${currentProjectName} clean install dockerfile:build"
//镜像打上标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${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_project}/${imageName}"
echo "镜像推送到harbor成功"
}
//sh "docker rmi ${harbor_url}/${harbor_project}/${imageName}"
//sh "docker rmi ${imageName}"
//sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deploy.sh $harbor_url $harbor_project $project_name $tag $port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
同时构建四个微服务:
eureka部署到多台服务器
添加远程ssh
添加多选框,支持选择多个服务器
远程部署脚本,放到需要部署微服务的机器上:/opt/jenkins_shell/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
#查询镜像是否存在,存在则删除
imaeId=`docker images | grep -w $project_name | awk '{print $3}'`
if [ "$imageId" != "" ] ; then
#删除镜像
docker rmi -f $imageId
echo "成功删除镜像"
fi
# 登录Harbor
docker login -u admin -p Harbor12345 $harbor_url
# 下载镜像
docker pull $imageName
# 启动容器
docker run -di -p $port:$port $imageName $profile
echo "容器启动成功"
修改jenkinsfile:
def git_auth = "ee475541-cfd3-42a8-b8eb-4e8f9ac5c5b7"
def git_url = "http://192.168.1.50:82/root/tensquare_back.git"
def tag = "latest"
//harbor的url
def harbor_url = "192.168.1.52:85"
def harbor_project = "tensquare"
//harbor凭证id
def harbor_auth = "25ca1748-2a03-4acb-a828-863fe805165c"
node {
//获取当前选择的项目名称
def selectedProjectNames = "${project_name}".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++){
def projectInfo = selectedProjectNames[i];
def currentProjectName = "${projectInfo}".split("@")[0];
def currentProjectPort = "${projectInfo}".split("@")[1];
//引入全局工具配置中的sonarqube-scanner名
def scannerHome = tool 'sonar-scanner'
//引入系统配置中Sonarqube server的名
withSonarQubeEnv('sonarqube'){
sh """
cd ${currentProjectName}
${scannerHome}/bin/sonar-scanner
"""
}
}
}
stage('编译,安装公共子工程') {
sh "mvn -f tensquare_common clean install"
}
def selectedServers = "${publish_server}".split(",")
stage('编译,打包微服务工程, 上传镜像') {
for(int i= 0 ; i < selectedProjectNames.length; i++){
def projectInfo = selectedProjectNames[i];
def currentProjectName = "${projectInfo}".split("@")[0];
def currentProjectPort = "${projectInfo}".split("@")[1];
def imageName = "${currentProjectName}:${tag}"
//打包、构建镜像
sh "mvn -f ${currentProjectName} clean install dockerfile:build"
//镜像打上标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${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_project}/${imageName}"
echo "镜像推送到harbor成功"
}
sh "docker rmi ${harbor_url}/${harbor_project}/${imageName}"
sh "docker rmi ${imageName}"
for(int j =0 ; j < selectedServers.length; j++){
def currentServerName = selectedServers[j];
def activeProfile = "--spring.profiles.active="
if(currentServerName == "master_server"){
activeProfile = activeProfile+"eureka_server1"
}else if(currentServerName == "slave_server"){
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)])
}
}
}
}
eureka配置文件:
# 集群版
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.1.73
client:
service-url:
# 将自己注册到eureka-server1、eureka-server2这个Eureka上面去
defaultZone: http://192.168.1.73:10086/eureka/,http://192.168.1.74:10086/eureka/
---
server:
port: 10086
spring:
profiles: eureka-server2
eureka:
instance:
hostname: 192.168.1.74
client:
service-url:
defaultZone: http://192.168.1.73:10086/eureka/,http://192.168.1.74:10086/eureka/
测试部署注册中心:
nginx+zuul集群实现高可用网关
修改nginx配置文件:
upstream zuulServer{
server 192.168.1.73:10020 weight=1;
server 192.168.1.74:10020 weight=1;
}
server {
listen 85 default_server;
listen [::]:85 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://zuulServer/;
}