This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from opendx/0.8.6
0.8.6
- Loading branch information
Showing
18 changed files
with
314 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ test-output/ | |
*.mp4 | ||
*.jpg | ||
spring.log* | ||
ext/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/daxiang/controller/AgentExtJarController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.daxiang.controller; | ||
|
||
import com.daxiang.model.Response; | ||
import com.daxiang.service.AgentExtJarService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Created by jiangyitao. | ||
*/ | ||
@RestController | ||
@RequestMapping("/agentExtJar") | ||
public class AgentExtJarController { | ||
|
||
@Autowired | ||
private AgentExtJarService agentExtJarService; | ||
|
||
@PostMapping("/load") | ||
public Response loadExtJar(@RequestBody String jarUrl) { | ||
agentExtJarService.loadJar(jarUrl); | ||
return Response.success("加载成功"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/daxiang/core/classloader/AgentClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.daxiang.core.classloader; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
/** | ||
* Created by jiangyitao. | ||
*/ | ||
public class AgentClassLoader extends URLClassLoader { | ||
|
||
public AgentClassLoader() { | ||
super(new URL[0], ClassLoader.getSystemClassLoader()); | ||
} | ||
|
||
public void addJar(File jar) { | ||
try { | ||
super.addURL(jar.toURI().toURL()); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/main/java/com/daxiang/core/classloader/AgentExtJarLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.daxiang.core.classloader; | ||
|
||
import com.daxiang.model.AgentExtJar; | ||
import com.daxiang.server.ServerClient; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.util.DigestUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.*; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Created by jiangyitao. | ||
*/ | ||
@Slf4j | ||
public class AgentExtJarLoader { | ||
|
||
public static final String JAR_DIR = "ext"; | ||
|
||
private static final AgentExtJarLoader INSTANCE = new AgentExtJarLoader(); | ||
|
||
private Map<String, File> loadedJarMap; | ||
private AgentClassLoader classLoader; | ||
|
||
private AgentExtJarLoader() { | ||
loadedJarMap = new HashMap<>(); | ||
classLoader = new AgentClassLoader(); | ||
} | ||
|
||
public static AgentExtJarLoader getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public ClassLoader getClassLoader() { | ||
return classLoader; | ||
} | ||
|
||
public synchronized void load(File jar) { | ||
String artifactId = getJarNameWithoutVersion(jar); | ||
if (loadedJarMap.containsKey(artifactId)) { | ||
// 使用新的ClassLoader加载其他已经加载过的jar,否则无法做到热更新 | ||
classLoader = new AgentClassLoader(); | ||
loadedJarMap.remove(artifactId); | ||
reload(); | ||
} | ||
|
||
loadJar(jar); | ||
loadedJarMap.put(artifactId, jar); | ||
} | ||
|
||
private String getJarNameWithoutVersion(File jar) { | ||
Matcher matcher = Pattern.compile("(.+)-([0-9].*)\\.jar").matcher(jar.getName()); | ||
while (matcher.find()) { | ||
return matcher.group(1); // spring-boot | ||
} | ||
|
||
throw new IllegalArgumentException(jar.getName() + "文件名不合法"); | ||
} | ||
|
||
private void reload() { | ||
log.info("reload"); | ||
loadedJarMap.values().forEach(this::loadJar); | ||
} | ||
|
||
private void loadJar(File jar) { | ||
log.info("loadJar: {}", jar); | ||
classLoader.addJar(jar); | ||
} | ||
|
||
/** | ||
* 初始化ext jar | ||
* 1. 以server返回的ext jar为准,EXT_JAR_DIR多删少下载 | ||
* 2. 加载jar | ||
*/ | ||
public void initExtJars() { | ||
File extJarDir = new File(JAR_DIR); | ||
if (!extJarDir.exists()) { | ||
extJarDir.mkdir(); | ||
} | ||
|
||
Set<AgentExtJar> serverJars = ServerClient.getInstance().getAgentExtJars(); | ||
log.info("server extJars: {}", serverJars); | ||
|
||
Set<AgentExtJar> localJars = Stream.of(extJarDir.listFiles()).map(file -> { | ||
AgentExtJar localJar = new AgentExtJar(); | ||
try { | ||
String md5 = DigestUtils.md5DigestAsHex(FileUtils.readFileToByteArray(file)); | ||
localJar.setMd5(md5); | ||
} catch (IOException e) { | ||
boolean deleteSuccess = file.delete(); | ||
log.error("read localJar={} err, delete it success? {}", file, deleteSuccess, e); | ||
return null; | ||
} | ||
localJar.setFilename(file.getName()); | ||
localJar.setFile(file); | ||
return localJar; | ||
}).filter(Objects::nonNull).collect(Collectors.toSet()); | ||
log.info("local extJars: {}", localJars); | ||
|
||
for (AgentExtJar localJar : localJars) { | ||
if (serverJars.contains(localJar)) { | ||
AgentExtJarLoader.getInstance().load(localJar.getFile()); | ||
} else { | ||
// 删除和服务端不匹配的文件 | ||
boolean deleteSucess = localJar.getFile().delete(); | ||
log.info("delete local extJar: {} success? {}", localJar, deleteSucess); | ||
} | ||
} | ||
|
||
// 下载本地没有的jar | ||
serverJars.stream().filter(serverJar -> !localJars.contains(serverJar)).forEach(serverJar -> { | ||
try { | ||
File localJarFile = new File(JAR_DIR, serverJar.getFilename()); | ||
log.info("download {} from {}", localJarFile, serverJar.getDownloadUrl()); | ||
FileUtils.copyURLToFile(new URL(serverJar.getDownloadUrl()), localJarFile); | ||
AgentExtJarLoader.getInstance().load(localJarFile); | ||
} catch (IOException e) { | ||
log.error("download {} err", serverJar.getDownloadUrl(), e); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.daxiang.model; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.io.File; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Created by jiangyitao. | ||
*/ | ||
@Getter | ||
@Setter | ||
public class AgentExtJar { | ||
private String filename; | ||
private String md5; | ||
private File file; | ||
private String downloadUrl; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AgentExtJar that = (AgentExtJar) o; | ||
return md5.equals(that.md5); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(md5); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return filename; | ||
} | ||
} |
Oops, something went wrong.