- 一,了解如何构建java的项目
- git clone
git@gitlab.oldxu.com
:dev/hello-word-demo.git
[root rel="nofollow" @jenkins ~]#cd hello-word-demo/ #进入java项目目录准备mavn
">随便找个终端把代码clone下来
[root rel="nofollow" @jenkins ~]#git clone
git@gitlab.oldxu.com
:dev/hello-word-demo.git
[root rel="nofollow" @jenkins ~]#cd hello-word-demo/ #进入java项目目录准备mavn
- @jenkins hello-word-demo]#
yum install maven -y
#也可以源码安装编译工具
[root@jenkins hello-word-demo]#mvn clean ``packag ``#先清理在打包
">打包
[root@jenkins hello-word-demo]#yum install maven -y
#也可以源码安装编译工具
[root@jenkins hello-word-demo]#mvn clean ``packag ``#先清理在打包
- 加速yum工具mavenan安装时的下载地址
- ssh
root@172.16.1.7
"rm -rf
/soft/tomcat/webapps/ROOT"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.8
"rm -rf
/soft/tomcat/webapps/ROOT"
">清理站点⽬录
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.7
"rm -rf
/soft/tomcat/webapps/ROOT"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.8
"rm -rf
/soft/tomcat/webapps/ROOT"
- scp target/*.war
root@172.16.1.7
:/soft/tomcat/webapps/ROOT.war
[root rel="nofollow" @jenkins hello-word-demo]#scp target/*.war
root@172.16.1.8
:/soft/tomcat/webapps/ROOT.war
">推送war包⾄站点⽬录,并改名为ROOT.war
[root rel="nofollow" @jenkins hello-word-demo]#scp target/*.war
root@172.16.1.7
:/soft/tomcat/webapps/ROOT.war
[root rel="nofollow" @jenkins hello-word-demo]#scp target/*.war
root@172.16.1.8
:/soft/tomcat/webapps/ROOT.war
- ssh
root@172.16.1.8
"pkill java"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.7
"pkill java"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.8
"/soft/tomcat/bin/startup.sh"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.7
"/soft/tomcat/bin/startup.sh"
">重启tomcat
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.8
"pkill java"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.7
"pkill java"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.8
"/soft/tomcat/bin/startup.sh"
[root rel="nofollow" @jenkins hello-word-demo]#ssh
root@172.16.1.7
"/soft/tomcat/bin/startup.sh"
一,了解如何构建java的项目
(nginx+tomcat的部署)
freestyle的方式做不到部署java项目,涉及到java编译需要引入mvn项目
1.手动部署java流程
首先:实现自动化运维之前,我们需要手动部署java项目, (java编写的代码必须使用maven工具进行打包才能在服务器运行)
0-搭建java环境:
- tomcat集群配置(请关闭web集群上的nginx)
[root@web01 /]# yum install java -y
[root@web01 /]# wget [https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-](https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-)9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz
[root@web01 /]# mkdir /soft
[root@web01 /]# tar xf apache-tomcat-9.0.36.tar.gz -C /soft
[root@web01 /]# ln -s /soft/apache-tomcat-9.0.36/ /soft/tomcat
- 负载均衡配置
[root@lb01 ~]# cat /etc/nginx/conf.d/tomcat.oldxu.com.conf
upstream java {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name tomcat.oldxu.com;
location / {
proxy_pass http://java;
include proxy_params;
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
- 配置windows的hosts劫持
10.0.0.5 tomcat.oldxu.com
②开发角色:提交代码:
1.1 在gitlab上创建⼀个java的仓库
1.2 模拟开发本地提交代码⾄仓库中
xudeMacBook-Pro:hello-world-war xuliangwei$ git remote remove origin
xudeMacBook-Pro:hello-world-war xuliangwei$ git add .
xudeMacBook-Pro:hello-world-war xuliangwei$ git commit -m "java"
[master 35a999d] java
1 file changed, 2 insertions(+), 2 deletions(-)
xudeMacBook-Pro:hello-world-war xuliangwei$ git remote add origin
git@gitlab.oldxu.com:dev/hello-word-demo.git
xudeMacBook-Pro:hello-world-war xuliangwei$ git push origin master
③运维角色:手动发布代码:
2.1 第⼀次需要先克隆项⽬(后期直接git pull就⾏) 2.2 安装maven⼯具、对源码包进⾏编译 2.3 通过scp将war包推送到tomcat指定的站点⽬录 2.4 重启集群的tomcat
随便找个终端把代码clone下来
[root@jenkins ~]# git clone
git@gitlab.oldxu.com
:dev/hello-word-demo.git
[root@jenkins ~]# cd hello-word-demo/ #进入java项目目录准备mavn
打包
[root@jenkins hello-word-demo]# yum install maven -y
#也可以源码安装编译工具
[root@jenkins hello-word-demo]# mvn clean ``packag ``#先清理在打包
加速yum工具mavenan安装时的下载地址
[root> @jenkins hello-word-demo]# >
vim /etc/maven/settings.xml
> AliMaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
清理站点⽬录
[root@jenkins hello-word-demo]# ssh
root@172.16.1.7
"rm -rf
/soft/tomcat/webapps/ROOT"
[root@jenkins hello-word-demo]# ssh
root@172.16.1.8
"rm -rf
/soft/tomcat/webapps/ROOT"
推送war包⾄站点⽬录,并改名为ROOT.war
[root@jenkins hello-word-demo]# scp target/*.war
root@172.16.1.7
:/soft/tomcat/webapps/ROOT.war
[root@jenkins hello-word-demo]# scp target/*.war
root@172.16.1.8
:/soft/tomcat/webapps/ROOT.war
重启tomcat
[root@jenkins hello-word-demo]# ssh
root@172.16.1.8
"pkill java"
[root@jenkins hello-word-demo]# ssh
root@172.16.1.7
"pkill java"
[root@jenkins hello-word-demo]# ssh
root@172.16.1.8
"/soft/tomcat/bin/startup.sh"
[root@jenkins hello-word-demo]# ssh
root@172.16.1.7
"/soft/tomcat/bin/startup.sh"
2. 实现java自动化发布:
jenkins自动化发布: (jenkins能拉取代码—》 jenkins可以调用maven编译项目—-》Shell脚本拿到war包,发布到web集群 )
创建⼀个项⽬,选择maven的⽅式
点击Buil时会提示填写 (Maven Version :让jenkins知道maven安装在那个⽬录,调用编译java项目)
你完成以下操作:
配置jenkins,这样jenkins就可以调⽤maven编译项⽬了
[root@jenkins target]# mvn --version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_252, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: “linux”, version: “3.10.0-1062.18.1.el7.x86_64”, arch: “amd64”,
family: “unix”
[root@jenkins target]# cat /scripts/java_deploy.sh
#!/usr/bin/bashDate=$(date +%F-%H-%M)
Web_Server="172.16.1.7 172.16.1.8"
Web_Name=ROOT-${Date}
Web_Dir=/opt
cd ${WORKSPACE} && \
for ip in ${Web_Server}
do
scp -rp target/*.war root@$ip:${Web_Dir}/${Web_Name}.war
ssh root@${ip} "cd ${Web_Dir} && \
mkdir -p ${Web_Name} && \
unzip ${Web_Name}.war -d ${Web_Name}
rm -f ${Web_Name}.war && \
rm -rf /soft/tomcat/webapps/ROOT && \
ln -s ${Web_Dir}/${Web_Name} /soft/tomcat/webapps/ROOT && \
pkill java && \
/soft/tomcat/bin/startup.sh"
done
webhook配置全自动上线自动代码:
http://jenkins.oldhou.com:8080/project/mvnpeoject
da464a5d9019764863d56da37b131f3f
3. 实现java参数化构建(半自动)
参数化构建:
拉取指定版本:
打包:
添加脚本,并编写:
修改一下脚本变量:
效果 :
java参数化构建(退回)
参数化构建
cat /scripts/html_deploy_rollback_java.sh
#!/usr/bin/bash
#0.变量
Tomcat_dir=/soft/tomcat/webapps/ROOT
Date=$(date +%F-%H-%M)
Web_server="172.16.1.7 172.16.1.8"
Web_dir=/opt
for ip in ${Web_server}
do
ssh root@${ip} "[ -d ${Web_dir}/ROOT-*-${git_version} ] && \
rm -rf ${Tomcat_dir} && \
ln -s ${Web_dir}/ROOT-*-${git_version} ${Tomecat_dir}
pkill java && \
/soft/tomcat/bin/startup.sh"
done
效果: