Jenkins+Docker+SpringCloud microservice continuous integration and optimization

Posted by ticallian on Sun, 06 Mar 2022 20:18:40 +0100

catalogue

1.Jenkins+Docker+SpringCloud deployment scheme optimization

2. Design the construction parameters of Jenkins cluster project

3. Multi server remote publishing of microservices

4.Nginx+Zuul cluster realizes high availability gateway

1.Jenkins+Docker+SpringCloud deployment scheme optimization

Problems in the above deployment scheme:

① Only one microservice deployment can be selected at a time

② Only one producer deployment server

③ There is only one instance of each microservice, and the fault tolerance rate is low

Optimization scheme:

① In a Jenkins project, you can select multiple microservices to publish at the same time

② In a Jenkins project, multiple production servers can be selected and deployed at the same time

③ Each microservice is deployed in the form of cluster high availability

Jenkins+Docker+SpringCloud cluster deployment process description

To configure two production servers as clusters, you need to configure the docker environment

In the eureka configuration, modify the version to the cluster version

# Cluster version
spring:
  application:
    name: EUREKA-HA


---
server:
  port: 10086
spring:
  # Specify profile = Eureka Server1
  profiles: eureka-server1
eureka:
  instance:
    # Specifies that when profile = Eureka Server1, the host name is Eureka Server1
    hostname: 192.168.100.129
  client:
    service-url:
      # Register yourself on eureka-server1 and eureka-server2
      defaultZone: http://192.168.100.129:10086/eureka/,http://192.168.100.137:10086/eureka/

---
server:
  port: 10086
spring:
  profiles: eureka-server2
eureka:
  instance:
    hostname: 192.168.100.137
  client:
    service-url:
      defaultZone: http://192.168.100.129:10086/eureka/,http://192.168.100.137:10086/eureka/

zuul gateway service configuration cluster address

admin_ Configure cluster address for service authentication center

Active microservice configuration cluster address

After all modifications are completed, submit the code to Gitlab

2. Design the construction parameters of Jenkins cluster project

Installing the Extended Choice Parameter plug-in supports multiple selection boxes

Create pipeline project

Project settings created in

Add string parameter: branch name

Continue to click Add parameter to find the Extended Choice Parameter

After all settings are completed, return to the build interface

Loop code check

//Gets the name of the currently selected item
def selectedProjectNames="${project_name}".split(",")

stage('check code') {

 //Cycle check
 for(int i=0;i<selectedProjectNames.length;i++){
      //Project information tensquare_eureka_server@10086
      def projectInfo=selectedProjectNames[i]

      //Current project name
      def currentProjectName="${projectInfo}".split("@")[0]

      //Current project port
      def currentProjectPort="${projectInfo}".split("@")[1]
      //Define the SonarQubeScanner tool
                def scannerHome = tool 'sonar-scanner'
                //Reference SonarQube system environment
                withSonarQubeEnv('sonarqube') {
                sh """
                    cd ${currentProjectName}
                    ${scannerHome}/bin/sonar-scanner
                   """
                }
    }

    }

Circular packaging compilation and image making

