java web项目war包自动升级部署方案

来源:互联网 发布:js promise的用法总结 编辑:程序博客网 时间:2024/05/16 11:29

前言

之前,我们公司部署以及升级都是由运维去管理的,联想到很多开源平台都支持自动升级,索性我也做个自动升级war的功能。
这里没有用docker镜像发包,灰度发包等,只适用于单个tomcat的部署环境,支持docker单个tomcat容器。

分析

先简单分析下war包自动升级流程:
1. 检查是否需要更新。
2. 下载更新的war包到服务器临时目录。(如后台上传则无需1,2步骤)
3. 停止tomcat
4. 清理tomcat下,webapps的war包解压目录、war包。
5. 启动tomcat

1,2步骤中没有什么坑,主要是3,4,5步骤,如果用java代码去执行,当tomcat服务关闭时,war包内的代码将停止,所以除非单独写个java程序跑才能继续执行下面代码。但又觉得这种方式麻烦,对环境依赖太高,最终采用shell脚本去执行3,4,5步骤,而java-web去调用这个shell脚本即可。

实施

检查更新

这一步比较简单,这里直接发送一个请求,带上版本号,如:
http://a.com/checkVersion?v=1.0
返回是否需要更新,以及更新地址:

{    "NeedUpdate": true,    "downUrl": "http://a.com/1.0.war"}

这里使用httpclient去调用接口:

/*使用时注意字符集 "GBK""UTF-8"*/public static String visitPost(String urlStr, String code) {        try{            URL url = new URL(urlStr);            HttpURLConnection con = (HttpURLConnection)url.openConnection();            con.setRequestMethod("GET");            con.connect();            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(),code));));            String line;            StringBuffer buffer = new StringBuffer();            while((line = reader.readLine()) != null) {                buffer.append(line);            }            reader.close();            con.disconnect();            String res = buffer.toString();            return res;        } catch(Exception e) {            e.printStackTrace();        }        return null;    }

类似方法有很多种,这里不举例。下载之后使用fastjson或者gson或者纯string解析出内容即可。

下载文件

java的下载文件方法有很多种,都是以流的形式写,代码量比较多,如果项目里有框架的话,直接用就可以了,没有的话,网上找一个。

    /**     * 保存附件     * 1、保存附件信息到附件表     * 2、保存文件到相应的目录     *     * @param file 文件实体     */    public boolean saveFile(FileEntity file) {        BufferedOutputStream bos = null;        FileOutputStream fos = null;        byte[] bytes = file.getBytes();        if (bytes != null) {            try {                fos = new FileOutputStream(file_Path + file.getName());                bos = new BufferedOutputStream(fos);                bos.write(bytes);                bos.flush();                IOUtils.closeQuietly(fos);                IOUtils.closeQuietly(bos);                return true;            } catch (Exception e) {                IOUtils.closeQuietly(fos);                IOUtils.closeQuietly(bos);                Log.error("保存文件失败", e);                return false;            }        }        return false;    }

启动shell脚本

runBatOrShell(packagename, System.getProperties().getProperty("os.name").indexOf("Windows") != -1)

这里需要判断Windows还是linux,true为Windows,否则为linux。引入包名参数是为了得到sh文件及bat文件。

    private String tomcat_Path = System.getProperty("catalina.base") + File.separator;//服务器路径    /**     * 执行方法脚本     * @param 路径     * @param 系统      */    public boolean runBatOrShell( String name, boolean os) {        Log.info("runBatOrShell   start:>>>>>>>>>>>>>>>>>>>>");        String _path;        try {            _path = os ? this.getClass().getResource("/batshell/web.bat").getPath() + " " + file_Path + " "+ name + " " + tomcat_Path : "sh " + this.getClass().getResource("/batshell/web.sh").getPath() + " " + file_Path + " "+ name + " " + tomcat_Path;            Log.info(_path);            Process ps = Runtime.getRuntime().exec(_path);            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(), "UTF-8"));//注意中文编码问题            String line;            while ((line = br.readLine()) != null) {                Log.info("runBatOrShell info=========>" + line);            }            br.close();        } catch (IOException ioe) {            Log.error("runBatOrShell error !!!!!!!!!!");            ioe.printStackTrace();            return false;        }        return true;    }

这里引入tomcat路径是为了方便脚本执行。
sh web.sh空格war文件目录空格文件名空格tomcat目录
sh web.sh /usr/local/war/ 1.0.war /usr/local/tomcat/
bat文件同理
web.bat d:/war/ 1.0.war d:/tomcat/
文件目录如下:

shell脚本执行

#!/bin/shcd $1echo $(date +%Y-%m-%d-%l:%M:%S) >>webvlog.txt;echo $2>>webvlog.txt;echo "正在关闭tomcat">>webvlog.txt;sh $3/bin/shutdown.shecho "正在执行删除war.war">>webvlog.txt;rm $3/webapps/war.war;echo "正在执行删除war文件夹">>webvlog.txt;rm -r $3/webapps/war;echo "正在部署war">>webvlog.txt;cp  $1$2 $3/webapps/war.warecho "正在重启tomcat">>webvlog.txt;sh $3/bin/startup.shecho "部署成功">>webvlog.txt;

其中12 $3 这种变量标示运行时后面的传值,其它代码echo就是注释。webvlog.txt为部署日志。

总结

这样改造后,基本满足目前现有需求,也简化了部署步骤。但无法适用多tomcat环境中,很多场景有load balance负载均衡、有多个docker环境,此时,该方案便无法解决。

遗留问题

war包在升级时,可能会增加表、视图等,所以还需要执行sql脚本,sql脚本升级方案下次分享。
觉得好的话,记得关注我哦!
csdn博客:
http://blog.csdn.net/qq273681448/
掘金:
https://juejin.im/user/57cd55218ac247006459c40c
Github:
https://github.com/qq273681448

原创粉丝点击