1. Use Jenkins to automate deployment in microservices architecture!

In a microservice architecture, as the fragmentation of projects becomes more and more detailed, leading to more and more services, the packaging and deployment of services can become quite a hassle. The companies I worked for before were all packaged locally, uploaded to the server, and then created scripts to run. The problem was that the service accumulation increased, and the deployment cost a lot of manpower and time. Is there any way that we can deploy automatically as long as we click execute after deployment? B: of course! Let’s use Jenkins to automate an integrated deployment in a microservice architecture.

1.1 Environment Preparations

Refer to my last blog: Jenkins+Docker+Gitlab+Harbor deployment environment construction this time to use the environment has:

software version describe
Docker 19.03.8 The latest wave of container technology, service isolation
Docker-compose latest Container choreography tool
Harbor 1.10.1 Harbor private mirror warehouse service
Jenkins latest Jenkins is the leader in open source CI&CD software, providing over 1,000 plug-ins to support build, deployment, automation, and any project needs
Gitlab latest GitLab is an open source project for a warehouse management system that uses Git as a code management tool

1.2 Deployment Architecture Diagram

2 Jenkins automated deployment of spring Boot project

2.1 Upload the code to Gitlab

After uploading to the repository, start the Jenkins creation task

2.2 Jenkins task construction

  • Create a task

  • Add the GitLab project

  • Add build steps to invoke top-level Maven targets

  • Configuring the Maven Environment

Note that if the project is an aggregation project, build dependent modules in the project, otherwise when building working service modules the build will fail because they cannot be found

#Only package Admin,core and Web modules
mvn clean package -pl  core,web,admin -am
Copy the code

  • Complete the automatic push of Maven packaged to harbor mirror repository with additional script files and start the new container publishing project

#! /usr/bin/env bash
#Initialize core parameters
#Jenkins task name
task_name='test'
#The jar package name
app_name='-test'
#The release
version='latest'
#Domain name address of the Harbor mirror warehouse
harbor_registry='www.example.com'
#Mirror warehouse
image_prefix='test'
#Maven BuildMaven_version = '0.0.1 - the SNAPSHOT'#The initial port
INIT_EXPOSE=8086
#External service port
EXPOSE=8086
#Jenkins task builds the original path
jenkins_jar_path='/usr/local/docker/jenkins/jenkins_home/workspace/'${task_name}
#Building a Mirror Path
projects_path='/usr/local/projects/'

#Stop deleting containersdocker stop ${app_name} echo 'stop container '${app_name}' success! ' docker rm ${app_name} echo 'delete container '${app_name}' success! '
#Copy the JAR package to the specified directory
#Note: Single Maven is not required${app_name}, aggregation items need to be added${app_name}
cp ${jenkins_jar_path}${app_name}/target/${app_name}-${maven_version}.jar  ${projects_path}${app_name}/
cp ${jenkins_jar_path}${app_name}/src/docker/Dockerfile ${projects_path}${app_name}/

#Building a Push Imagedocker login --username=zhouxinlei --password=Zxl298828 https://${harbor_registry} docker build -t ${image_prefix}/${app_name}:${maven_version} -f ${projects_path}${app_name}/Dockerfile ${projects_path}${app_name}/. docker tag ${image_prefix}/${app_name}:${maven_version} ${harbor_registry}/${image_prefix}/${app_name}:${version} docker  push ${harbor_registry}/${image_prefix}/${app_name}:${version} docker rmi `docker images|grep none | awk '{print $3}'` docker rmi ${image_prefix}/${app_name}:${maven_version}#Run the containerdocker run -p ${EXPOSE}:${INIT_EXPOSE} --name ${app_name} -v /etc/localtime:/etc/localtime -v ${projects_path}${app_name}/logs:/var/logs -d ${harbor_registry}/${image_prefix}/${app_name}:${version} echo 'run container '${app_name}' success! 'Copy the code
  • Click Save to finish creating the test execution task.

2.3 Perform Jenkins task

  • You can view the Maven build steps on the console

  • As shown in the following figure, the Spring Boot project is successfully built and released

  • View container health

2.4 Access project interface

3 summary

By creating tasks in Jenkins, we completed the packaging and deployment of services in microservice architecture. In this way, after modifying the code each time, we only need to click the start task to achieve one-click packaging and deployment, saving the trouble of frequent packaging and deployment.

Partial reference: MacroZheng

My personal blog website, welcome everyone to watch personal blog