Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 2ab5540

Browse files
authored
Merge pull request #422 from sofastack/f_cnf
支持CNF启动加速
2 parents 82be164 + fde12e2 commit 2ab5540

File tree

17 files changed

+671
-1
lines changed

17 files changed

+671
-1
lines changed

sofa-serverless-runtime/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
<module>arklet-core</module>
8383
<module>arklet-springboot-starter</module>
8484
<module>sofa-serverless-adapter-ext</module>
85-
</modules>
85+
<module>sofa-serverless-base-loader</module>
86+
</modules>
8687

8788
<dependencyManagement>
8889
<dependencies>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>sofa-serverless-runtime</artifactId>
7+
<groupId>com.alipay.sofa.serverless</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<version>${revision}</version>
13+
<packaging>pom</packaging>
14+
<artifactId>sofa-serverless-base-loader</artifactId>
15+
<modules>
16+
<module>sofa-serverless-spring-loader</module>
17+
<module>sofa-serverless-spring-loader-tool</module>
18+
</modules>
19+
20+
<properties>
21+
<maven.compiler.source>8</maven.compiler.source>
22+
<maven.compiler.target>8</maven.compiler.target>
23+
</properties>
24+
25+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 使用说明
2+
3+
1. 引入打包依赖
4+
```xml
5+
<plugin>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-maven-plugin</artifactId>
8+
<version>2.7.15</version>
9+
<configuration>
10+
<outputDirectory>../../target/boot</outputDirectory>
11+
<classifier>executable</classifier>
12+
</configuration>
13+
<executions>
14+
<execution>
15+
<id>package</id>
16+
<goals>
17+
<goal>repackage</goal>
18+
</goals>
19+
</execution>
20+
</executions>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.alipay.sofa.serverless</groupId>
24+
<artifactId>sofa-serverless-spring-loader-tool</artifactId>
25+
<!--最新版本0.5.6-->
26+
<version>0.5.6</version>
27+
</dependency>
28+
</dependencies>
29+
</plugin>
30+
```
31+
2. fat jar启动方式不变,会默认使用sofa-serverless-spring-loader的JarLauncher启动
32+
```shell
33+
java -jar xxx-executable.jar
34+
```
35+
3. 解压启动方式,Launcher需要改成com.alipay.sofa.serverless.spring.loader.JarLauncher
36+
```shell
37+
java -classpath xxx-executable-unpack com.alipay.sofa.serverless.spring.loader.JarLauncher
38+
```
39+
40+
# 维护说明
41+
42+
如果改了ofa-serverless-spring-loader代码,需要先手动mvn打包,然后将sofa-serverless-spring-loader/target/sofa-serverless-spring-loader-xxx.jar复制到sofa-serverless-spring-loader-tool/src/main/resources/META-INF/loader
43+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.alipay.sofa.serverless</groupId>
7+
<artifactId>sofa-serverless-base-loader</artifactId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>sofa-serverless-spring-loader-tool</artifactId>
14+
<version>${revision}</version>
15+
16+
<properties>
17+
<maven.compiler.source>8</maven.compiler.source>
18+
<maven.compiler.target>8</maven.compiler.target>
19+
</properties>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-loader</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-loader-tools</artifactId>
28+
<version>${spring.boot.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-inline</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.serverless.spring.loader.tools;
18+
19+
import java.io.File;
20+
21+
import org.springframework.boot.loader.tools.Layout;
22+
import org.springframework.boot.loader.tools.LayoutFactory;
23+
24+
/**
25+
* CustomLayoutFactory
26+
* @author zjulbj
27+
* @daye 2023/12/26
28+
* @version CustomLayoutFactory.java, v 0.1 2023年12月26日 14:45 syd
29+
*/
30+
public class CustomLayoutFactory implements LayoutFactory {
31+
@Override
32+
public Layout getLayout(File source) {
33+
return Layouts.forFile(source);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.serverless.spring.loader.tools;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.Locale;
24+
import java.util.Map;
25+
26+
import org.springframework.boot.loader.tools.CustomLoaderLayout;
27+
import org.springframework.boot.loader.tools.Layout;
28+
import org.springframework.boot.loader.tools.LibraryScope;
29+
import org.springframework.boot.loader.tools.LoaderClassesWriter;
30+
31+
/**
32+
* Custom Layouts
33+
* @author zjulbj
34+
* @daye 2023/12/26
35+
* @version Layouts.java, v 0.1 2023年12月26日 14:45 syd
36+
*/
37+
public class Layouts {
38+
private Layouts() {
39+
}
40+
41+
/**
42+
* Return a layout for the given source file.
43+
*
44+
* @param file the source file
45+
* @return a {@link Layout}
46+
*/
47+
public static Layout forFile(File file) {
48+
if (file == null) {
49+
throw new IllegalArgumentException("File must not be null");
50+
}
51+
String lowerCaseFileName = file.getName().toLowerCase(Locale.ENGLISH);
52+
if (lowerCaseFileName.endsWith(".jar")) {
53+
return new Jar();
54+
}
55+
return org.springframework.boot.loader.tools.Layouts.forFile(file);
56+
}
57+
58+
/**
59+
* Executable JAR layout.
60+
*/
61+
public static class Jar extends org.springframework.boot.loader.tools.Layouts.Jar implements
62+
CustomLoaderLayout {
63+
@Override
64+
public String getLauncherClassName() {
65+
return "com.alipay.sofa.serverless.spring.loader.JarLauncher";
66+
}
67+
68+
@Override
69+
public void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {
70+
writer.writeLoaderClasses("META-INF/loader/spring-boot-loader.jar");
71+
writer.writeLoaderClasses("META-INF/loader/sofa-serverless-spring-loader.jar");
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.loader.tools.LayoutFactory=com.alipay.sofa.serverless.spring.loader.tools.CustomLayoutFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.serverless.spring.loader.tools;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.net.URL;
22+
import java.net.URLClassLoader;
23+
import java.util.jar.JarFile;
24+
25+
import com.alipay.sofa.serverless.spring.loader.tools.Layouts.Jar;
26+
import junit.framework.TestCase;
27+
import org.junit.Assert;
28+
29+
import org.springframework.boot.loader.tools.JarWriter;
30+
import org.springframework.boot.loader.tools.Layout;
31+
32+
public class LayoutsTest extends TestCase {
33+
34+
public void testForNullFile() throws IOException {
35+
IllegalArgumentException exception = null;
36+
try {
37+
Layouts.forFile(null);
38+
} catch (IllegalArgumentException e) {
39+
exception = e;
40+
}
41+
assertNotNull(exception);
42+
}
43+
44+
public void testForFile() throws IOException {
45+
CustomLayoutFactory customLayoutFactory = new CustomLayoutFactory();
46+
47+
File appJar = new File(getClass().getClassLoader().getResource("demo-executable.jar")
48+
.getFile());
49+
Layout layout = customLayoutFactory.getLayout(appJar);
50+
assertTrue(layout instanceof Jar);
51+
Jar jar = (Jar) layout;
52+
assertEquals("com.alipay.sofa.serverless.spring.loader.JarLauncher",
53+
jar.getLauncherClassName());
54+
File rewrite = new File(appJar.getParent() + "/demo-executable-rewrite.jar");
55+
JarWriter writer = new JarWriter(rewrite);
56+
jar.writeLoadedClasses(writer);
57+
writer.close();
58+
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { rewrite.toURI().toURL() },
59+
null);
60+
try {
61+
urlClassLoader.loadClass(jar.getLauncherClassName());
62+
} catch (ClassNotFoundException e) {
63+
Assert.fail(e.getMessage());
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)