本文主要研究怎么在docker的java9镜像上运行springboot2并精简jdk.

maven

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RC2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>9</java.version>
        <start-class>com.example.demo.DemoApplication</start-class>
    </properties>
注意springboot得2版本才能支持java9,另外这个java.version设置为9
这里maven用的版本是3.3.3

mvn package

mvn clean package -Dmaven.test.skip=true
//......
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by lombok.javac.apt.LombokProcessor to field com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs
WARNING: Please consider reporting this to the maintainers of lombok.javac.apt.LombokProcessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
可以看到这里可以编译成功,但是有WARNING

docker构建

Dockerfile

FROM dekstroza/openjdk9-alpine as packager

# First stage: JDK 9 with modules required for Spring Boot
RUN /opt/jdk-9/bin/jlink \
    --module-path /opt/jdk-9/jmods \
    --verbose \
    --add-modules java.base,java.logging,java.xml,jdk.unsupported,java.sql,java.desktop,java.management,java.naming,java.instrument \
    --compress 2 \
    --no-header-files \
    --output /opt/jdk-9-minimal

# Second stage, add only our custom jdk9 distro and our app
FROM alpine:3.6
COPY --from=packager /opt/jdk-9-minimal /opt/jdk-9-minimal
ENV JAVA_HOME=/opt/jdk-9-minimal
ENV PATH="$PATH:$JAVA_HOME/bin"

## copy app.jar
VOLUME /tmp
ADD app.jar /app.jar
#RUN sh -c 'touch /app.jar'

EXPOSE 8080
ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=${PROFILE} -jar /app.jar
这里使用的是alpine镜像,应为它比较小,基础镜像就几M,相比ubuntu等要小很多。由于Alpine Linux使用MUSL作为标准C库,而openjdk依赖于GNU标准C库(gclib),因此需要alpine版的jdk9才可以在alpine上运行,但是目前jdk 9还没有正式的alpine镜像,只有有一个early access的版Announcing: Early-Access builds of JDK 9 for Alpine Linux/musl

这里我们使用dekstroza基于alpine3.6构建好的镜像dekstroza/openjdk9-alpine作为jlink的基础镜像,然后确定好工程依赖的jmods(下面的内容会讲怎么确定),然后使用jlink构建最小的jdk运行环境.

这里的--module-path(-p)指定依赖的模块路径;--add-modules添加root module用于分析依赖;--compress指定压缩方式,0为不压缩,1为常量字符串共享,2为zip压缩;--no-header-files指定排除头文件;--verbose用于开启trace输出。

构建并运行

mvn clean package -Dmaven.test.skip=true
cd target
cp ../src/main/docker/Dockerfile .
docker build -t springboot2-java9-demo .
docker run --rm -p 8080:8080 \
--name springboot2-java9-demo \
-e PROFILE=default \
springboot2-java9-demo

确定所依赖的jmod

这个目前来说还没找到现成命令/工具可以动态找出工程依赖的jmod.不过可以自己通过jdeps命令构建shell脚本来实现

copy-dependencies

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
这里通过copy-dependencies插件把依赖jar存放到classes/lib目录下,然后使用jdeps指定这个class-path来分析app.jar(这样做的目的主要是springboot打包出来的是fatjar,相关jar都在fatjar中,不好指定class-path)

jdeps recursive summary

➜  target git:(master) ✗ jdeps --class-path 'classes/lib/*' -recursive -summary app.jar
拆分程序包: javax.annotation [jrt:/java.xml.ws.annotation, classes/lib/javax.annotation-api-1.3.1.jar, classes/lib/jsr305-1.3.9.jar]

