以前,咱们公司部署以及升级都是由运维去管理的,联想到不少开源平台都支持自动升级,索性我也作个自动升级war的功能。
这里没有用docker镜像发包,灰度发包等,只适用于单个tomcat的部署环境,支持docker单个tomcat容器。java
先简单分析下war包自动升级流程:linux
1,2步骤中没有什么坑,主要是3,4,5步骤,若是用java代码去执行,当tomcat服务关闭时,war包内的代码将中止,因此除非单独写个java程序跑才能继续执行下面代码。但又以为这种方式麻烦,对环境依赖过高,最终采用shell脚本去执行3,4,5步骤,而java-web去调用这个shell脚本便可。web
这一步比较简单,这里直接发送一个请求,带上版本号,如:
a.com/checkVersio…
返回是否须要更新,以及更新地址:sql
{
"NeedUpdate": true,
"downUrl": "http://a.com/1.0.war"
}复制代码
这里使用httpclient去调用接口:docker
/*使用时注意字符集 "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解析出内容便可。shell
java的下载文件方法有不少种,都是以流的形式写,代码量比较多,若是项目里有框架的话,直接用就能够了,没有的话,网上找一个。json
/**
* 保存附件
* 一、保存附件信息到附件表
* 二、保存文件到相应的目录
*
* @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;
}复制代码
runBatOrShell(packagename, System.getProperties().getProperty("os.name").indexOf("Windows") != -1)复制代码
这里须要判断Windows仍是linux,true为Windows,不然为linux。引入包名参数是为了获得sh文件及bat文件。tomcat
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/
文件目录以下:
bash
#!/bin/sh
cd $1
echo $(date +%Y-%m-%d-%l:%M:%S) >>webvlog.txt;
echo $2>>webvlog.txt;
echo "正在关闭tomcat">>webvlog.txt;
sh $3/bin/shutdown.sh
echo "正在执行删除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.war
echo "正在重启tomcat">>webvlog.txt;
sh $3/bin/startup.sh
echo "部署成功">>webvlog.txt;复制代码
其中$1 $2 $3 这种变量标示运行时后面的传值,其它代码echo就是注释。webvlog.txt为部署日志。服务器
这样改造后,基本知足目前现有需求,也简化了部署步骤。但没法适用多tomcat环境中,不少场景有load balance负载均衡、有多个docker环境,此时,该方案便没法解决。
war包在升级时,可能会增长表、视图等,因此还须要执行sql脚本,sql脚本升级方案下次分享。