Jenkins+Docker+SpringCloud微服务持续集成(六):微服务发布

链接:https://pan.baidu.com/s/1HR3Q-cojnCidRy1xrdlOxg
提取码:7m56
--来自百度网盘超级会员V3的分享

完成微服务多服务器远程发布
1)配置远程部署服务器
从Jenkins服务器拷贝公钥到docker2远程服务器
ssh-copy-id 192.168.195.182

系统配置->添加远程服务器
image

2)修改Docker配置信任Harbor私服地址

点击查看代码
{
  "registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"], 
  "insecure-registries": ["192.168.195.183:85"]
}

重启Docker

3)添加参数
多选框:部署服务器
image
image

最终效果:
image

4)修改Jenkinsfile构建脚本

点击查看代码
//git的凭证
def git_auth="1be38991-873b-4a68-8eb6-312347fdc0a4"
//git的URL
def git_url="git@192.168.195.180:devops_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(",")
    //获取当前选择服务器
    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 "容器启动成功"

image
image

6)集群效果
image
image

后台微服务集群成功

Nginx+Zuul集群实现高可用网关
image

1)docker2服务器上安装Nginx(已完成)
vi /etc/nginx/nginx.conf
内容如下:
image

3)重启Nginx:
systemctl restart nginx

4)修改前端Nginx的访问地址
image

再次构建前端工程

posted @ 2022-03-06 22:01  视觉装置在笑  阅读(76)  评论(0编辑  收藏  举报