app.jar -> classes/lib/commons-lang3-3.7.jar
app.jar -> classes/lib/guava-24.0-jre.jar
app.jar -> java.base
app.jar -> java.logging
app.jar -> classes/lib/reactive-streams-1.0.2.jar
app.jar -> classes/lib/reactor-core-3.1.4.RELEASE.jar
app.jar -> classes/lib/rest-common-0.0.2.jar
app.jar -> classes/lib/slf4j-api-1.7.25.jar
app.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
app.jar -> classes/lib/spring-boot-2.0.0.RC2.jar
app.jar -> classes/lib/spring-boot-autoconfigure-2.0.0.RC2.jar
app.jar -> classes/lib/spring-context-5.0.4.RELEASE.jar
app.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
app.jar -> classes/lib/spring-web-5.0.4.RELEASE.jar
app.jar -> classes/lib/spring-webflux-5.0.4.RELEASE.jar
checker-compat-qual-2.0.0.jar -> java.base
commons-lang3-3.7.jar -> java.base
error_prone_annotations-2.1.3.jar -> java.base
guava-24.0-jre.jar -> classes/lib/checker-compat-qual-2.0.0.jar
guava-24.0-jre.jar -> classes/lib/error_prone_annotations-2.1.3.jar
guava-24.0-jre.jar -> java.base
guava-24.0-jre.jar -> java.logging
logback-classic-1.2.3.jar -> java.base
logback-classic-1.2.3.jar -> java.management
logback-classic-1.2.3.jar -> java.naming
logback-classic-1.2.3.jar -> java.xml
logback-classic-1.2.3.jar -> jdk.unsupported
logback-classic-1.2.3.jar -> classes/lib/logback-core-1.2.3.jar
logback-classic-1.2.3.jar -> classes/lib/slf4j-api-1.7.25.jar
logback-classic-1.2.3.jar -> 找不到
logback-core-1.2.3.jar -> java.base
logback-core-1.2.3.jar -> java.xml
logback-core-1.2.3.jar -> 找不到
netty-buffer-4.1.21.Final.jar -> java.base
netty-buffer-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-codec-4.1.21.Final.jar -> java.base
netty-codec-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-codec-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-codec-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-codec-4.1.21.Final.jar -> 找不到
netty-codec-http-4.1.21.Final.jar -> java.base
netty-codec-http-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-codec-http-4.1.21.Final.jar -> classes/lib/netty-codec-4.1.21.Final.jar
netty-codec-http-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-codec-http-4.1.21.Final.jar -> classes/lib/netty-handler-4.1.21.Final.jar
netty-codec-http-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-codec-socks-4.1.21.Final.jar -> java.base
netty-codec-socks-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-codec-socks-4.1.21.Final.jar -> classes/lib/netty-codec-4.1.21.Final.jar
netty-codec-socks-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-codec-socks-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-common-4.1.21.Final.jar -> java.base
netty-common-4.1.21.Final.jar -> java.logging
netty-common-4.1.21.Final.jar -> jdk.unsupported
netty-common-4.1.21.Final.jar -> classes/lib/slf4j-api-1.7.25.jar
netty-common-4.1.21.Final.jar -> 找不到
netty-handler-4.1.21.Final.jar -> java.base
netty-handler-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-handler-4.1.21.Final.jar -> classes/lib/netty-codec-4.1.21.Final.jar
netty-handler-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-handler-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-handler-4.1.21.Final.jar -> 找不到
netty-handler-proxy-4.1.21.Final.jar -> java.base
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-codec-4.1.21.Final.jar
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-codec-http-4.1.21.Final.jar
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-codec-socks-4.1.21.Final.jar
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-handler-proxy-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-resolver-4.1.21.Final.jar -> java.base
netty-resolver-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-transport-4.1.21.Final.jar -> java.base
netty-transport-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-transport-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-transport-4.1.21.Final.jar -> classes/lib/netty-resolver-4.1.21.Final.jar
netty-transport-native-epoll-4.1.21.Final.jar -> java.base
netty-transport-native-epoll-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-transport-native-epoll-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-transport-native-epoll-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
netty-transport-native-epoll-4.1.21.Final.jar -> classes/lib/netty-transport-native-unix-common-4.1.21.Final.jar
netty-transport-native-unix-common-4.1.21.Final.jar -> java.base
netty-transport-native-unix-common-4.1.21.Final.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
netty-transport-native-unix-common-4.1.21.Final.jar -> classes/lib/netty-common-4.1.21.Final.jar
netty-transport-native-unix-common-4.1.21.Final.jar -> classes/lib/netty-transport-4.1.21.Final.jar
reactive-streams-1.0.2.jar -> java.base
reactor-core-3.1.4.RELEASE.jar -> java.base
reactor-core-3.1.4.RELEASE.jar -> java.logging
reactor-core-3.1.4.RELEASE.jar -> classes/lib/reactive-streams-1.0.2.jar
reactor-core-3.1.4.RELEASE.jar -> classes/lib/slf4j-api-1.7.25.jar
reactor-core-3.1.4.RELEASE.jar -> 找不到
reactor-netty-0.7.4.RELEASE.jar -> java.base
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-codec-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-codec-http-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-common-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-handler-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-handler-proxy-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-resolver-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-transport-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/netty-transport-native-epoll-4.1.21.Final.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/reactive-streams-1.0.2.jar
reactor-netty-0.7.4.RELEASE.jar -> classes/lib/reactor-core-3.1.4.RELEASE.jar
reactor-netty-0.7.4.RELEASE.jar -> 找不到
rest-common-0.0.2.jar -> java.base
slf4j-api-1.7.25.jar -> java.base
slf4j-api-1.7.25.jar -> classes/lib/logback-classic-1.2.3.jar
spring-aop-5.0.4.RELEASE.jar -> java.base
spring-aop-5.0.4.RELEASE.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
spring-aop-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-aop-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-beans-5.0.4.RELEASE.jar -> java.base
spring-beans-5.0.4.RELEASE.jar -> java.desktop
spring-beans-5.0.4.RELEASE.jar -> java.xml
spring-beans-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-beans-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-beans-5.0.4.RELEASE.jar -> 找不到
spring-boot-2.0.0.RC2.jar -> java.base
spring-boot-2.0.0.RC2.jar -> java.desktop
spring-boot-2.0.0.RC2.jar -> java.management
spring-boot-2.0.0.RC2.jar -> java.xml
spring-boot-2.0.0.RC2.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
spring-boot-2.0.0.RC2.jar -> classes/lib/spring-context-5.0.4.RELEASE.jar
spring-boot-2.0.0.RC2.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-boot-2.0.0.RC2.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-boot-2.0.0.RC2.jar -> classes/lib/spring-web-5.0.4.RELEASE.jar
spring-boot-2.0.0.RC2.jar -> 找不到
spring-boot-autoconfigure-2.0.0.RC2.jar -> java.base
spring-boot-autoconfigure-2.0.0.RC2.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
spring-boot-autoconfigure-2.0.0.RC2.jar -> classes/lib/spring-boot-2.0.0.RC2.jar
spring-boot-autoconfigure-2.0.0.RC2.jar -> classes/lib/spring-context-5.0.4.RELEASE.jar
spring-boot-autoconfigure-2.0.0.RC2.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-boot-autoconfigure-2.0.0.RC2.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> java.base
spring-context-5.0.4.RELEASE.jar -> java.desktop
spring-context-5.0.4.RELEASE.jar -> java.instrument
spring-context-5.0.4.RELEASE.jar -> java.management
spring-context-5.0.4.RELEASE.jar -> java.naming
spring-context-5.0.4.RELEASE.jar -> java.xml
spring-context-5.0.4.RELEASE.jar -> java.xml.ws
spring-context-5.0.4.RELEASE.jar -> java.xml.ws.annotation
spring-context-5.0.4.RELEASE.jar -> classes/lib/spring-aop-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> classes/lib/spring-expression-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-context-5.0.4.RELEASE.jar -> 找不到
spring-core-5.0.4.RELEASE.jar -> java.base
spring-core-5.0.4.RELEASE.jar -> java.desktop
spring-core-5.0.4.RELEASE.jar -> java.sql
spring-core-5.0.4.RELEASE.jar -> java.xml
spring-core-5.0.4.RELEASE.jar -> jdk.unsupported
spring-core-5.0.4.RELEASE.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
spring-core-5.0.4.RELEASE.jar -> classes/lib/netty-common-4.1.21.Final.jar
spring-core-5.0.4.RELEASE.jar -> classes/lib/reactive-streams-1.0.2.jar
spring-core-5.0.4.RELEASE.jar -> classes/lib/reactor-core-3.1.4.RELEASE.jar
spring-core-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-core-5.0.4.RELEASE.jar -> 找不到
spring-expression-5.0.4.RELEASE.jar -> java.base
spring-expression-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-expression-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-jcl-5.0.4.RELEASE.jar -> java.base
spring-jcl-5.0.4.RELEASE.jar -> java.logging
spring-jcl-5.0.4.RELEASE.jar -> classes/lib/slf4j-api-1.7.25.jar
spring-jcl-5.0.4.RELEASE.jar -> 找不到
spring-web-5.0.4.RELEASE.jar -> java.base
spring-web-5.0.4.RELEASE.jar -> classes/lib/netty-buffer-4.1.21.Final.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/netty-codec-http-4.1.21.Final.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/netty-transport-4.1.21.Final.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/reactive-streams-1.0.2.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/reactor-core-3.1.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/reactor-netty-0.7.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/spring-beans-5.0.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/spring-context-5.0.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar -> 找不到
spring-webflux-5.0.4.RELEASE.jar -> java.base
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/reactive-streams-1.0.2.jar
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/reactor-core-3.1.4.RELEASE.jar
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/spring-context-5.0.4.RELEASE.jar
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/spring-core-5.0.4.RELEASE.jar
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/spring-jcl-5.0.4.RELEASE.jar
spring-webflux-5.0.4.RELEASE.jar -> classes/lib/spring-web-5.0.4.RELEASE.jar
由于classes/lib/log4j-api-2.10.0.jar是multi-release的需特殊指定multi-release 9,但是由于使用的是recursive,因此这里先临时删掉,再分析
通过输出找出java,jdk开头的,去重之后得到所需的jmods
java.base,java.logging,java.xml,jdk.unsupported,java.sql,java.desktop,java.management,java.naming,java.instrument

