微服务远程发布(至少一分钟前,你根本不知道会有这个吻。一无所有的时候,说明你该拥有的,还没有到来。)

完成微服务多服务器远程发布
1) 配置远程部署服务器
Jenkins服务器拷贝公钥到docker2远程服务器
ssh-copy-id 192.168.195.182
系统配置->添加远程服务器
 
2) 修改Docker配置信任Harbor私服地址
{
"registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.195.183:85"]
}
重启Docker
 
 
3) 添加参数
多选框:部署服务器

 

最终效果:

 

4) 修改Jenkinsfile构建脚本
//git的凭证
def git_auth="1be38991-873b-4a68-8eb6-312347fdc0a4"
//gitURL
def git_url="git@192.168.195.180:kgc_group/tensquare_back.git"
//镜像标签
def tag="latest"
//harborurl地址
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(",")
    //获取当前选择服务器
    def selectedServers="${publish_server}".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 镜像上传成功"
            }
        
            //遍历所有服务器,分别部署
            for (int j=0;j<selectedServers.length;j++){
                //获取当前服务器名称
                def currentServerName=selectedServers[j]
                //调用不同服务器模块内容--spring.profiles.active=eureka-server1/eureka-server2
                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_name} ${currentProjectName} ${tag} ${currentProjectPort} ${activeProfile}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
 
            }
  }
    }
 
}
推送上传gitlab
5) 编写deployCluster.sh部署脚本,放到两台生产服务器中路径:/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
 
#查询镜像是否存在,存在则删除
imageId=`docker images | grep -w $project_name  | awk '{print $3}'`
 
if [ "$imageId" !=  "" ] ; then
      
    #删除镜像
    docker rmi -f $imageId
 
 echo "成功删除镜像"
fi
 
 
# 登录Harbor
docker login -u tom -p Abcd1234 $harbor_url
 
# 下载镜像
docker pull $imageName
 
# 启动容器
docker run -di -p $port:$port $imageName $profile
 
echo "容器启动成功"

 

两台生产服务器集群都启动成功!!!
6) 集群效果

 

后台微服务集群成功!!!
 
Nginx+Zuul集群实现高可用网关

 

 

1)docker2服务器上安装
yum install epel-release -y
yum -y install nginx
 
2)Nginx(已完成)端口也要修改
vi /etc/nginx/nginx.conf
upstream zuulServer{
        server 192.168.153.50:10020 weight=1;
        server 192.168.153.30:10020 weight=1;
    }
location / {
          proxy_pass http://zuulServer/;
        }
 
内容如下:

 

3)重启Nginx systemctl restart nginx 
4)修改前端Nginx的访问地址

 

再次构建前端工程

 

集群网站成功!!!
 
posted @ 2022-03-04 20:00  十一没有撤退可言!  阅读(91)  评论(0编辑  收藏  举报