学习体验下Koupleless框架-浅析代码

上次简单体验了下koupleless框架,可以发现ark模块的加载运行是非常有优势的,
这次就来看下这个模块式怎么加载的
我们可以通过启动基座使用调试模式,然后打下断点在ArkClient这个类的几个核心方法
比如com.alipay.sofa.ark.api.ArkClient#installBiz(java.io.File)
核心方法是在这

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private static ClientResponse doInstallBiz(File bizFile, String[] args, Map<String, String> envs) throws Throwable {
AssertUtils.assertNotNull(bizFactoryService, "bizFactoryService must not be null!");
AssertUtils.assertNotNull(bizManagerService, "bizManagerService must not be null!");
AssertUtils.assertNotNull(bizFile, "bizFile must not be null!");
long start = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss,SSS");
String startDate = sdf.format(new Date(start));
Biz biz = bizFactoryService.createBiz(bizFile);
ClientResponse response = new ClientResponse();
if (bizManagerService.getBizByIdentity(biz.getIdentity()) == null && bizManagerService.registerBiz(biz)) {
try {
biz.start(args, envs);
long end = System.currentTimeMillis();
response.setCode(ResponseCode.SUCCESS).setMessage(String.format("Install Biz: %s success, cost: %s ms, started at: %s", biz.getIdentity(), end - start, startDate)).setBizInfos(Collections.singleton(biz));
getLogger().info(response.getMessage());
return response;
} catch (Throwable var15) {
long end = System.currentTimeMillis();
response.setCode(ResponseCode.FAILED).setMessage(String.format("Install Biz: %s fail,cost: %s ms, started at: %s", biz.getIdentity(), end - start, startDate));
getLogger().error(response.getMessage(), var15);
boolean autoUninstall = Boolean.parseBoolean(ArkConfigs.getStringValue("sofa.ark.auto.uninstall.when.failed.enable", "true"));
if (autoUninstall) {
try {
getLogger().error(String.format("Start Biz: %s failed, try to unInstall this biz.", biz.getIdentity()));
biz.stop();
} catch (Throwable var14) {
getLogger().error(String.format("UnInstall Biz: %s fail.", biz.getIdentity()), var14);
}
}

throw var15;
}
} else {
return response.setCode(ResponseCode.REPEAT_BIZ).setMessage(String.format("Biz: %s has been installed or registered.", biz.getIdentity()));
}
}

具体的代码在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Biz createBiz(File file) throws IOException {
boolean unpackBizWhenInstall = Boolean.parseBoolean(ArkConfigs.getStringValue("sofa.ark.unpack.biz.when.install", "true"));
Object bizArchive;
if (ArkConfigs.isEmbedEnable() && unpackBizWhenInstall) {
File unpackFile = FileUtils.file(file.getAbsolutePath() + "-unpack");
if (!unpackFile.exists()) {
unpackFile = FileUtils.unzip(file, file.getAbsolutePath() + "-unpack");
}

if (file.exists()) {
file.delete();
}

file = unpackFile;
bizArchive = new ExplodedBizArchive(unpackFile);
} else {
JarFile bizFile = new JarFile(file);
JarFileArchive jarFileArchive = new JarFileArchive(bizFile);
bizArchive = new JarBizArchive(jarFileArchive);
}

BizModel biz = (BizModel)this.createBiz((BizArchive)bizArchive);
biz.setBizTempWorkDir(file);
return biz;
}

接下来是创建biz模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public Biz createBiz(BizArchive bizArchive) throws IOException {
AssertUtils.isTrue(this.isArkBiz(bizArchive), "Archive must be a ark biz!", new Object[0]);
BizModel bizModel = new BizModel();
Attributes manifestMainAttributes = bizArchive.getManifest().getMainAttributes();
String mainClass = manifestMainAttributes.getValue("Main-Class");
String startClass = manifestMainAttributes.getValue("Start-Class");
bizModel.setBizState(BizState.RESOLVED, StateChangeReason.CREATED).setBizName(manifestMainAttributes.getValue("Ark-Biz-Name")).setBizVersion(manifestMainAttributes.getValue("Ark-Biz-Version")).setMainClass(!StringUtils.isEmpty(startClass) ? startClass : mainClass).setPriority(manifestMainAttributes.getValue("priority")).setWebContextPath(manifestMainAttributes.getValue("web-context-path")).setDenyImportPackages(manifestMainAttributes.getValue("deny-import-packages")).setDenyImportClasses(manifestMainAttributes.getValue("deny-import-classes")).setDenyImportResources(manifestMainAttributes.getValue("deny-import-resources")).setInjectPluginDependencies(this.getInjectDependencies(manifestMainAttributes.getValue("inject-plugin-dependencies"))).setInjectExportPackages(manifestMainAttributes.getValue("inject-export-packages")).setDeclaredLibraries(manifestMainAttributes.getValue("declared-libraries")).setClassPath(bizArchive.getUrls()).setPluginClassPath(this.getPluginURLs());
if (!(bizArchive instanceof DirectoryBizArchive)) {
bizModel.setBizUrl(bizArchive.getUrl());
}

BizClassLoader bizClassLoader = new BizClassLoader(bizModel.getIdentity(), this.getBizUcp(bizModel.getClassPath()), bizArchive instanceof ExplodedBizArchive || bizArchive instanceof DirectoryBizArchive);
bizClassLoader.setBizModel(bizModel);
bizModel.setClassLoader(bizClassLoader);
return bizModel;
}

