From c5c7f7fd713ab9c54a0ad3404c1ed33103b7e0fb Mon Sep 17 00:00:00 2001 From: tjq Date: Tue, 19 May 2020 22:44:55 +0800 Subject: [PATCH] [fix] fix instanceLog's bug --- .../kfcfans/oms/server/akka/OhMyServer.java | 4 ++-- .../common/constans/ContainerSourceType.java | 2 +- .../oms/server/common/utils/TimeUtils.java | 2 ++ .../oms/server/service/ContainerService.java | 2 +- .../web/controller/ContainerController.java | 12 ++++++++++++ .../web/controller/InstanceController.java | 2 +- .../server/web/response/ContainerInfoVO.java | 6 +++--- others/Dockerfile | 8 ++++---- others/script/build_docker.sh | 19 +++++++++++++++++++ 9 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 others/script/build_docker.sh diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/akka/OhMyServer.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/akka/OhMyServer.java index 6e4c1054..6687ab39 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/akka/OhMyServer.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/akka/OhMyServer.java @@ -8,7 +8,6 @@ import com.github.kfcfans.oms.common.utils.NetUtils; import com.github.kfcfans.oms.server.akka.actors.FriendActor; import com.github.kfcfans.oms.server.akka.actors.ServerActor; import com.github.kfcfans.oms.server.common.utils.PropertyUtils; -import com.github.kfcfans.oms.server.common.utils.TimeUtils; import com.google.common.base.Stopwatch; import com.google.common.collect.Maps; import com.typesafe.config.Config; @@ -39,7 +38,8 @@ public class OhMyServer { Stopwatch stopwatch = Stopwatch.createStarted(); log.info("[OhMyServer] OhMyServer's akka system start to bootstrap..."); - TimeUtils.check(); + // 忽略了一个问题,机器是没办法访问外网的,除非架设自己的NTP服务器 + // TimeUtils.check(); // 解析配置文件 PropertyUtils.init(); diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/constans/ContainerSourceType.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/constans/ContainerSourceType.java index 9419da76..e526a081 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/constans/ContainerSourceType.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/constans/ContainerSourceType.java @@ -13,7 +13,7 @@ import lombok.Getter; @AllArgsConstructor public enum ContainerSourceType { - JarFile(1, "Jar文件"), + FatJar(1, "Jar文件"), Git(2, "Git代码库"); private final int v; diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/utils/TimeUtils.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/utils/TimeUtils.java index 46a2dcd0..2e2d22b9 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/utils/TimeUtils.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/common/utils/TimeUtils.java @@ -55,6 +55,8 @@ public class TimeUtils { } } + + public static final class TimeCheckException extends RuntimeException { public TimeCheckException(String message) { super(message); diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/ContainerService.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/ContainerService.java index 529286c7..59fea182 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/ContainerService.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/service/ContainerService.java @@ -90,7 +90,7 @@ public class ContainerService { container.setStatus(request.getStatus().getV()); // 文件上传形式的 sourceInfo 为该文件的 md5 值,Git形式的 md5 在部署阶段生成 - if (request.getSourceType() == ContainerSourceType.JarFile) { + if (request.getSourceType() == ContainerSourceType.FatJar) { container.setVersion(request.getSourceInfo()); }else { container.setVersion("init"); diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/ContainerController.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/ContainerController.java index 940d92fd..038d2fa3 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/ContainerController.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/ContainerController.java @@ -3,6 +3,8 @@ package com.github.kfcfans.oms.server.web.controller; import com.github.kfcfans.oms.common.model.DeployedContainerInfo; import com.github.kfcfans.oms.common.response.ResultDTO; import com.github.kfcfans.oms.server.akka.OhMyServer; +import com.github.kfcfans.oms.server.common.constans.ContainerSourceType; +import com.github.kfcfans.oms.server.common.constans.ContainerStatus; import com.github.kfcfans.oms.server.common.utils.ContainerTemplateGenerator; import com.github.kfcfans.oms.server.common.utils.OmsFileUtils; import com.github.kfcfans.oms.server.persistence.core.model.AppInfoDO; @@ -15,6 +17,7 @@ import com.github.kfcfans.oms.server.web.request.GenerateContainerTemplateReques import com.github.kfcfans.oms.server.web.request.SaveContainerInfoRequest; import com.github.kfcfans.oms.server.web.response.ContainerInfoVO; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; @@ -109,6 +112,15 @@ public class ContainerController { private static ContainerInfoVO convert(ContainerInfoDO containerInfoDO) { ContainerInfoVO vo = new ContainerInfoVO(); BeanUtils.copyProperties(containerInfoDO, vo); + if (containerInfoDO.getLastDeployTime() == null) { + vo.setLastDeployTime("N/A"); + }else { + vo.setLastDeployTime(DateFormatUtils.format(containerInfoDO.getLastDeployTime(), "yyyy-MM-dd HH:mm:ss")); + } + ContainerStatus status = ContainerStatus.of(containerInfoDO.getStatus()); + vo.setStatus(status.name()); + ContainerSourceType sourceType = ContainerSourceType.of(containerInfoDO.getSourceType()); + vo.setSourceType(sourceType.name()); return vo; } } diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java index db4f83cb..873fb2e5 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/controller/InstanceController.java @@ -78,7 +78,7 @@ public class InstanceController { // 转发HTTP请求(如果使用Akka,则需要传输两次,而转发HTTP请求只需要传输一次"大"数据包) if (!OhMyServer.getActorSystemAddress().equals(targetServer)) { String ip = targetServer.split(":")[0]; - String url = "http://" + ip + ":" + port + "/instance/log?instanceId=" + instanceId; + String url = String.format("http://%s:%s/instance/log?instanceId=%d&index=%d", ip, port, instanceId, index); try { response.sendRedirect(url); return ResultDTO.success(StringPage.simple("redirecting...")); diff --git a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/response/ContainerInfoVO.java b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/response/ContainerInfoVO.java index 3438c4d3..82bfceb0 100644 --- a/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/response/ContainerInfoVO.java +++ b/oh-my-scheduler-server/src/main/java/com/github/kfcfans/oms/server/web/response/ContainerInfoVO.java @@ -18,7 +18,7 @@ public class ContainerInfoVO { private String containerName; // 容器类型,枚举值为 ContainerSourceType - private Integer sourceType; + private String sourceType; // 由 sourceType 决定,JarFile -> String,存储文件名称;Git -> JSON,包括 URL,branch,username,password private String sourceInfo; @@ -26,10 +26,10 @@ public class ContainerInfoVO { private String version; // 状态,枚举值为 ContainerStatus - private Integer status; + private String status; // 上一次部署时间 - private Date lastDeployTime; + private String lastDeployTime; private Date gmtCreate; private Date gmtModified; diff --git a/others/Dockerfile b/others/Dockerfile index 8c575df0..0218955c 100644 --- a/others/Dockerfile +++ b/others/Dockerfile @@ -1,5 +1,5 @@ -# 基础镜像 -FROM openjdk:8 +# 基础镜像(由于OhMyScheduler-Server的运行需要Maven和Git环境,就自己打包了一个镜像) +FROM tjqq/jdk-maven-git:8 # 维护者 MAINTAINER tengjiqi@gmail.com # 设置环境变量 @@ -8,8 +8,8 @@ ENV APP_NAME=oh-my-scheduler-server ENV PARAMS="" # 将应用 jar 包拷入 docker COPY oms-server.jar /oms-server.jar -# 暴露端口(HTTP + AKKA) -EXPOSE 7700 10086 +# 暴露端口(HTTP + AKKA-Server + AKKA-Client) +EXPOSE 7700 10086 27777 # 创建 docker 文件目录(盲猜这是用户目录) RUN mkdir -p /root/oms-server # 挂载数据卷,将文件直接输出到宿主机(注意,此处挂载的是匿名卷,即在宿主机位置随机) diff --git a/others/script/build_docker.sh b/others/script/build_docker.sh new file mode 100644 index 00000000..752cf70d --- /dev/null +++ b/others/script/build_docker.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# -p:允许后面跟一个字符串作为提示 -r:保证读入的是原始内容,不会发生任何转义 +read -r -p "请输入Dockedr镜像版本:" version +echo "即将构建的Docker镜像:oms-server:$version" +read -r -p "任意键继续:" + +# 一键部署脚本,请勿挪动脚本 +cd `dirname $0`/../.. || exit +echo "================== 构建 jar ==================" +mvn clean package -DskipTests -Pdev -U -e +echo "================== 拷贝 jar ==================" +/bin/cp -rf oh-my-scheduler-server/target/*.jar others/oms-server.jar +ls -l others/oms-server.jar +echo "================== 构建应用镜像 ==================" +docker build -t tjqq/oms-server:$version others/. || exit +echo "================== (关闭老应用)括号代表非必须,只是顺便运行下新版本进行测试 ==================" +docker stop oms-server +echo "================== (删除老容器) ==================" +docker container rm oms-server