[dev] fix container version bug & fuck the tomcat's websocket bug, I am going to use undertow~

This commit is contained in:
tjq 2020-05-18 09:10:30 +08:00
parent c18658003e
commit dccec9767f
5 changed files with 63 additions and 36 deletions

View File

@ -54,6 +54,17 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version> <version>${springboot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>${springboot.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -96,7 +96,7 @@ public class ServerActor extends AbstractActor {
ServerDeployContainerRequest dpReq = new ServerDeployContainerRequest(); ServerDeployContainerRequest dpReq = new ServerDeployContainerRequest();
BeanUtils.copyProperties(containerInfo, dpReq); BeanUtils.copyProperties(containerInfo, dpReq);
String downloadURL = String.format("http://%s:%s/container/downloadJar?md5=%s", NetUtils.getLocalHost(), port, containerInfo.getMd5()); String downloadURL = String.format("http://%s:%s/container/downloadJar?version=%s", NetUtils.getLocalHost(), port, containerInfo.getVersion());
dpReq.setDownloadURL(downloadURL); dpReq.setDownloadURL(downloadURL);
askResponse.setData(JsonUtils.toBytes(dpReq)); askResponse.setData(JsonUtils.toBytes(dpReq));

View File

@ -30,8 +30,8 @@ public class ContainerInfoDO {
// sourceType 决定JarFile -> String存储文件名称Git -> JSON包括 URLbranchusernamepassword // sourceType 决定JarFile -> String存储文件名称Git -> JSON包括 URLbranchusernamepassword
private String sourceInfo; private String sourceInfo;
// jar的MD5唯一作为 GridFS 的文件名 // 版本 Jar包使用md5Git使用commitId前者32位后者40位不会产生碰撞
private String md5; private String version;
// 状态枚举值为 ContainerStatus // 状态枚举值为 ContainerStatus
private Integer status; private Integer status;

View File

@ -26,6 +26,10 @@ import org.apache.commons.lang3.time.DateUtils;
import org.apache.maven.shared.invoker.*; import org.apache.maven.shared.invoker.*;
import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -43,10 +47,7 @@ import javax.websocket.RemoteEndpoint;
import javax.websocket.Session; import javax.websocket.Session;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.*;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@ -95,7 +96,9 @@ public class ContainerService {
// 文件上传形式的 sourceInfo 为该文件的 md5 Git形式的 md5 在部署阶段生成 // 文件上传形式的 sourceInfo 为该文件的 md5 Git形式的 md5 在部署阶段生成
if (request.getSourceType() == ContainerSourceType.JarFile) { if (request.getSourceType() == ContainerSourceType.JarFile) {
container.setMd5(request.getSourceInfo()); container.setVersion(request.getSourceInfo());
}else {
container.setVersion("init");
} }
containerInfoRepository.saveAndFlush(container); containerInfoRepository.saveAndFlush(container);
} }
@ -119,8 +122,6 @@ public class ContainerService {
FileUtils.forceMkdirParent(tmpFile); FileUtils.forceMkdirParent(tmpFile);
file.transferTo(tmpFile); file.transferTo(tmpFile);
// TODO检验 jar 是否合法
// 生成MD5 // 生成MD5
String md5 = OmsFileUtils.md5(tmpFile); String md5 = OmsFileUtils.md5(tmpFile);
String fileName = genContainerJarName(md5); String fileName = genContainerJarName(md5);
@ -145,21 +146,22 @@ public class ContainerService {
/** /**
* 获取构建容器所需要的 Jar 文件 * 获取构建容器所需要的 Jar 文件
* @param filename 文件名称 * @param version 版本
* @return 本地Jar文件 * @return 本地Jar文件
*/ */
public File fetchContainerJarFile(String filename) { public File fetchContainerJarFile(String version) {
String jarFileName = OmsFileUtils.genContainerJarPath() + filename; String fileName = genContainerJarName(version);
File jarFile = new File(jarFileName); String filePath = OmsFileUtils.genContainerJarPath() + fileName;
File localFile = new File(filePath);
if (jarFile.exists()) { if (localFile.exists()) {
return jarFile; return localFile;
} }
if (gridFsTemplate != null) { if (gridFsTemplate != null) {
downloadJarFromGridFS(filename, jarFile); downloadJarFromGridFS(fileName, localFile);
} }
return jarFile; return localFile;
} }
/** /**
@ -198,7 +200,6 @@ public class ContainerService {
// 准备文件 // 准备文件
File jarFile = prepareJarFile(container, session); File jarFile = prepareJarFile(container, session);
if (jarFile == null) { if (jarFile == null) {
remote.sendText("SYSTEM: prepare jarFile failed!");
return; return;
} }
@ -219,8 +220,8 @@ public class ContainerService {
} }
String port = environment.getProperty("local.server.port"); String port = environment.getProperty("local.server.port");
String downloadURL = String.format("http://%s:%s/container/downloadJar?filename=%s", NetUtils.getLocalHost(), port, jarFile.getName()); String downloadURL = String.format("http://%s:%s/container/downloadJar?version=%s", NetUtils.getLocalHost(), port, container.getVersion());
ServerDeployContainerRequest req = new ServerDeployContainerRequest(containerName, container.getMd5(), downloadURL); ServerDeployContainerRequest req = new ServerDeployContainerRequest(containerName, container.getVersion(), downloadURL);
long sleepTime = calculateSleepTime(jarFile.length()); long sleepTime = calculateSleepTime(jarFile.length());
AtomicInteger count = new AtomicInteger(); AtomicInteger count = new AtomicInteger();
@ -268,13 +269,30 @@ public class ContainerService {
} }
cloneCommand.call(); cloneCommand.call();
// 获取最新的 commitId 作为版本
String oldVersion = container.getVersion();
try (Repository repository = Git.open(workerDir).getRepository()) {
Ref head = repository.getRefDatabase().findRef("HEAD");
container.setVersion(head.getObjectId().getName());
}
if (container.getVersion().equals(oldVersion)) {
remote.sendText(String.format("SYSTEM: this commitId(%s) is the same as the last.", oldVersion));
}else {
remote.sendText(String.format("SYSTEM: new version detected, from %s to %s.", oldVersion, container.getVersion()));
}
// mvn clean package -DskipTests -U // mvn clean package -DskipTests -U
remote.sendText("SYSTEM: git clone successfully, star to compile the project."); remote.sendText("SYSTEM: git clone successfully, star to compile the project.");
Invoker mvnInvoker = new DefaultInvoker(); Invoker mvnInvoker = new DefaultInvoker();
InvocationRequest ivkReq = new DefaultInvocationRequest(); InvocationRequest ivkReq = new DefaultInvocationRequest();
ivkReq.setGoals(Lists.newArrayList("clean", "package", "-DskipTests", "-U")); // -U强制让Maven检查所有SNAPSHOT依赖更新确保集成基于最新的状态
// -e如果构建出现异常该参数能让Maven打印完整的stack trace
// -B让Maven使用批处理模式构建项目能够避免一些需要人工参与交互而造成的挂起状态
ivkReq.setGoals(Lists.newArrayList("clean", "package", "-DskipTests", "-U", "-e", "-B"));
ivkReq.setBaseDirectory(workerDir); ivkReq.setBaseDirectory(workerDir);
ivkReq.setOutputHandler(remote::sendText); ivkReq.setOutputHandler(remote::sendText);
ivkReq.setBatchMode(true);
mvnInvoker.execute(ivkReq); mvnInvoker.execute(ivkReq);
@ -289,11 +307,8 @@ public class ContainerService {
} }
File jarWithDependency = jarFile.iterator().next(); File jarWithDependency = jarFile.iterator().next();
String md5 = OmsFileUtils.md5(jarWithDependency);
// 更新 MD5
container.setMd5(md5);
String jarFileName = genContainerJarName(md5); String jarFileName = genContainerJarName(container.getVersion());
GridFsResource resource = gridFsTemplate.getResource(jarFileName); GridFsResource resource = gridFsTemplate.getResource(jarFileName);
if (!resource.exists()) { if (!resource.exists()) {
@ -318,7 +333,7 @@ public class ContainerService {
} }
// 先查询本地是否存在目标 Jar 文件 // 先查询本地是否存在目标 Jar 文件
String jarFileName = genContainerJarName(container.getMd5()); String jarFileName = genContainerJarName(container.getVersion());
String localFileStr = OmsFileUtils.genContainerJarPath() + jarFileName; String localFileStr = OmsFileUtils.genContainerJarPath() + jarFileName;
File localFile = new File(localFileStr); File localFile = new File(localFileStr);
if (localFile.exists()) { if (localFile.exists()) {
@ -357,13 +372,8 @@ public class ContainerService {
} }
} }
private static String genContainerJarName(String md5) { private static String genContainerJarName(String version) {
return String.format("oms-container-%s.jar", md5); return String.format("oms-container-%s.jar", version);
}
@Autowired(required = false)
public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
this.gridFsTemplate = gridFsTemplate;
} }
/** /**
@ -374,4 +384,10 @@ public class ContainerService {
private long calculateSleepTime(long fileLength) { private long calculateSleepTime(long fileLength) {
return (fileLength / FileUtils.ONE_MB / 10 + 1) * 1000; return (fileLength / FileUtils.ONE_MB / 10 + 1) * 1000;
} }
@Autowired(required = false)
public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
this.gridFsTemplate = gridFsTemplate;
}
} }

View File

@ -39,8 +39,8 @@ public class ContainerController {
private ContainerService containerService; private ContainerService containerService;
@GetMapping("/downloadJar") @GetMapping("/downloadJar")
public void downloadJar(String filename, HttpServletResponse response) throws IOException { public void downloadJar(String version, HttpServletResponse response) throws IOException {
File file = containerService.fetchContainerJarFile(filename); File file = containerService.fetchContainerJarFile(version);
if (file.exists()) { if (file.exists()) {
OmsFileUtils.file2HttpResponse(file, response); OmsFileUtils.file2HttpResponse(file, response);
} }