这里就会读取对应biz包的启动类等,以及创建对应的biz模块的类加载器 BizClassLoader
接下去回到上一层

1
biz.start(args, envs);

接下来就是BizModel的start方法

1
2
3
public void start(String[] args, Map<String, String> envs) throws Throwable {
this.doStart(args, envs);
}

实际的就是doStart方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
private void doStart(String[] args, Map<String, String> envs) throws Throwable {
AssertUtils.isTrue(this.bizState == BizState.RESOLVED, "BizState must be RESOLVED", new Object[0]);
if (envs != null) {
String mainClassFromEnv = (String)envs.get("sofa.ark.biz.main.class");
if (mainClassFromEnv != null) {
this.mainClass = mainClassFromEnv;
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} will start with main class {} from envs", this.getIdentity(), mainClassFromEnv);
}
}

if (this.mainClass == null) {
throw new ArkRuntimeException(String.format("biz: %s has no main method", this.getBizName()));
} else {
ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.classLoader);
EventAdminService eventAdminService = (EventAdminService)ArkServiceContainerHolder.getContainer().getService(EventAdminService.class);

try {
eventAdminService.sendEvent(new BeforeBizStartupEvent(this));
this.resetProperties();
if (!this.isMasterBizAndEmbedEnable()) {
long start = System.currentTimeMillis();
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} start.", this.getIdentity());
MainMethodRunner mainMethodRunner = new MainMethodRunner(this.mainClass, args, envs);
mainMethodRunner.run();
eventAdminService.sendEvent(new AfterBizStartupEvent(this));
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} started in {} ms", this.getIdentity(), System.currentTimeMillis() - start);
}
} catch (Throwable var11) {
this.setBizState(BizState.BROKEN, StateChangeReason.INSTALL_FAILED, getStackTraceAsString(var11));
eventAdminService.sendEvent(new AfterBizStartupFailedEvent(this, var11));
throw var11;
} finally {
ClassLoaderUtils.popContextClassLoader(oldClassLoader);
}

BizManagerService bizManagerService = (BizManagerService)ArkServiceContainerHolder.getContainer().getService(BizManagerService.class);
if (bizManagerService.getActiveBiz(this.bizName) == null) {
this.setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
} else {
boolean activateMultiBizVersion = Boolean.parseBoolean(ArkConfigs.getStringValue("sofa.ark.activate.multi.biz.version.enable", "false"));
if (activateMultiBizVersion) {
this.setBizState(BizState.ACTIVATED, StateChangeReason.STARTED);
} else {
if (Boolean.getBoolean("activate.new.module")) {
Biz currentActiveBiz = bizManagerService.getActiveBiz(this.bizName);
((BizModel)currentActiveBiz).setBizState(BizState.DEACTIVATED, StateChangeReason.SWITCHED, String.format("switch to new biz %s", this.getIdentity()));
this.setBizState(BizState.ACTIVATED, StateChangeReason.STARTED, String.format("switch from old biz: %s", currentActiveBiz.getIdentity()));
} else {
this.setBizState(BizState.DEACTIVATED, StateChangeReason.STARTED, "start but is deactivated");
}

}
}
}
}

主要是看这个方法

1
2
3
ArkLoggerFactory.getDefaultLogger().info("Ark biz {} start.", this.getIdentity());
MainMethodRunner mainMethodRunner = new MainMethodRunner(this.mainClass, args, envs);
mainMethodRunner.run();

看到这个run方法

1
2
3
4
5
public Object run() throws Exception {
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
return mainMethod.invoke((Object)null, this.args);
}

最终就是到这个,它就用了这个上下文的类加载器,而这个类加载器就是用的前面设置的bizClassLoader

1
2
3
4
5
public static ClassLoader pushContextClassLoader(ClassLoader newClassLoader) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(newClassLoader);
return oldClassLoader;
}