Spring cloud + docker + Jenkins + gitlab + Maven to realize automatic construction and deployment

Java architecture_ Twitter 2021-06-23 15:32:15
spring cloud docker jenkins gitlab


1、 Foreword and original intention

This article will cover Docker Common command basic knowledge points are used in combination with different scenarios .

This article will focus on the project case scenario of deploying servers in different environments in the process of work .

This article mainly talks about Docker、Jenkins、GitLab、Git、JDK、SpringBoot、Maven And other technologies to achieve automatic operation and maintenance deployment (DevOps) Application engineering , fit SpringCloud Deploy .

Original idea : In the process of learning encountered more interesting problems 、 But it took a little effort and time to sort it out , However, to sort out an article is more complete and has a knowledge system DevOps Automation build and deploy engineering article , There is a lot of technical knowledge , And the content of the article is long , However, it is divided into several articles of association

2、 What is? DevOps?

DevOps(Development and Operations The combination of the words ) It's a set of processes 、 A general term for methods and systems , To facilitate development ( Applications / Software Engineering )、 Technical operations and quality assurance (QA) Communication between departments 、 Collaboration and integration , It is a value “ Software developer (Dev)” and “IT Operation and maintenance technicians (Ops)” Communication and cooperation between cultures 、 Sports or practices . Through automation “ Software delivery ” and “ Architectural change ” The process of , To make the build 、 test 、 Distributing software can be faster 、 Frequent and reliable .

It emerged as the software industry became increasingly aware of it : To deliver software products and services on time , Development and operations must work closely together .

3 It involves the construction of software environment

  • How to be in Centos7 install JDK1.8-u121 Detailed explanation
  • How to be in Centos7 install Maven3.6.1 Detailed explanation
  • How to be in Centos7 install Git Detailed explanation
  • How to be in CentOS7 And Git Configure password free login details
  • How to be in Docker install GitLab Detailed explanation
  • How to be in Docker establish NetWork Network details
  • How to be in Docker install Registry Detailed explanation of private service
  • How to be in Docker install Jenkins Detailed explanation

See appendix

In particular

1、 How to use Maven combination Docker hold SpringBoot The application is compiled into an available image for deployment .

2、 among JDK and Maven It's the traditional way to install , Because I Centos The operating system has other software that depends on them , Sometimes it's better to install software in the traditional way , There's just a lot of elaboration here . Some of the software is in the Docker The installation process and use process are not as simple as the traditional way , such as :Jenkins.

4、 What are the tasks that need to be prepared

4.1 Engineering structure

  • open IDEA or Eclipse Create a new one SpringBoot Application .

Special instructions for environment configuration

matters needing attention : among Gitlab、Registry、Jenkins All installed in node1 On the machine , That is to say node1 As a host (master),node2 As slave( Slave machine or auxiliary machine ), The machine name is meaningful or distinguishable , Recommend master and slave, I won't elaborate too much here , In order to avoid reading the article in doubt , Look at the list .

4.2、SpringBoot Configuration and code details

4.2.1 engineering pom.xml To configure
<dependencies>
<!-- Springboot Rely on the Jar package -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Springboot Thermal deployment jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--yml Configuration file prompt plug-in -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- spring-boot test jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<!-- Be sure to declare the following configuration pack xml To Jar package -->
<!-- <resources>
<resource>
<directory>src/main/java</directory>
Whether to replace the attribute in the resource
<filtering>false</filtering>
</resource>
</resources>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- The default support jdk1.8 compile -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--docke rmaven Compile the plug-in -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.flong.SpringbootApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
4.2.2 no main manifest attribute Error resolution
  • Configure the main entrance of the project
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.flong.SpringbootApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
4.2.3 env Environment variables file
  • Used to set environment dynamic parameters , The file is .env For format
JAVA_OPTS_DEFAULT=-Xmx512m
4.2.4 Dockerfile Packaging project image details
  • In the development environment Dockerfile For example , If it's a test environment, then , Include all paths in springboot_dev Change to springboot_test
