下载远程版本文件方法

This commit is contained in:
bwcx_jzy 2021-09-24 22:47:59 +08:00
parent 26cbc0af25
commit 17fff821d3
7 changed files with 90 additions and 13 deletions

View File

@ -68,6 +68,9 @@ public class JpomApplication extends ApplicationBuilder {
JpomApplication.appType = appType;
JpomApplication.appClass = appClass;
JpomApplication.args = args;
// 检查 type 中的 applicationClass 配置是否正确
String applicationClass = appType.getApplicationClass();
Assert.state(StrUtil.equals(applicationClass, appClass.getName()), "当前允许的类和配置的类名不一致io.jpom.common.Type#getApplicationClass()");
addHttpMessageConverter(new StringHttpMessageConverter(CharsetUtil.CHARSET_UTF_8));
@ -157,6 +160,7 @@ public class JpomApplication extends ApplicationBuilder {
/**
* 重启自身
* 分发会延迟2秒执行正式升级 重启命令
*/
public static void restart() {
File scriptFile = JpomManifest.getScriptFile();

View File

@ -273,9 +273,17 @@ public class JpomManifest {
return checkJpomJar(path, clsName.getName(), true);
}
// public static JsonMessage<Tuple> checkJpomJar(String path, String name) {
// return checkJpomJar(path, name, true);
// }
/**
* 检查是否为jpom包
*
* @param path 路径
* @param name 类名
* @return 结果消息
* @see Type#getApplicationClass()
*/
public static JsonMessage<Tuple> checkJpomJar(String path, String name) {
return checkJpomJar(path, name, true);
}
/**
* 检查是否为jpom包
@ -284,6 +292,7 @@ public class JpomManifest {
* @param name 类名称
* @param checkRepeat 是否检查版本重复
* @return 结果消息
* @see Type#getApplicationClass()
*/
public static JsonMessage<Tuple> checkJpomJar(String path, String name, boolean checkRepeat) {
String version;

View File

@ -2,15 +2,21 @@ package io.jpom.common;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import com.alibaba.fastjson.JSONObject;
import io.jpom.JpomApplication;
import io.jpom.system.ConfigBean;
import org.springframework.util.Assert;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
@ -174,6 +180,11 @@ public class RemoteVersion {
}
}
/**
* 缓存信息
*
* @param remoteVersion 远程版本信息
*/
private static void cacheLoadTime(RemoteVersion remoteVersion) {
remoteVersion = ObjectUtil.defaultIfNull(remoteVersion, new RemoteVersion());
remoteVersion.setLastTime(SystemClock.now());
@ -229,6 +240,36 @@ public class RemoteVersion {
return remoteVersion;
}
/**
* 升级
*
* @param savePath 下载文件保存路径
* @throws IOException 异常
*/
public static void upgrade(String savePath) throws IOException {
RemoteVersion remoteVersion = loadRemoteInfo();
Assert.notNull(remoteVersion, "没有可用的新版本升级:-1");
Assert.state(remoteVersion.getUpgrade() != null && remoteVersion.getUpgrade(), "没有可用的新版本升级");
// 检查是否存在下载地址
Type type = JpomManifest.getInstance().getType();
String remoteUrl = type.getRemoteUrl(remoteVersion);
Assert.hasText(remoteUrl, "存在新版本,下载地址不可用");
// 下载
File versionFile = HttpUtil.downloadFileFromUrl(remoteUrl, savePath);
// 解析压缩包
File file = JpomManifest.zipFileFind(FileUtil.getAbsolutePath(versionFile), type, savePath);
// 基础检查
String path = FileUtil.getAbsolutePath(file);
JsonMessage<Tuple> error = JpomManifest.checkJpomJar(path, type.getApplicationClass());
Assert.state(error.getCode() == HttpStatus.HTTP_OK, error.getMsg());
//
Tuple data = error.getData();
String version = data.get(0);
JpomManifest.releaseJar(path, version);
//
JpomApplication.restart();
}
/**
* 保存的文件
*

View File

@ -12,20 +12,26 @@ public enum Type {
/**
* 插件端
*/
Agent(RemoteVersion::getAgentUrl),
Agent("io.jpom.JpomAgentApplication", RemoteVersion::getAgentUrl),
/**
* 中心服务端
*/
Server(RemoteVersion::getServerUrl),
Server("io.jpom.JpomServerApplication", RemoteVersion::getServerUrl),
;
private final Function<RemoteVersion, String> remoteUrl;
private final String applicationClass;
Type(Function<RemoteVersion, String> remoteUrl) {
Type(String applicationClass, Function<RemoteVersion, String> remoteUrl) {
this.applicationClass = applicationClass;
this.remoteUrl = remoteUrl;
}
public String getRemoteUrl(RemoteVersion remoteVersion) {
return remoteUrl.apply(remoteVersion);
}
public String getApplicationClass() {
return applicationClass;
}
}

View File

@ -145,7 +145,7 @@ public class NodeIndexController extends BaseServerController {
File file = JpomManifest.zipFileFind(path, Type.Agent, saveDir);
path = FileUtil.getAbsolutePath(file);
// 基础检查
JsonMessage<Tuple> error = JpomManifest.checkJpomJar(path, "io.jpom.JpomAgentApplication", false);
JsonMessage<Tuple> error = JpomManifest.checkJpomJar(path, Type.Agent.getApplicationClass(), false);
if (error.getCode() != HttpStatus.HTTP_OK) {
FileUtil.del(path);
return error.toString();

View File

@ -33,7 +33,6 @@ import java.util.List;
@PreLoadClass
public class AutoImportLocalNode {
private static final String AGENT_MAIN_CLASS = "io.jpom.JpomAgentApplication";
private static NodeService nodeService;
@PreLoadMethod
@ -58,7 +57,7 @@ public class AutoImportLocalNode {
}
//
try {
List<sun.jvmstat.monitor.MonitoredVm> monitoredVms = JvmUtil.listMainClass(AGENT_MAIN_CLASS);
List<sun.jvmstat.monitor.MonitoredVm> monitoredVms = JvmUtil.listMainClass(Type.Agent.getApplicationClass());
monitoredVms.forEach(monitoredVm -> {
sun.jvmstat.monitor.VmIdentifier vmIdentifier = monitoredVm.getVmIdentifier();
findPid(vmIdentifier.getUserInfo());

View File

@ -1,12 +1,30 @@
package TestA;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import org.junit.Test;
/**
* @author bwcx_jzy
* @date 2019/8/28
*/
public class TestVersion {
public static void main(String[] args) {
String version = TestVersion.class.getPackage().getImplementationVersion();
System.out.println(version);
}
public static void main(String[] args) {
String version = TestVersion.class.getPackage().getImplementationVersion();
System.out.println(version);
}
@Test
public void test1() {
HttpRequest request = HttpUtil.createGet("https://gitee.com/dromara/Jpom/raw/master/CHANGELOG.md");
String body = request.execute().body();
System.out.println(body);
}
@Test
public void test2() {
System.out.println(CharsetUtil.defaultCharset());
}
}