jdk-internals

➜  target git:(master) ✗ jdeps --class-path 'classes/lib/*' -recursive --jdk-internals  app.jar
拆分程序包: javax.annotation [jrt:/java.xml.ws.annotation, classes/lib/javax.annotation-api-1.3.1.jar, classes/lib/jsr305-1.3.9.jar]

fastjson-1.2.35.jar -> java.base
   com.alibaba.fastjson.serializer.AnnotationSerializer -> sun.reflect.annotation.AnnotationType              JDK internal API (java.base)
guava-24.0-jre.jar -> jdk.unsupported
   com.google.common.cache.Striped64                  -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.cache.Striped64$1                -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.cache.Striped64$Cell             -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.LittleEndianByteArray$UnsafeByteArray -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.LittleEndianByteArray$UnsafeByteArray$1 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.LittleEndianByteArray$UnsafeByteArray$2 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.LittleEndianByteArray$UnsafeByteArray$3 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.Striped64                   -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.Striped64$1                 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.hash.Striped64$Cell              -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.primitives.UnsignedBytes$LexicographicalComparatorHolder$UnsafeComparator -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.primitives.UnsignedBytes$LexicographicalComparatorHolder$UnsafeComparator$1 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper$1 -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
logback-classic-1.2.3.jar -> jdk.unsupported
   ch.qos.logback.classic.spi.PackagingDataCalculator -> sun.reflect.Reflection                             JDK internal API (jdk.unsupported)