FROM frolvlad/alpine-oraclejdk8:slim
MAINTAINER jilongliang@sina.com
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir -p /home/devsoft/springboot_dev
WORKDIR /home/devsoft/springboot_dev
EXPOSE 7011
ADD ./target/springboot.jar ./
CMD java ${JAVA_OPTS_DEFAULT} -Djava.security.egd=file:/dev/./urandom -jar springboot.jar
  • Parameter description

  • WORKDIR The working directory indicates that when you enter the container, there will be a .jar Is in Dockerfile Of ADD Add in
docker exec -it Container name or container id /bin/sh

Or to use sh and bash Want to see COMMAND,-it

docker exec -it Container name or container id/bin/bash

4.2.5 build.sh file shell Script details

Be careful 1: After testing dynamic variables 【 Equal sign 】 There can't be spaces and tab Key set , Otherwise, we can't get the value , And in shell Script code does not support space formatting , Support tab Set formatting . At terminal (ssh Software side ) or Jenkins client shell command ,『 Positional variable 』 The parameters of are separated by spaces .

Such as :sh build.sh 192.168.1.235 springboot 0.0.1 7011 /home/jenkins/workspace/springboot_dev

$IMG_NAME:$IMG_VERSION This IMG_VERSION edition (tag) Parameter does not specify the default latest

Be careful 2: Usually Docker It is executed by default Dockerfile, But you can customize the suffix file to compile , The premise must be -f(force) Force the specified file to run

#!/usr/bin/env bash
# Of dynamic variables 【 Equal sign 】 There can't be spaces and tab Key set , Otherwise, we can't get the value , And in shell Script code does not support space formatting , Support tab Set formatting .
# At terminal (ssh Software side ) or Jenkins client shell command , Parameters are separated by spaces . Such as :sh build.sh 192.168.1.235 springboot 0.0.1 7011 /home/jenkins/workspace/springboot_dev
IMG_SERVER="$1"
IMG_NAME="$2"
IMG_VERSION="$3"
IMG_PORT="$4"
RUN_EVN="$5"
IMG_PATH="$6"
echo " Service address :$IMG_SERVER"
echo " Project image name :$IMG_NAME"
echo " Project version number :$IMG_VERSION"
echo " Engineering port :$IMG_PORT"
echo " Service environment :$RUN_EVN"
# Private visit url After the path and compilation, the image file is stored in the specified path , Do not process dynamic parameters and transfer values .
REGISTRY_URL="192.168.1.235:5000"
IMG_TAR_GZ_PATH="/home/img_tar_gz_path/"
# Perform the following operation only when the dynamic parameter is not an empty string
if [ "$IMG_SERVER" != "" ] && [ "$IMG_NAME" != "" ] && [ "$IMG_VERSION" != "" ] && [ "$IMG_PORT" != "" ]; then
echo " ....... Enter delete Container & Images operation ......."
# Clean up the image , free disk space
#docker images|grep none|awk '{print $3 }'|xargs docker rmi
# Get the container ID
CONTAINER_ID=`docker ps -a | grep $IMG_NAME | awk '{ print $1 }'`
# Get mirror image ID
IMAGE_ID=`docker images | grep $IMG_NAME | awk '{ print $3 }'`
# Determine if there is a delete development container
if [[ "$CONTAINER_ID" != "" ]]; then
docker rm -f $CONTAINER_ID
fi
# Determine whether there is a delete development image
if [[ "$IMAGE_ID" != "" ]]; then
docker rmi -f $IMAGE_ID
fi
# $IMG_NAME:$IMG_VERSION This IMG_VERSION edition (tag) Parameter does not specify the default latest, Different environment files are executed by different parameters
# -f It means mandatory designation Dockerfile File for compilation
echo " ....... Get into Building & Images operation ....... "
# Method 1、 Specify different files to store the default Dockerfile, Use -f Force compilation
#docker build -t $IMG_NAME:$IMG_VERSION -f $IMG_PATH"env/"$RUN_EVN/Dockerfile $IMG_PATH
# Method 2、 It's different from it Dockerfile File suffix to compile files in different environments
docker build -t $IMG_NAME:$IMG_VERSION -f $IMG_PATH"env/"Dockerfile_$RUN_EVN $IMG_PATH
# Label the image , Then the label is pushed to the private service , The name of the tag is the name of the service
docker tag $IMG_NAME:$IMG_VERSION $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
# Push the image into the private clothes
docker push $REGISTRY_URL/$IMG_NAME:$IMG_VERSION
# Determine if there is a folder
if [ -d "$IMG_PATH" ];then
echo " Already exist :"$IMG_PATH
else
mkdir -p $IMG_PATH
fi
# Save the compiled image file to the specified path
docker save $IMG_NAME -o $IMG_TAR_GZ_PATH/$IMG_NAME.tar.gz
echo " ....... Get into Runing operation ....."
docker run -d --network default_network --restart=always --env-file=./.env -e spring.profiles.active=$RUN_EVN --expose=$IMG_PORT --name=$IMG_NAME -p $IMG_PORT:$IMG_PORT $IMG_NAME:$IMG_VERSION
echo " .......Build & Run Finish Success~...."
else
echo " .......Illegal Command Operation ......."
fi
4.2.6 Docker (save、load、tag、push,pull) Command to use
  • among push,pull One is push , One is pull , In a way , They all operate on the image on the private server
  • docker save The command is to save the compilation tar.gz or tar Compressed files , Grammar is like :
