当前位置: 首页 > news >正文

【Java】Devops、CI/CD、jenkins

https://heuqqdmbyk.feishu.cn/docx/ITjadnZfAoxauXxqgiccu9b6nGg
https://www.bilibili.com/video/BV1j8411N7Bm?spm_id_from=333.788.videopod.episodes&vd_source=6bec1f5b9fe3a91a9829e3a951692cc5&p=169

CI/CD 是近年来企业有效实施DevOps的具体方案。
CI/CD 包含了一个 CI 和两个 CD,CI全称 Continuous Integration,表示持续集成,CD包含 Continuous Delivery和 Continuous Deployment,分别是持续交付和持续部署,三者具有前后依赖关系。

在这里插入图片描述
CI/CD的技术方案
在这里插入图片描述

人工部署方式

项目打包
1、在父工程聚合各模块
首先在父工程添加models,聚合各各模块

<modules>
    <module>../xuecheng-plus-base</module>
    <module>../xuecheng-plus-checkcode</module>
    <module>../xuecheng-plus-gateway</module>
    <module>../xuecheng-plus-auth</module>
    <module>../xuecheng-plus-content</module>
    <module>../xuecheng-plus-learning</module>
    <module>../xuecheng-plus-media</module>
    <module>../xuecheng-plus-orders</module>
    <module>../xuecheng-plus-message-sdk</module>
    <module>../xuecheng-plus-search</module>
    <module>../xuecheng-plus-system</module>
</modules>

2、配置打包插件
使用springboot打包插件进行打包,在需要打可执行包的工程中配置spring-boot-maven-plugin插件

<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在父工程执行:clean install -DskipTests -f pom.xml 对所有工程进行打包
打包完成可在本地通过java -jar 运行jar包观察是否可以正常运行。

下边测试验证码服务:
进入验证码服务的target目录,cmd运行:
java -Dfile.encoding=utf-8 -jar xuecheng-plus-checkcode-0.0.1-SNAPSHOT.jar
使用httpclient测试申请验证码
POST localhost:63075/checkcode/pic

将打成的jar包拷贝到Linux,生成镜像,并创建容器。
1、编写Dockerfile文件

FROM java:8u20
MAINTAINER docker_maven docker_maven@email.com
WORKDIR /ROOT
ADD xuecheng-plus-checkcode-0.0.1-SNAPSHOT.jar xuecheng-plus-checkcode.jar
CMD ["java", "-version"]
ENTRYPOINT ["java", "-Dfile.encoding=utf-8","-Dfile.encoding=utf-8","-jar", "xuecheng-plus-checkcode.jar"]
EXPOSE 63075

2、上传jar包
rz

3、创建镜像
docker build -t checkcode:1.0 .
.表示dockerfile所在目录
docker images查询镜像

4、创建容器
docker run --name xuecheng-plus-checkcode -p 63075:63075 -idt checkcode:1.0

5、再次测试
POST 192.168.101.65:63075/checkcode/pic

6、查看日志
docker logs -f xuecheng-plus-checkcode

自动部署

1、将代码 使用Git托管
2、在jenkins创建任务,从Git拉取代码。
3、拉取代码后进行自动构建:测试、打包、部署。
首先将代码打成镜像包上传到docker私服。
自动创建容器、启动容器。
4、当有代码push到git实现自动构建。

在pom.xml添加docker-maven-plugin插件实现将springboot工程创建镜像

<dependency>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
</dependency>

具体如下:

<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.2.2</version>
            <configuration>
                <!--修改imageName节点的内容,改为私有仓库地址和端口,再加上镜像id和 TAG,我们要直接传到私服-->
                <!--配置最后生成的镜像名,docker images里的,我们这边取项目名:版本-->
                <!--<imageName>${project.artifactId}:${project.version}</imageName>-->
                <imageName>192.168.101.65:5000/${project.artifactId}:${project.version}</imageName>
                <!--也可以通过以下方式定义image的tag信息。 -->
                <!-- <imageTags>
                     <imageTag>${project.version}</imageTag>
                     &lt;!&ndash;build 时强制覆盖 tag,配合 imageTags 使用&ndash;&gt;
                     <forceTags>true</forceTags>
                     &lt;!&ndash;build 完成后,push 指定 tag 的镜像,配合 imageTags 使用&ndash;&gt;
                     <pushImageTag>true</pushImageTag>
                 </imageTags>-->
                <baseImage>java:8u20</baseImage>
                <maintainer>docker_maven docker_maven@email.com</maintainer>
                <workdir>/root</workdir>
                <cmd>["java", "-version"]</cmd>
                <!--来指明Dockerfile文件的所在目录,如果配置了dockerDirectory则忽略baseImage,maintainer等配置-->
                <!--<dockerDirectory>./</dockerDirectory>-->
                <!--2375是docker的远程端口,插件生成镜像时连接docker,这里需要指定docker远程端口-->
                <dockerHost>http://192.168.101.65:2375</dockerHost>
                <!--入口点,project.build.finalName就是project标签下的build标签下 的filename标签内容,testDocker-->
                <!--相当于启动容器后,会自动执行java -jar ...-->
                <entryPoint>["java", "-Dfile.encoding=utf-8","-jar", "/root/${project.build.finalName}.jar"]</entryPoint>
                <!--是否推送到docker私有仓库,旧版本插件要配置maven的settings文件。 -->
                <pushImage>true</pushImage>
                <registryUrl>192.168.101.65:5000</registryUrl>  <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                <resources>
                    <resource>
                        <targetPath>/root</targetPath>
                        <directory>${project.build.directory}</directory>
                        <!--把哪个文件上传到docker,相当于Dockerfile里的add app.jar /-->
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>

……
https://heuqqdmbyk.feishu.cn/docx/ITjadnZfAoxauXxqgiccu9b6nGg

相关文章:

  • Linux系统之美:进程初识
  • C语言每日一练——day_8
  • Qt常用控件之网格布局QGridLayout
  • 2025-03-15 学习记录--C/C++-C语言 char* price; 和 char price;的区别
  • 背诵--1
  • 【NLP】7. 自然语言处理 (NLP) 的关键要素
  • 零基础上手Python数据分析 (2):Python核心语法快速入门
  • golang从入门到做牛马:第二十篇-Go语言接口:行为的“契约”
  • 深入探究 HTML 框架:多页面同窗口显示的奥秘
  • 【红黑树】—— 我与C++的不解之缘(二十五)
  • 软件环境安装-通过Docker安装Elasticsearch和Kibana【保姆级教程、内含图解】
  • SpringBoot配置文件
  • 【Docker compose】Neo4j 数据备份与恢复
  • 插入排序c++
  • 【BP神经网络】实战
  • PHP语言的区块链扩展性
  • 大模型后训练+微调
  • MAC地址IP地址如何转换?
  • LuaJIT 学习(5)—— string.buffer 库
  • 梧桐:开发者的命令行效率应用
  • 网信部门曝光网络谣言典型案例,“AI预测彩票号码百分百中奖”等在列
  • 著名文物鉴赏家吴荣光逝世,享年78岁
  • 重庆大学:对学术不端行为“零容忍”,发现一例、查处一例
  • 被取消总统候选人资格,金文洙:将采取政治法律措施讨回公道
  • 毕赣新作《狂野时代》入围戛纳主竞赛单元,易烊千玺舒淇主演
  • 复旦发文缅怀文科杰出教授裘锡圭:曾提出治学需具备三种精神