性能环境之Jenkins+Maven自动化部署SpringBoot压测环境(Docker篇)

举报
zuozewei 发表于 2021/09/27 22:10:56 2021/09/27
【摘要】 如何使用Jenkins+Maven+Docker自动化部署我们的压测环境?

一、前言

在上文性能环境之Jenkins+Maven自动化部署SpringBoot压测环境(实战篇)中我们介绍了常规部署流程,本文将在上文的基础上扩展Jenkins+Maven+Docker自动化部署我们的压测环境。

关于Docker

Docker在这里有什么用?

Docker,是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
Docker是基础架构的管理员。它将能够打包软件及其所有依赖项以作为容器运行。您可以将应用(作为docker镜像打包)部署到安装了docker的任何计算机中。它将软件与硬件分开 - 因此开发人员可以放心,应用程序将在任何计算机上运行,​​而不管机器可能具有哪些可能与用于编写和测试代码的机器不同的自定义设置。

题外话:本人觉得,每个Tester都应该了解下Docker,毕竟我们不是专业的运维,如果花费大量时间在倒腾一些环境的安装,也是蛮枯燥乏味的。使用了Docker可以减少很多的时间,可以直接上手部署各式各样的环境了。真的很Nice呀!

整体设计

在这里插入图片描述

新建Jenkins Job

我们在上文常规部署Job基础上复制一个新的job

在这里插入图片描述

配置Job

Post Steps

Send files or execute commands over SSH中的Exec command修改为jenkins-in/buildimage.sh
在这里插入图片描述

新建buildimage脚本

测试应用服务上/root/jenkins-in目录新建buildimage.sh

# 设置系统时间
DATE=$(date +%Y%m%d)
# 设置程序目录
DIR=/usr/local/app
# 设置Jar名称
JARFILE=person-0.0.1-SNAPSHOT.jar
# 设置Docker镜像&容器名称
SERVER_NAME=springboot-docker-demo
# 容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
# 镜像id
IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')

# 判断是否存在backp目录,如果不存储就创建
if [ ! -d $DIR/backup ];then
   mkdir -p $DIR/backup
fi
cd $DIR

# 杀掉当前的容器及镜像
docker stop $CID
docker rm $CID
docker rmi $IID

# 备份旧程序
mv $JARFILE $DIR/backup/$JARFILE$DATE
# 部署新程序
mv -f /root/jenkins-in/$JARFILE .
# build镜像
docker build -t $SERVER_NAME .

echo "The service will be starting"
# 运行容器
docker run --name $SERVER_NAME -d -p 8081:8081 $SERVER_NAME

cd backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf
echo "starting success!!!"
~                                

创建Dockerfile

测试应用服务器/usr/local/app下创建Dockerfile

# Dockerfile
# 基于的镜像
FROM frolvlad/alpine-oraclejdk8

# 数据挂载点
VOLUME /usr/local/app/logs

# 指定路径下的copy内容到容器中路径下
ADD person-0.0.1-SNAPSHOT.jar /usr/local/app/app.jar

# 指定镜像的默认入口
# -Djava.security.egd=file:/dev/./urandom 可解决tomcat可能启动慢的问题
ENTRYPOINT java ${JAVA_OPTS} ${JVM_GC_LOG_PATH} -Djava.security.egd=file:/dev/./urandom -jar /usr/local/app/app.jar

# 对外端口
EXPOSE 8081

# 工作目录
WORKDIR /usr/local/app

# JVM参数
ARG JAVA_OPTS="-server -Xms1024M -Xmx1024M -XX:PermSize=256M -XX:MaxPermSize=256M \
-XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails \
-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=127.0.0.1 \
-Dcom.sun.management.jmxremote.port=10086 -Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false"

# log路径
ARG JVM_GC_LOG_PATH="-XX:ErrorFile=/usr/local/app/logs/gc/hs_err_pid%p.log -XX:HeapDumpPath=/usr/local/app/logs/gc"

# JVM环境变量
ENV JAVA_OPTS=${JAVA_OPTS}
ENV JVM_GC_LOG_PATH=${JVM_GC_LOG_PATH}

部署项目

点击“立即构建”,任务控制台将实时输出相关信息
在这里插入图片描述
我们看到输出了starting success!!!,说明我们需要的应用容器已经跑起来了!

我们可以去测试应用服务器验证下