lombok-1.16.20.jar -> jdk.compiler
   lombok.javac.apt.Processor                         -> com.sun.tools.javac.processing.JavacFiler          JDK internal API (jdk.compiler)
   lombok.javac.apt.Processor                         -> com.sun.tools.javac.processing.JavacProcessingEnvironment JDK internal API (jdk.compiler)
   lombok.javac.apt.Processor                         -> com.sun.tools.javac.util.Context                   JDK internal API (jdk.compiler)
   lombok.javac.apt.Processor                         -> com.sun.tools.javac.util.Options                   JDK internal API (jdk.compiler)
netty-common-4.1.21.Final.jar -> jdk.unsupported
   io.netty.util.internal.CleanerJava9                -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.PlatformDependent$Mpsc$1    -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.PlatformDependent0          -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.PlatformDependent0$1        -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.PlatformDependent0$2        -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.PlatformDependent0$3        -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.BaseLinkedQueueConsumerNodeRef -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.BaseLinkedQueueProducerNodeRef -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueColdProducerFields -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueConsumerFields -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.BaseMpscLinkedArrayQueueProducerFields -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.LinkedQueueNode -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueConsumerIndexField -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueProducerIndexField -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueueProducerLimitField -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.util.UnsafeAccess -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
netty-handler-4.1.21.Final.jar -> java.base
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.util.ObjectIdentifier                 JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.AlgorithmId                      JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateAlgorithmId           JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateIssuerName            JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateSerialNumber          JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateSubjectName           JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateValidity              JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateVersion               JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.CertificateX509Key               JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.X500Name                         JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.X509CertImpl                     JDK internal API (java.base)
   io.netty.handler.ssl.util.OpenJdkSelfSignedCertGenerator -> sun.security.x509.X509CertInfo                     JDK internal API (java.base)
