因为公司业务的需求想实现一键分发程序,更新所有门店以及店铺运行的程序jar包版本,根据技术探讨,所以选型 rocket MQ 消息中间件 + linux 命令来实现这个需求。
Rocket MQ 消息中间件
-
rocket mq 实现提供者和消费者可以查看下面的代码
https://gitee.com/adorexm/adore-rocket-mq
注意: 上面的链接代码实现了rocketmq的单机版本,未拆分出提供者和消费者的jar包。
rocket mq的安装参看下面链接
https://www.jianshu.com/p/d1481c00ce07
选择rocket mq的原因:
(1)支持顺序消费
(2)支持分布式事务
程序发布的原理
简单的理解就是:使用rocket mq的消息订阅和分发,来实现一对多的消费。并根据java对linux命令的集成。
实现的简单代码如下:
package com.uhi.publish.service;
import com.alibaba.fastjson.JSONObject;
import com.Adore.publish.entity.TAdorePublishServiceManage;
import com.Adore.publish.utils.DateUtils;
import com.Adore.rocket.consumer.mq.entity.AdoreMQBaseMsg;
import com.Adore.rocket.consumer.mq.service.AdoreMQConsumerService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* @Author: AdoreFT
* @License: (C) Copyright 2017-2022, AdoreXM Corporation Limited.
* @Date: 2021/6/30 11:21
* @Version: 1.0
* @Description: AdorePublishServiceImpl
**/
@Service
@Log4j2
public class AdorePublishServiceImpl implements AdoreMQConsumerService {
@Override
public void consumerMessage(String message, String messageId) throws Exception {
if (StringUtils.isBlank(message)) {
log.error(String.format("消息消费程序为空,messageId:%s", messageId));
return;
}
//获取message
AdoreMQBaseMsg mqBaseMsg = JSONObject.parseObject(message, AdoreMQBaseMsg.class);
//转换message
TAdorePublishServiceManage publishServiceManage = JSONObject.parseObject(mqBaseMsg.getMessage(), TAdorePublishServiceManage.class);
if (StringUtils.isBlank(publishServiceManage.getOssurl())) {
log.error(String.format("消息消费程序ossUrl为空,message:%s,messageId:%s", message, messageId));
return;
}
if (StringUtils.isBlank(publishServiceManage.getJarUrl())) {
log.error(String.format("消息消费程序JarUrl为空,message:%s,messageId:%s", message, messageId));
return;
}
try {
log.info(String.format("开始执行程序,message:%s,messageId:%s", message, messageId));
/**
* 开始操作数据并下载发布程序
* cp /usr/local/posService/Adore-pos-service-1.0.0-SNAPSHOT.jar /usr/local/posService/backJar/Adore-pos-service-1.0.2-SNAPSHOT.jar
* ps -ef | grep Adore-pos-service-1.0.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9
* kill -9 `ps -ef | grep Adore-pos-service-1.0.0-SNAPSHOT.jar | grep -v grep | awk '{print $2}'`
* */
//1.备份原来的jar包
String cpJar = "cp " + publishServiceManage.getJarUrl() + "/" + publishServiceManage.getJarName() + " " + publishServiceManage.getJarUrl() + "/backJar/" + DateUtils.dateTimeNow() + "-" + publishServiceManage.getJarName();
//2.进入目录
String cdJarUrl = "cd " + publishServiceManage.getJarUrl();
//3.杀死原来的进程
String killJar = "ps -ef | grep " + publishServiceManage.getJarName() + " | grep -v grep | awk '{print $2}' | xargs kill -9";
//4.删除原来jar包
String rmJarUrl = "rm -rf " + publishServiceManage.getJarName();
//5.下载最新的jar包
String wgetJarUrl = "wget " + publishServiceManage.getOssurl();
//6.启动jar包程序
String start = "nohup java -jar " + publishServiceManage.getJarName() + " >/dev/null 2>&1 &";
//7.拼接字符串
String shell = cpJar + ";" + cdJarUrl + ";" + killJar + ";" + rmJarUrl + ";" + wgetJarUrl + ";" + start;
//8.输出打印进入到jar包目录命令
log.info(shell);
Process JarUrlProc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shell});
BufferedReader JarUrlProcReader = new BufferedReader(new InputStreamReader(JarUrlProc.getInputStream()));
String JarUrlProcStr;
//读取数据
while ((JarUrlProcStr = JarUrlProcReader.readLine()) != null) {
//打印结果
log.info("cdJarUrl" + JarUrlProcStr);
}
JarUrlProc.waitFor();
JarUrlProcReader.close();
log.info(String.format("结束执行程序,message:%s,messageId:%s", message, messageId));
} catch (Exception e) {
log.error(e);
}
}
}
(1) 因为linux 命令中有使用到管道所以加入了"/bin/sh", "-c"
(2)Runtime.getRuntime().exec()
批量执行linux 命令需要每个命令用 ;
隔开
前端界面
前端界面只是一个点击操作,点击之后会发送mq消息,发布程序消费,执行linux命令,在最后收集日志到前端日志界面。
其中ossUrl:
后期会做一个jar包上传oss
的功能
感谢:
https://blog.csdn.net/u012995856/article/details/78375581
后期改进
因为现在的只是 V 1.0
版本,后期后将linux
写入到shell
脚本中,最后只需调用执行shell
脚本就可以。