[root@zuozewei app]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
springboot-docker-demo                 latest              c1916f7c7aac        46 minutes ago      197 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              b81355b10fa3        6 days ago          168 MB
quay.io/influxdb/influxdb              nightly             4fb0d5f41632        7 weeks ago         262 MB
docker.io/influxdb                     latest              dff45714308e        7 weeks ago         213 MB
docker.io/mongo                        latest              a41c82c0998a        2 months ago        380 MB
docker.io/mysql                        5.7                 43b029b6b640        2 months ago        372 MB
docker.io/redis                        latest              4e8db158f18d        2 months ago        83.4 MB
docker.io/nginx                        latest              c82521676580        3 months ago        109 MB
docker.io/jenkins                      latest              cd14cecfdb3a        3 months ago        696 MB
docker.io/redis                        3.2                 e2e6164a20de        3 months ago        76 MB
docker.io/hello-world                  latest              2cb0d9787c4d        3 months ago        1.85 kB
docker.io/primetoninc/jdk              1.7                 2b2bf2dcec72        6 months ago        589 MB
docker.io/primetoninc/jdk              latest              4f4be6992080        9 months ago        879 MB
docker.io/primetoninc/jdk              1.8                 f4b4fccc65bb        9 months ago        657 MB
docker.io/ubuntu                       15.10               9b9cb95443b5        2 years ago         137 MB
[root@zuozewei app]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                              NAMES
ef0c317eb320        springboot-docker-demo   "/bin/sh -c 'java ..."   46 minutes ago      Up 46 minutes       0.0.0.0:8081->8081/tcp                             springboot-docker-demo
9f5891f311b9        docker.io/mongo          "docker-entrypoint..."   8 weeks ago         Up 15 hours         0.0.0.0:27017->27017/tcp                           mongo
0e1e223051d1        jenkins                  "/bin/tini -- /usr..."   2 months ago        Up 15 hours         0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   myjenkins
c24e1ac84a60        mysql:5.7                "docker-entrypoint..."   2 months ago        Up 15 hours         0.0.0.0:3306->3306/tcp, 33060/tcp                  mymysql
[root@zuozewei app]# docker logs ef0c317eb320
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=256M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)
2018-10-29 08:27:52.388  INFO 1 --- [           main] com.techstar.person.PersonApplication    : Starting PersonApplication v0.0.1-SNAPSHOT on ef0c317eb320 with PID 1 (/usr/local/app/app.jar started by root in /usr/local/app)
2018-10-29 08:27:52.413  INFO 1 --- [           main] com.techstar.person.PersonApplication    : The following profiles are active: a
2018-10-29 08:27:52.633  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5f150435: startup date [Mon Oct 29 08:27:52 GMT 2018]; root of context hierarchy
[Full GC (Metadata GC Threshold) [Tenured: 0K->16498K(699072K), 0.1457872 secs] 184618K->16498K(1013632K), [Metaspace: 20819K->20819K(1069056K)], 0.1458659 secs] [Times: user=0.13 sys=0.01, real=0.14 secs] 
2018-10-29 08:27:57.331  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081 (http)
2018-10-29 08:27:57.365  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2018-10-29 08:27:57.382  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.14
2018-10-29 08:27:57.599  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/springboot]    : Initializing Spring embedded WebApplicationContext
2018-10-29 08:27:57.600  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4984 ms
2018-10-29 08:27:57.993  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-10-29 08:27:58.012  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-10-29 08:27:58.012  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-10-29 08:27:58.012  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-10-29 08:27:58.012  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
[GC (Allocation Failure) [DefNew: 279616K->13447K(314560K), 0.0690457 secs] 296114K->29946K(1013632K), 0.0690980 secs] [Times: user=0.06 sys=0.01, real=0.07 secs] 
2018-10-29 08:28:00.486  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-10-29 08:28:00.526  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
	name: default
	...]
2018-10-29 08:28:00.672  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2018-10-29 08:28:00.676  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-10-29 08:28:00.684  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
[Full GC (Metadata GC Threshold) [Tenured: 16498K->29548K(699072K), 0.1120392 secs] 152567K->29548K(1013632K), [Metaspace: 34944K->34944K(1081344K)], 0.1121033 secs] [Times: user=0.10 sys=0.01, real=0.11 secs] 
2018-10-29 08:28:00.915  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-10-29 08:28:01.318  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-10-29 08:28:02.157  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
[GC (Allocation Failure) [DefNew: 279616K->10432K(314560K), 0.0668048 secs] 309164K->39980K(1013632K), 0.0668578 secs] [Times: user=0.06 sys=0.00, real=0.07 secs] 
2018-10-29 08:28:03.692  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5f150435: startup date [Mon Oct 29 08:27:52 GMT 2018]; root of context hierarchy
2018-10-29 08:28:03.932  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/age/{age}],methods=[GET]}" onto public java.util.List<com.techstar.person.entity.Person> com.techstar.person.controller.PersonController.personListByAge(java.lang.Integer)
2018-10-29 08:28:03.943  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/two],methods=[POST]}" onto public void com.techstar.person.controller.PersonController.personTwo()
2018-10-29 08:28:03.944  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/{id}],methods=[DELETE]}" onto public void com.techstar.person.controller.PersonController.personDelete(java.lang.Integer)
2018-10-29 08:28:03.944  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person/find/{id}],methods=[GET]}" onto public com.techstar.person.entity.Person com.techstar.person.controller.PersonController.personFindOne(java.lang.Integer)
2018-10-29 08:28:03.947  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/persion/{id}],methods=[PUT]}" onto public com.techstar.person.entity.Person com.techstar.person.controller.PersonController.personUpdate(java.lang.Integer,java.lang.String,java.lang.Integer)
2018-10-29 08:28:03.947  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person],methods=[POST]}" onto public com.techstar.person.entity.Result com.techstar.person.controller.PersonController.personAdd(com.techstar.person.entity.Person,org.springframework.validation.BindingResult)
2018-10-29 08:28:03.947  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person],methods=[GET]}" onto private java.util.List<com.techstar.person.entity.Person> com.techstar.person.controller.PersonController.personlist()
2018-10-29 08:28:03.958  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-29 08:28:03.959  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-29 08:28:04.068  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-29 08:28:04.068  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-29 08:28:04.120  INFO 1 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in exceptionHandle
2018-10-29 08:28:04.176  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-29 08:28:04.738  INFO 1 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-29 08:28:04.995  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2018-10-29 08:28:05.008  INFO 1 --- [           main] com.techstar.person.PersonApplication    : Started PersonApplication in 13.941 seconds (JVM running for 15.278)
PersonApplication.main

我们可以看到springboot-docker-demo镜像已经成功运行起来

测试验证

此处,我们还是使用Postman验证应用查询人员接口
在这里插入图片描述
我们可以看到应用成功返回了JSON数据,测试成功。

相关源码:

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。