docker save Mirror name -o route / Mirror name .tar.gz

or

docker save Mirror name -o route / Mirror name .tar

docker load The command is used to import using docker save Command exported image , This command is very important , Because some customers require the project to be deployed in the intranet , At this point, this command shows its important position when deploying projects in Intranet without network . grammar docker load [OPTIONS], It's a little slow in the loading process , Because the file is a little big , Which shows Loady Layer [======] Output information , Confirm that the image is a hierarchical relationship .

docker load -i /home/img_tar_gz_path/springboot.tar.gz

  • Parameter description

  • docker tag and docker push Commands are used together , First tag after push, Each image name and version is distinguished by a colon , and docker pull Use... As appropriate .
# Label the image , Then the label is pushed to the private service , The name of the tag is the name of the service
docker tag Mirror name : Version number Private service path / Mirror name : Version number
# Push the image into the private clothes
docker push Private service path / Mirror name : Version number
  • Look at the mirror image

Browser validation docker push Push the mirror image of private service

4.2.7 A false image
  • stay docker If the compilation fails, the new version will cover the old version and be classified as a suspended image , There is no warehouse name to generate this image , And there's no label , Are all <none>. Generally speaking , The mirror image has lost its value of existence , It can be deleted at will .

4.2.8 Profiles for different environments

Configuration parameters

Different environment configuration parameter content

server:
port: 7011
runEvn: ' development environment '
server:
port: 7011
runEvn: ' Test environment '
4.2.9 Controller Test code
@RestController
public class SimpleController {
// Read configuration dynamic parameters
@Value("${runEvn}")
private String runEvn;
@GetMapping("/test")
public String test() {
return "this spring boot " + runEvn +" date long "
+ System.currentTimeMillis();
}
}

5、 Non multi machine remote login without password &Jenkins Deployment details

5.1 In particular

  • Take the development environment as an example
  • Development environment deployment target machine is with Jenkins The same machine , General situation ,Jenkins It's a single machine , Here, in order to save your own computer memory , So put it on the same machine for demonstration and learning .

5.2 newly build maven engineering

  • Click on Jenkins New task menu for

5.3 Parametric construction process description

5.3.1 Add parameter

5.3.2 The parameter description takes the development environment as an example

5.3.3 Source code management

5.3.4 Build Compile settings

Ignore test unit classes for compilation

clean install -U -Dmaven.test.skip=true

5.4 SSH Publishers Set up

  • among SSH Server Name Is in the http://jenkins Address : port /jenkins/configure Set up to choose
  • Transfer Set Source file The path to transfer the file , Placeholders that can be built with parameters ${serverPath} obtain
  • Remote directory Remote file directory , In the same way, it is also a place holder for parameter construction ${serverPath} obtain

  • SSH Publishers shell Script #!/bin/bash Tell the terminal to use bash The parser executes , And only the first line bash It works .
