本节主要介绍如何通过 GraalVM 的 native-image 组件构建 ShardingSphere-Proxy 的 Native Image 和对应的 Docker Image
。
ShardingSphere Proxy 尚未准备好与 GraalVM Native Image 集成。 其在 https://github.com/apache/shardingsphere/actions/ 存在每日构建的任务用于测试构建。
若你发现构建过程存在缺失的 GraalVM Reachability Metadata, 应当在 https://github.com/oracle/graalvm-reachability-metadata 打开新的 issue , 并提交包含 ShardingSphere 自身或依赖的第三方库缺失的 GraalVM Reachability Metadata 的 PR。
ShardingSphere 的 master 分支尚未准备好处理 Native Image 中的单元测试,
需要等待 Junit 5 Platform 的集成,你总是需要在构建 GraalVM Native Image 的过程中,
加上特定于 GraalVM Native Build Tools 的 -DskipNativeTests 或 -DskipTests 参数跳过 Native Image 中的单元测试。
本节假定处于 Linux(amd64,aarch64), MacOS( amd64 )或 Windows(amd64)环境。 如果你位于 MacOS(aarch64/M1) 环境, 你需要关注尚未关闭的 https://github.com/oracle/graal/issues/2666 。
根据 https://www.graalvm.org/downloads/ 要求安装和配置 JDK 17 对应的 GraalVM CE 或 GraalVM EE。
同时可以通过 SDKMAN! 安装 JDK 17 对应的 GraalVM CE。
通过 GraalVM Updater 工具安装 native-image 组件。
根据 https://www.graalvm.org/22.2/reference-manual/native-image/#prerequisites 的要求安装本地工具链。
如果需要构建 Docker Image, 确保 docker-cli 在系统环境变量内。
情形一:不需要使用存在 SPI 实现的 JAR 或第三方依赖的 JAR
在 Git Source 同级目录下执行如下命令, 直接完成 Native Image 的构建。
./mvnw -am -pl shardingsphere-distribution/shardingsphere-proxy-native-distribution -B -Pnative -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Dspotless.apply.skip=true -Drat.skip=true clean package
情形二:需要使用存在 SPI 实现的 JAR 或 GPL V2 等 LICENSE 的第三方依赖的 JAR。
在 shardingsphere-distribution/shardingsphere-proxy-native-distribution/pom.xml 的 dependencies 加入存在 SPI 实现的 JAR
或第三方依赖的 JAR。示例如下
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.30</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>shardingsphere-sql-translator-jooq-provider</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>
./mvnw -am -pl shardingsphere-distribution/shardingsphere-proxy-native-distribution -B -Pnative -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Dspotless.apply.skip=true -Drat.skip=true clean package
server.yaml 的 /conf 文件夹,
假设已存在文件夹./custom/conf,示例为./apache-shardingsphere-proxy 3307 ./custom/conf
./mvnw -am -pl shardingsphere-distribution/shardingsphere-proxy-native-distribution -B -Pnative,docker.native -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Dspotless.apply.skip=true -Drat.skip=true clean package
server.yaml 的 conf 文件夹为 ./custom/conf,可通过如下的 docker-compose.yml 文件启动 GraalVM Native
Image 对应的 Docker Image。version: "3.8"
services:
  apache-shardingsphere-proxy-native:
    image: apache/shardingsphere-proxy-native:latest
    volumes:
      - ./custom/conf:/conf
    ports:
      - "3307:3307"
shardingsphere-distribution/shardingsphere-proxy-native-distribution/Dockerfile
使用 scratch 作为 base docker image。
但如果您主动为pom.xml的native profile添加jvmArgs为-H:+StaticExecutableWithDynamicLibC,
以静态链接除 glic 之外的所有内容,您应该切换 base image 到 busybox:glic。
参考 https://www.graalvm.org/22.2/reference-manual/native-image/guides/build-static-executables/ 。
另请注意,某些第三方依赖将需要更多系统库,例如 libdl。
因此请确保根据您的使用情况调整 base docker image 和shardingsphere-distribution/shardingsphere-proxy-native-distribution
下的 pom.xml 和 Dockerfile 的内容。