objenesis-2.6.jar -> jdk.unsupported
   org.objenesis.instantiator.sun.UnsafeFactoryInstantiator -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   org.objenesis.instantiator.util.ClassDefinitionUtils -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   org.objenesis.instantiator.util.UnsafeUtils        -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
reactor-core-3.1.4.RELEASE.jar -> jdk.unsupported
   reactor.core.publisher.MultiProducerRingBuffer     -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   reactor.core.publisher.RingBuffer                  -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   reactor.core.publisher.RingBufferFields            -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   reactor.core.publisher.UnsafeSequence              -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   reactor.core.publisher.UnsafeSupport               -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
spring-core-5.0.4.RELEASE.jar -> jdk.unsupported
   org.springframework.objenesis.instantiator.sun.UnsafeFactoryInstantiator -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   org.springframework.objenesis.instantiator.util.ClassDefinitionUtils -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)
   org.springframework.objenesis.instantiator.util.UnsafeUtils -> sun.misc.Unsafe                                    JDK internal API (jdk.unsupported)

警告: 不支持 JDK 内部 API, 它们专用于通过不兼容方式来
删除或更改的 JDK 实现, 可能会损坏您的应用程序。
请修改您的代码, 消除与任何 JDK 内部 API 的相关性。
有关 JDK 内部 API 替换的最新更新, 请查看:
https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

JDK 内部 API                               建议的替换
----------                               -----
com.sun.tools.javac.processing.JavacFiler Use javax.tools and javax.lang.model @since 1.6
com.sun.tools.javac.processing.JavacProcessingEnvironment Use javax.tools and javax.lang.model @since 1.6
com.sun.tools.javac.util.Context         Use javax.tools and javax.lang.model @since 1.6
com.sun.tools.javac.util.Options         Use javax.tools and javax.lang.model @since 1.6
sun.misc.Unsafe                          See http://openjdk.java.net/jeps/260
sun.reflect.Reflection                   Use java.lang.StackWalker @since 9
sun.reflect.annotation.AnnotationType    Removed. See http://openjdk.java.net/jeps/260
sun.security.x509.X500Name               Use javax.security.auth.x500.X500Principal @since 1.4
可以发现netty,netty等还使用了sun.misc.Unsafe,这个被归到了jdk.unsupported,顾名思义就是不建议用户程序调用,它原本只想让oracle jdk team来使用.

小结

一个jdk8的镜像,使用ubuntu可能要用到六七百M,使用alpine的话要大概200M.通过jlink之后这个例子的java9大小为63.22M,加上springboot2的fatjar一共87.54M.

目前本文所用的方式还有诸多不足,主要有如下几点:

  • 使用MUSL编译版本的openjdk目前只有Early-Access build版本还没有正式发布
  • dockerfile指定的jmod有待通过脚本去解析依赖jar包动态确定
  • springboot工程及诸多第三方类库还没有使用java9的模块系统,虽然java9通过unnamed module来支持java9以前的jar包,但迁移到java9毕竟是最好的
  • maven相关的plugin比如jlink,jmod目前还只是pre-release,还没有正式发布
jdk9的周边设施还有待进一步完善

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论