#!/bin/bash
# Create directory
mkdir -p ${serverPath}
# Toggle directory
cd ${serverPath}
# Run script
sh build.sh $server ${appName} ${version} ${port} ${env} ${serverPath}

5.5 Build and compile deployment projects

  • The construction parameters of the screenshot are the parameters configured in the parametric construction process

5.6 structure & function && Deploy the results

  • The build process

  • Deploy the results

5、 Multi machine remote login without password &Jenkins Deployment process details

6.1 In particular

  • Take the test environment as an example
  • The process is almost the same , The only thing is SSH Publishers It's not the same as the source storage path , The target machine of the test environment deployment is Jenkins A machine is different from a machine

6.2 newly build maven engineering

  • Click on Jenkins New task menu for

6.3 Parametric construction process description

6.3.1 Add parameter

6.3.2 The parameter description takes the test environment as an example

6.3.3 Source code management

6.3.4 Build Compile settings

6.4 SSH Publishers Set up

  1. among SSH Server Name Is in the http://jenkins Address : port /jenkins/configure Set up to choose
  2. Transfer Set Source file The path to transfer the file , Placeholders that can be built with parameters ${serverPath} obtain
  3. Remote directory Remote file directory , In the same way, it is also a place holder for parameter construction ${serverPath} obtain
  4. docker_server1 To express with Jenkins Deploy the same host , There are remote machines using scripts shell Script operation, no login operation .

  • docker_server1 Shell Script #!/bin/bash Tell the terminal to use bash The parser executes , And only the first line bash It works .
#!/bin/bash
# Print information
echo " user name ${userName}"
echo " The server ${server}"
echo " Server directory ${serverPath}"
# Create a directory path to store the code uploaded remotely
ssh $server mkdir -p ${targetServerPath}
# Remote copy code to the target machine to specify the path
scp -r ${serverPath}/ ${userName}@${server}:${targetServerPath}
  • docker_server2 To deploy the target machine , So its script is similar to docker_server1 Dissimilarity .

  • docker_server2 Shell Script
#!/bin/bash
# Switch file directory
cd ${serverPath}
# Execute the script
sh build.sh ${server} ${appName} ${version} ${port} ${env} ${serverPath}

6.5 Build and compile deployment projects

  • The construction parameters of the screenshot are the parameters configured in the parametric construction process

6.6 structure & function & Deploy the results

  • The build process

  • Deploy the results

7、 summary & Suggest & Study

7.1 Summary and suggestions

1、 This article is for reference only , If you need to learn systematically, you can learn according to your own information .

2、 The above problems are sorted out and summarized one by one according to the problems encountered in the actual learning process , In addition to technical problems, we can search a lot of online materials and organize and share them after learning .

3、 In the process of learning, I also encountered many difficulties and doubts , If there is a problem or a delay , I hope you can point out or put forward suggestions . I will take all kinds of good suggestions and correct ways to constantly improve the current situation , People need high quality food in the process of growth .

4、 When encountering problems, it is suggested to ask more 『 Google 、 Bing Ying 、stackoverflow、 Baidu 』 These gods .

5、 It is suggested that the official manual be more authoritative , Because with the development and iteration of Technology , Usually official documents are updated faster , Domestic websites update slowly .

6、 Computer is a science 『 Learning by doing 』 The subject of , I'm not going to do it again , It's about doing it . More practice , As the saying goes, practice makes perfect .

7、 What kind of technology should we learn 『 First Know how, Again Know Why』, It means getting started first , To get a HelloWorld, Let's go further .

8、 I hope this article can help you better understand what (DevOps) It's about automating image building and deployment , How to be in Docker+Jenkins+GitLab+Maven+SpringBoot&SpringCloud Automatically build image and deploy service application , There will be some twists and turns in the whole learning process and construction , It won't go so well , Also hope you read this article or find materials for personal experience, learning effect will be better .

版权声明
本文为[Java architecture_ Twitter]所创,转载请带上原文链接,感谢
https://javamana.com/2021/06/20210623153045502I.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云