for (int i=0;i<selectedProjectNames.length;i++){
                //Project name tensquare_eureka_server@10086
                def projectInfo=selectedProjectNames[i]

                //Current project name
                def currentProjectName=projectInfo.split("@")[0]

                //Current port
                def currentProjectPort=projectInfo.split("@")[1]

                sh  "mvn -f ${currentProjectName} clean package dockerfile:build"

3. Multi server remote publishing of microservices

Configure remote deployment server

Copy the public key from Jenkins server to the second production server (docker environment has been configured)

Add server node in system configuration

Modify Docker configuration trust Harbor private server address

Add parameter checkbox: deploy server

After the parameter configuration is completed, view the build interface

Modify Jenkins "le build script

//Define git credentials
def git_auth="a5118464-372d-4269-a19d-2003b8911594"
//Define the URL of git
def git_url="git@192.168.100.135:kgc/tensquare_back.git"
//Define mirror label
def tag="latest"
//Define the url address of harbor
def harbor_url="192.168.100.129:85"
//Define image warehouse name
def harbor_name="tensquare"
//Define harbor voucher
def harbor_auth="483d5448-fcc4-44c5-8b5d-0740caa4c2ea"

node {
       //Gets the name of the currently selected item
       def selectedProjectNames="${project_name}".split(",")
       //Gets the currently selected server
       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') {

    //Cycle check
        for (int i=0;i<selectedProjectNames.length;i++){
            //Project information tensquare_eureka_server@10086
            def projectInfo=selectedProjectNames[i]

            //Current project name
            def currentProjectName="${projectInfo}".split("@")[0]

            //Current project port
            def currentProjectPort="${projectInfo}".split("@")[1]

            //Define the SonarQubeScanner tool
                def scannerHome = tool 'sonar-scanner'
                //Reference SonarQube system environment
                withSonarQubeEnv('sonarqube') {
                sh """
                    cd ${currentProjectName}
                    ${scannerHome}/bin/sonar-scanner
                   """
                }
        }

    }
    //Compile, package and make image
    stage('make package images') {
        sh "mvn -f tensquare_common clean install"
        }
    //Compiling and packaging microservices
    stage('make server') {

        for(int i=0;i<selectedProjectNames.length;i++){
                    //Project information tensquare_eureka_server@10086
                    def projectInfo=selectedProjectNames[i]

                    //Current project name
                    def currentProjectName="${projectInfo}".split("@")[0]

                    //Current project port
                    def currentProjectPort="${projectInfo}".split("@")[1]

                sh "mvn -f ${currentProjectName} clean package dockerfile:build"
                //Define image name
                def imageName="${currentProjectName}:${tag}"
                //Define image labeling
                sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}"
                //Upload image
                withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
                //Log in to harbor warehouse
                sh "docker login -u ${username} -p ${password} ${harbor_url}"
                    //Push image
                    sh "docker push ${harbor_url}/${harbor_name}/${imageName}"
                    sh "echo Image upload succeeded"
                }

        //Traverse all servers and deploy them separately
        for (int j=0;j<selectedServers.length;j++){
            //Get the current server name
            def currentServerName=selectedServers[j]
            //Call the contents of different server modules -- spring profiles. active=eureka-server1/eureka-server2
            def activeProfile="--spring.profiles.active="

            //Call different server configuration information according to different server names
            if (currentServerName=="master_server"){
                activeProfile=activeProfile+"eureka-server1"
            }else if (currentServerName=="slave_server"){
                activeProfile=activeProfile+"eureka-server2"
            }
            //Business deployment
             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)])
        }
      }
    }
}

Push upload gitlab

Write deploycluster SH deployment script and put it into two production servers

According to the deployed script path, click / opt/jenkins_shell/deployCluster.sh (give permission to execute)

#! /bin/sh
#Receive external parameters
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"

#Query whether the container exists, and delete it if it exists
containerId=`docker ps -a | grep -w ${project_name}:${tag}  | awk '{print $1}'`

if [ "$containerId" !=  "" ] ; then
    #Stop the container
    docker stop $containerId

    #Delete container
    docker rm $containerId
 
 echo "Successfully deleted container"
fi

#Query whether the image exists, and delete it if it exists
imageId=`docker images | grep -w $project_name  | awk '{print $3}'`

if [ "$imageId" !=  "" ] ; then
      
    #delete mirror
    docker rmi -f $imageId
 
 echo "Mirror deleted successfully"
fi


# Log in to Harbor
docker login -u zch -p Zch12345 $harbor_url

# Download Image
docker pull $imageName

# Start container
docker run -di -p $port:$port $imageName $profile

echo "Container started successfully"

Choose to build the registration center eureka with master and slave nodes

Browser view effect

Finally, build the rest

4.Nginx+Zuul cluster realizes high availability gateway

Install nginx on the second production server

After installation, set the configuration environment as the reverse proxy

[root@docker2 ~]# vim /etc/nginx/nginx.conf
server {
        listen       85;            #To prevent port conflict, change the port
        listen       [::]:85;
        server_name  _;
        root         /usr/share/nginx/html;

Restart Nginx: systemctl restart nginx

Modify the access address of the front-end Nginx

After the modification is completed, submit it to the front end with a little turtle

Build front end projects

Topics: Docker jenkins Microservices