|
| 1 | +# tio-http-server-native |
| 2 | + |
| 3 | +将基于 tio-http-server 的 jar 编译成二进制文件运行 |
| 4 | +pom.xml |
| 5 | + |
| 6 | +``` |
| 7 | + <properties> |
| 8 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 9 | + <java.version>1.8</java.version> |
| 10 | + <maven.compiler.source>${java.version}</maven.compiler.source> |
| 11 | + <maven.compiler.target>${java.version}</maven.compiler.target> |
| 12 | + <graalvm.version>23.1.1</graalvm.version> |
| 13 | + <tinylog.version>2.6.2</tinylog.version> |
| 14 | + <mainClass.server>demo.DemoHttpServer</mainClass.server> |
| 15 | + </properties> |
| 16 | +
|
| 17 | + <dependencies> |
| 18 | + <dependency> |
| 19 | + <groupId>com.litongjava</groupId> |
| 20 | + <artifactId>tio-http-server</artifactId> |
| 21 | + <version>3.7.3.v20240130-RELEASE</version> |
| 22 | + </dependency> |
| 23 | + <dependency> |
| 24 | + <groupId>org.projectlombok</groupId> |
| 25 | + <artifactId>lombok</artifactId> |
| 26 | + <version>1.18.30</version> |
| 27 | + <optional>true</optional> |
| 28 | + <scope>provided</scope> |
| 29 | + </dependency> |
| 30 | + <dependency> |
| 31 | + <groupId>com.google.code.gson</groupId> |
| 32 | + <artifactId>gson</artifactId> |
| 33 | + <version>2.10.1</version> |
| 34 | + </dependency> |
| 35 | + </dependencies> |
| 36 | + <build> |
| 37 | + <finalName>${project.artifactId}</finalName> |
| 38 | + </build> |
| 39 | + <profiles> |
| 40 | + <profile> |
| 41 | + <id>jar</id> |
| 42 | + <activation> |
| 43 | + <activeByDefault>true</activeByDefault> |
| 44 | + </activation> |
| 45 | + <dependencies> |
| 46 | + <!-- 非 GraalVM 环境用 tinylog --> |
| 47 | + <dependency> |
| 48 | + <groupId>org.tinylog</groupId> |
| 49 | + <artifactId>slf4j-tinylog</artifactId> |
| 50 | + <version>${tinylog.version}</version> |
| 51 | + </dependency> |
| 52 | + <dependency> |
| 53 | + <groupId>org.tinylog</groupId> |
| 54 | + <artifactId>tinylog-impl</artifactId> |
| 55 | + <version>${tinylog.version}</version> |
| 56 | + </dependency> |
| 57 | + </dependencies> |
| 58 | + <build> |
| 59 | + <plugins> |
| 60 | + <plugin> |
| 61 | + <groupId>org.apache.maven.plugins</groupId> |
| 62 | + <artifactId>maven-jar-plugin</artifactId> |
| 63 | + <version>3.2.0</version> |
| 64 | + </plugin> |
| 65 | + <plugin> |
| 66 | + <groupId>org.apache.maven.plugins</groupId> |
| 67 | + <artifactId>maven-assembly-plugin</artifactId> |
| 68 | + <version>3.1.1</version> |
| 69 | + <configuration> |
| 70 | + <archive> |
| 71 | + <manifest> |
| 72 | + <mainClass>${mainClass.server}</mainClass> |
| 73 | + </manifest> |
| 74 | + </archive> |
| 75 | + <descriptorRefs> |
| 76 | + <descriptorRef>jar-with-dependencies</descriptorRef> |
| 77 | + </descriptorRefs> |
| 78 | + <appendAssemblyId>false</appendAssemblyId> |
| 79 | + </configuration> |
| 80 | + <executions> |
| 81 | + <execution> |
| 82 | + <id>make-assembly</id> |
| 83 | + <phase>package</phase> |
| 84 | + <goals> |
| 85 | + <goal>single</goal> |
| 86 | + </goals> |
| 87 | + </execution> |
| 88 | + </executions> |
| 89 | + </plugin> |
| 90 | + </plugins> |
| 91 | + </build> |
| 92 | + </profile> |
| 93 | + <profile> |
| 94 | + <id>native</id> |
| 95 | + <dependencies> |
| 96 | + <!-- GraalVM 环境使用 jdk log --> |
| 97 | + <dependency> |
| 98 | + <groupId>org.slf4j</groupId> |
| 99 | + <artifactId>slf4j-jdk14</artifactId> |
| 100 | + <version>1.7.31</version> |
| 101 | + </dependency> |
| 102 | + <!-- GraalVM --> |
| 103 | + <dependency> |
| 104 | + <groupId>org.graalvm.sdk</groupId> |
| 105 | + <artifactId>graal-sdk</artifactId> |
| 106 | + <version>${graalvm.version}</version> |
| 107 | + <scope>provided</scope> |
| 108 | + </dependency> |
| 109 | + </dependencies> |
| 110 | + <build> |
| 111 | + <finalName>tio-http-server-graal</finalName> |
| 112 | + <plugins> |
| 113 | + <plugin> |
| 114 | + <groupId>org.graalvm.nativeimage</groupId> |
| 115 | + <artifactId>native-image-maven-plugin</artifactId> |
| 116 | + <version>21.2.0</version> |
| 117 | + <executions> |
| 118 | + <execution> |
| 119 | + <goals> |
| 120 | + <goal>native-image</goal> |
| 121 | + </goals> |
| 122 | + <phase>package</phase> |
| 123 | + </execution> |
| 124 | + </executions> |
| 125 | + <configuration> |
| 126 | + <skip>false</skip> |
| 127 | + <imageName>${project.artifactId}</imageName> |
| 128 | + <mainClass>${mainClass.server}</mainClass> |
| 129 | + <buildArgs> |
| 130 | + -H:+RemoveSaturatedTypeFlows |
| 131 | + --allow-incomplete-classpath |
| 132 | + --no-fallback |
| 133 | + </buildArgs> |
| 134 | + </configuration> |
| 135 | + </plugin> |
| 136 | + </plugins> |
| 137 | + </build> |
| 138 | + </profile> |
| 139 | + </profiles> |
| 140 | +``` |
| 141 | + |
| 142 | +启动类 |
| 143 | + |
| 144 | +``` |
| 145 | +package demo; |
| 146 | +
|
| 147 | +import com.litongjava.tio.http.common.HttpConfig; |
| 148 | +import com.litongjava.tio.http.common.handler.HttpRequestHandler; |
| 149 | +import com.litongjava.tio.http.server.HttpServerStarter; |
| 150 | +import com.litongjava.tio.http.server.handler.HttpRoutes; |
| 151 | +import com.litongjava.tio.http.server.handler.SimpleHttpDispatcherHandler; |
| 152 | +import com.litongjava.tio.http.server.handler.SimpleHttpRoutes; |
| 153 | +import com.litongjava.tio.utils.json.GsonFactory; |
| 154 | +import com.litongjava.tio.utils.json.Json; |
| 155 | +import demo.controller.IndexController; |
| 156 | +
|
| 157 | +import java.io.IOException; |
| 158 | +
|
| 159 | +public class DemoHttpServer { |
| 160 | +
|
| 161 | + public static void main(String[] args) throws IOException { |
| 162 | + Json.setDefaultJsonFactory(new GsonFactory()); |
| 163 | + // 实例化Controller |
| 164 | + IndexController controller = new IndexController(); |
| 165 | +
|
| 166 | + // 手动添加路由 |
| 167 | + HttpRoutes simpleHttpRoutes = new SimpleHttpRoutes(); |
| 168 | + simpleHttpRoutes.add("/", controller::index); |
| 169 | + simpleHttpRoutes.add("/txt", controller::txt); |
| 170 | + simpleHttpRoutes.add("/json", controller::json); |
| 171 | + simpleHttpRoutes.add("/exception", controller::exception); |
| 172 | +
|
| 173 | + // 配置服务服务器 |
| 174 | + HttpConfig httpConfig; |
| 175 | + HttpRequestHandler requestHandler; |
| 176 | + HttpServerStarter httpServerStarter; |
| 177 | +
|
| 178 | + httpConfig = new HttpConfig(80, null, null, null); |
| 179 | + requestHandler = new SimpleHttpDispatcherHandler(httpConfig, simpleHttpRoutes); |
| 180 | + httpServerStarter = new HttpServerStarter(httpConfig, requestHandler); |
| 181 | + // 启动服务器 |
| 182 | + httpServerStarter.start(); |
| 183 | + } |
| 184 | +
|
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +实体类 |
| 189 | + |
| 190 | +``` |
| 191 | +package demo.model; |
| 192 | +
|
| 193 | +import lombok.AllArgsConstructor; |
| 194 | +import lombok.Data; |
| 195 | +import lombok.NoArgsConstructor; |
| 196 | +
|
| 197 | +@NoArgsConstructor |
| 198 | +@AllArgsConstructor |
| 199 | +@Data |
| 200 | +public class Hello { |
| 201 | + private String name; |
| 202 | + private String from; |
| 203 | +} |
| 204 | +
|
| 205 | +``` |
| 206 | + |
| 207 | +Controller |
| 208 | + |
| 209 | +``` |
| 210 | +package demo.controller; |
| 211 | +
|
| 212 | +import com.litongjava.tio.http.common.HttpRequest; |
| 213 | +import com.litongjava.tio.http.common.HttpResponse; |
| 214 | +import com.litongjava.tio.http.server.util.Resps; |
| 215 | +import demo.model.Hello; |
| 216 | +
|
| 217 | +public class IndexController { |
| 218 | +
|
| 219 | + public HttpResponse index(HttpRequest request) { |
| 220 | + return Resps.txt(request, "index"); |
| 221 | +
|
| 222 | + } |
| 223 | +
|
| 224 | + public HttpResponse txt(HttpRequest request) { |
| 225 | + return Resps.txt(request, "index"); |
| 226 | + } |
| 227 | +
|
| 228 | + public HttpResponse exception(HttpRequest request) { |
| 229 | + throw new RuntimeException("error"); |
| 230 | + } |
| 231 | +
|
| 232 | + public HttpResponse json(HttpRequest httpRequest) { |
| 233 | + Hello hello = new Hello("Tong Li", "Earth"); |
| 234 | + return Resps.json(httpRequest, hello); |
| 235 | + } |
| 236 | +} |
| 237 | +
|
| 238 | +``` |
| 239 | + |
| 240 | +maven and jdk |
| 241 | + |
| 242 | +``` |
| 243 | +mvn -version |
| 244 | +``` |
| 245 | + |
| 246 | +``` |
| 247 | +Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39) |
| 248 | +Maven home: /root/program/apache-maven-3.8.8 |
| 249 | +Java version: 21.0.1, vendor: Oracle Corporation, runtime: /root/program/graalvm-jdk-21.0.1+12.1 |
| 250 | +Default locale: en_US, platform encoding: UTF-8 |
| 251 | +OS name: "linux", version: "5.15.0-92-generic", arch: "amd64", family: "unix" |
| 252 | +``` |
| 253 | + |
| 254 | +打包成二进制文件 |
| 255 | + |
| 256 | +``` |
| 257 | +mvn package -DskipTests -Pnative |
| 258 | +``` |
| 259 | + |
| 260 | +打包过程中执使用的 native-image 命令如下 |
| 261 | + |
| 262 | +``` |
| 263 | +/root/program/graalvm-jdk-21.0.1+12.1/lib/svm/bin/native-image -cp /root/.m2/repository/com/litongjava/tio-http-server/3.7.3.v20240130-RELEASE/tio-http-server-3.7.3.v20240130-RELEASE.jar:/root/.m2/repository/com/litongjava/tio-http-common/3.7.3.v20240130-RELEASE/tio-http-common-3.7.3.v20240130-RELEASE.jar:/root/.m2/repository/com/litongjava/tio-core/3.7.3.v20240130-RELEASE/tio-core-3.7.3.v20240130-RELEASE.jar:/root/.m2/repository/com/litongjava/tio-utils/3.7.3.v20240130-RELEASE/tio-utils-3.7.3.v20240130-RELEASE.jar:/root/.m2/repository/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar:/root/.m2/repository/org/slf4j/slf4j-jdk14/1.7.31/slf4j-jdk14-1.7.31.jar:/root/.m2/repository/org/slf4j/slf4j-api/1.7.31/slf4j-api-1.7.31.jar:/root/code/java-ee-tio-boot-study/tio-http-server-study/tio-http-server-native-study/target/tio-http-server-graal.jar -H:+RemoveSaturatedTypeFlows --allow-incomplete-classpath --no-fallback -H:Class=demo.DemoHttpServer -H:Name=tio-http-server-native-study |
| 264 | +``` |
| 265 | + |
| 266 | +编译成功,编译过重中日志如下 |
| 267 | + |
| 268 | +``` |
| 269 | +======================================================================================================================== |
| 270 | +GraalVM Native Image: Generating 'tio-http-server-native-study' (executable)... |
| 271 | +======================================================================================================================== |
| 272 | +[1/8] Initializing... (8.9s @ 0.07GB) |
| 273 | + Java version: 21.0.1+12, vendor version: Oracle GraalVM 21.0.1+12.1 |
| 274 | + Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred |
| 275 | + C compiler: gcc (linux, x86_64, 9.4.0) |
| 276 | + Garbage collector: Serial GC (max heap size: 80% of RAM) |
| 277 | + 1 user-specific feature(s): |
| 278 | + - com.oracle.svm.thirdparty.gson.GsonFeature |
| 279 | +------------------------------------------------------------------------------------------------------------------------ |
| 280 | + 1 experimental option(s) unlocked: |
| 281 | + - '-H:Name' (alternative API option(s): -o tio-http-server-native-study; origin(s): command line) |
| 282 | +------------------------------------------------------------------------------------------------------------------------ |
| 283 | +Build resources: |
| 284 | + - 5.80GB of memory (75.6% of 7.67GB system memory, determined at start) |
| 285 | + - 4 thread(s) (100.0% of 4 available processor(s), determined at start) |
| 286 | +Found pending operations, continuing analysis. |
| 287 | +[2/8] Performing analysis... [******] (88.3s @ 0.61GB) |
| 288 | + 6,631 reachable types (83.0% of 7,986 total) |
| 289 | + 8,971 reachable fields (54.2% of 16,566 total) |
| 290 | + 33,828 reachable methods (56.6% of 59,732 total) |
| 291 | + 2,137 types, 123 fields, and 1,824 methods registered for reflection |
| 292 | + 60 types, 58 fields, and 55 methods registered for JNI access |
| 293 | + 4 native libraries: dl, pthread, rt, z |
| 294 | +[3/8] Building universe... (11.6s @ 0.69GB) |
| 295 | +[4/8] Parsing methods... [*****] (32.0s @ 0.76GB) |
| 296 | +[5/8] Inlining methods... [***] (4.6s @ 0.81GB) |
| 297 | +[6/8] Compiling methods... [*************] (184.0s @ 0.70GB) |
| 298 | +[7/8] Layouting methods... [***] (10.1s @ 0.74GB) |
| 299 | +[8/8] Creating image... [[8/8] Creating image... [***] (5.2s @ 0.89GB) |
| 300 | + 17.10MB (53.14%) for code area: 18,845 compilation units |
| 301 | + 13.83MB (42.98%) for image heap: 201,318 objects and 49 resources |
| 302 | + 1.25MB ( 3.88%) for other data |
| 303 | + 32.17MB in total |
| 304 | +------------------------------------------------------------------------------------------------------------------------ |
| 305 | +Top 10 origins of code area: Top 10 object types in image heap: |
| 306 | + 11.95MB java.base 4.63MB byte[] for code metadata |
| 307 | + 2.56MB svm.jar (Native Image) 2.48MB byte[] for java.lang.String |
| 308 | + 352.71kB java.rmi 1.40MB java.lang.String |
| 309 | + 265.40kB java.naming 1.12MB java.lang.Class |
| 310 | + 260.34kB tio-core-3.7.3.v20240130-RELEASE.jar 613.59kB byte[] for general heap data |
| 311 | + 249.64kB jdk.crypto.ec 373.98kB byte[] for reflection metadata |
| 312 | + 242.15kB gson-2.10.1.jar 310.83kB com.oracle.svm.core.hub.DynamicHubCompanion |
| 313 | + 171.04kB tio-utils-3.7.3.v20240130-RELEASE.jar 253.84kB java.util.HashMap$Node |
| 314 | + 162.92kB com.oracle.svm.svm_enterprise 193.38kB c.o.svm.core.hub.DynamicHub$ReflectionMetadata |
| 315 | + 155.25kB java.logging 191.68kB char[] |
| 316 | + 660.28kB for 20 more packages 2.30MB for 1787 more object types |
| 317 | + Use '-H:+BuildReport' to create a report with more details. |
| 318 | +------------------------------------------------------------------------------------------------------------------------ |
| 319 | +Security report: |
| 320 | + - Binary includes Java deserialization. |
| 321 | + - Use '--enable-sbom' to embed a Software Bill of Materials (SBOM) in the binary. |
| 322 | +------------------------------------------------------------------------------------------------------------------------ |
| 323 | +Recommendations: |
| 324 | + G1GC: Use the G1 GC ('--gc=G1') for improved latency and throughput. |
| 325 | + PGO: Use Profile-Guided Optimizations ('--pgo') for improved throughput. |
| 326 | + INIT: Adopt '--strict-image-heap' to prepare for the next GraalVM release. |
| 327 | + HEAP: Set max heap for improved and more predictable memory usage. |
| 328 | + CPU: Enable more CPU features with '-march=native' for improved performance. |
| 329 | +------------------------------------------------------------------------------------------------------------------------ |
| 330 | + 27.7s (8.0% of total time) in 365 GCs | Peak RSS: 1.74GB | CPU load: 3.06 |
| 331 | +------------------------------------------------------------------------------------------------------------------------ |
| 332 | +Produced artifacts: |
| 333 | + /root/code/java-ee-tio-boot-study/tio-http-server-study/tio-http-server-native-study/target/tio-http-server-native-study (executable) |
| 334 | +======================================================================================================================== |
| 335 | +Finished generating 'tio-http-server-native-study' in 5m 46s. |
| 336 | +[INFO] ------------------------------------------------------------------------ |
| 337 | +[INFO] BUILD SUCCESS |
| 338 | +[INFO] ------------------------------------------------------------------------ |
| 339 | +[INFO] Total time: 05:53 min |
| 340 | +[INFO] Finished at: 2024-01-31T10:22:24+08:00 |
| 341 | +[INFO] ------------------------------------------------------------------------ |
| 342 | +``` |
| 343 | + |
| 344 | +测试启动成功 |
| 345 | + |
| 346 | +``` |
| 347 | +./target/tio-http-server-native-study |
| 348 | +Jan 31, 2024 10:38:59 AM com.litongjava.tio.server.TioServer start |
| 349 | +INFO: |
| 350 | +|----------------------------------------------------------------------------------------| |
| 351 | +| t-io on gitee | https://gitee.com/ppnt/t-io | |
| 352 | +| t-io on github | https://github.com/litongjava/t-io | |
| 353 | +| ---------------------------------------------------------------------------------------| |
| 354 | +| TioConfig name | Tio Http Server | |
| 355 | +| Started at | 2024-01-31 10:38:59 | |
| 356 | +| Listen on | 0.0.0.0:80 | |
| 357 | +| Main Class | java.lang.invoke.LambdaForm$DMH/sa346b79c | |
| 358 | +| Jvm start time | 9ms | |
| 359 | +| Tio start time | 5ms | |
| 360 | +| Pid | 203800 | |
| 361 | +|----------------------------------------------------------------------------------------| |
| 362 | +``` |
| 363 | + |
| 364 | +访问测试接口 |
| 365 | + |
| 366 | +- http://localhost/ |
| 367 | +- http://localhost/txt |
| 368 | +- http://localhost/json |
| 369 | +- http://localhost/exception |
| 370 | + |
| 371 | +json 接口返回有时的{},其他接口正常,原因不明 |
0 commit comments