zoukankan      html  css  js  c++  java
  • graalvm native image 试用

    graalvm 提供的native 模式,可以加速应用的启动,不同可以让应用不再依赖jvm 运行时环境,但是
    也有一些限制 参考 https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md

    项目准备

    • 项目结构
     
    ├── Dockerfile
    ├── pom.xml
    └── src
        ├── main
        ├── java
        └── com
        └── dalong
        └── Application.java
        └── resources
        └── test
            └── java
     
     
    • maven pom.xml
      代码是一个简单的调用js 的代码,使用了graalvm 的polyglot 特性
      同时包含了native-image 的maven plugin 方便生成native 模式的可运行软件
     
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.dalong</groupId>
        <artifactId>js-exec</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <encoding>UTF-8</encoding>
            <java.version>1.8</java.version>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.graalvm.js</groupId>
                <artifactId>js</artifactId>
                <version>19.2.1</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>native-image-js</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.dalong.Application</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.oracle.substratevm</groupId>
                    <artifactId>native-image-maven-plugin</artifactId>
                    <version>19.2.0.1</version>
                    <configuration>
                        <buildArgs>--language:js</buildArgs>
                        <imageName>${project.artifactId}</imageName>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>native-image</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    • 代码
      Application.java
     
    package com.dalong;
    import org.graalvm.polyglot.*;
    /**
     * @author dalong
     */
    public class Application {
        /**
         * main for test graalvm exec js
         * @param args
         */
        public static void main(String[] args) {
            System.out.println("Hello Java!");
            try (Context context = Context.create()) {
                context.eval("js", "print('Hello JavaScript!');");
            }
        }
    } 
    • dockerfile
      注意此构建是通过本机模式生成的(linux),我们也可以使用docker multi stage 的模式构建
     
    FROM debian:9-slim
    LABEL maintainer="dalongrong"
    LABEL email = "1141591465@qq.com"
    COPY target/js-exec /js-exec
    CMD [ "/js-exec" ]

    构建&&测试

    • 本地构建

      注意需要安装graalvm 可以通过sdkman 安装

    mvn clean package

    效果

    [INFO] ImageClasspath Entry: org.graalvm.js:js:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/js/js/19.2.1/js-19.2.1.jar)
    [INFO] ImageClasspath Entry: org.graalvm.regex:regex:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/regex/regex/19.2.1/regex-19.2.1.jar)
    [INFO] ImageClasspath Entry: org.graalvm.truffle:truffle-api:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/truffle/truffle-api/19.2.1/truffle-api-19.2.1.jar)
    [INFO] ImageClasspath Entry: org.graalvm.sdk:graal-sdk:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/sdk/graal-sdk/19.2.1/graal-sdk-19.2.1.jar)
    [INFO] ImageClasspath Entry: org.ow2.asm:asm:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm/6.2.1/asm-6.2.1.jar)
    [INFO] ImageClasspath Entry: org.ow2.asm:asm-tree:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-tree/6.2.1/asm-tree-6.2.1.jar)
    [INFO] ImageClasspath Entry: org.ow2.asm:asm-analysis:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-analysis/6.2.1/asm-analysis-6.2.1.jar)
    [INFO] ImageClasspath Entry: org.ow2.asm:asm-commons:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-commons/6.2.1/asm-commons-6.2.1.jar)
    [INFO] ImageClasspath Entry: org.ow2.asm:asm-util:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-util/6.2.1/asm-util-6.2.1.jar)
    [INFO] ImageClasspath Entry: com.ibm.icu:icu4j:jar:62.1:compile (file:///Users/dalong/.m2/repository/com/ibm/icu/icu4j/62.1/icu4j-62.1.jar)
    [INFO] ImageClasspath Entry: com.dalong:js-exec:jar:1.0-SNAPSHOT (file:///Users/dalong/quarkus-cloud-native/quarkus-quickstarts/jsexec/target/native-image-js.jar)
    [WARNING] Version mismatch between native-image-maven-plugin (19.2.0.1) and native-image executable (19.2.1)
    [INFO] Executing: /Users/dalong/.sdkman/candidates/java/19.2.1-grl/jre/bin/native-image -cp /Users/dalong/.m2/repository/org/graalvm/js/js/19.2.1/js-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/regex/regex/19.2.1/regex-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/truffle/truffle-api/19.2.1/truffle-api-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/sdk/graal-sdk/19.2.1/graal-sdk-19.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm/6.2.1/asm-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-tree/6.2.1/asm-tree-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-analysis/6.2.1/asm-analysis-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-commons/6.2.1/asm-commons-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-util/6.2.1/asm-util-6.2.1.jar:/Users/dalong/.m2/repository/com/ibm/icu/icu4j/62.1/icu4j-62.1.jar:/Users/dalong/quarkus-cloud-native/quarkus-quickstarts/jsexec/target/native-image-js.jar --language:js -H:Class=com.dalong.Application -H:Name=js-exec
    Build on Server(pid: 88272, port: 60056)*
    [js-exec:88272] classlist: 5,881.54 ms
    [js-exec:88272] (cap): 4,853.08 ms
    [js-exec:88272] setup: 7,128.30 ms
    [js-exec:88272] (typeflow): 32,678.52 ms
    [js-exec:88272] (objects): 19,067.05 ms
    [js-exec:88272] (features): 10,743.39 ms
    [js-exec:88272] analysis: 65,674.81 ms
    [js-exec:88272] (clinit): 1,198.02 ms
    8711 method(s) included for runtime compilation
    [js-exec:88272] universe: 4,099.50 ms
    [js-exec:88272] (parse): 8,269.88 ms
    [js-exec:88272] (inline): 8,757.03 ms
    [js-exec:88272] (compile): 57,386.27 ms
    [js-exec:88272] compile: 78,905.65 ms
    [js-exec:88272] image: 13,244.40 ms
    [js-exec:88272] write: 3,109.99 ms
    [js-exec:88272] [total]: 179,773.77 ms
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 03:10 min
    [INFO] Finished at: 2019-11-20T19:34:36+08:00
    [INFO] ---------------------------------------------
    • 运行效果
    ./target/js-exec 
    Hello Java!
    Hello JavaScript!

    参考资料

    https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md
    https://www.graalvm.org/docs/reference-manual/native-image/

  • 相关阅读:
    体温填报APP--流程设计
    构建之法阅读笔记(一)
    家庭记账本(七)
    家庭记账本(六)
    家庭记账本(五)
    家庭记账本(四)
    家庭记账本(三)
    家庭记账本(二)
    家庭记账本(一)
    20210207 BaseAdapter
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11900331.html
Copyright © 2011-2022 走看看