Cloud Native Buildpacks/Paketo with Java/Spring Boot:如何配置不同的 JDK 下载 uri(例如无法访问 github.com)

Cloud Native Buildpacks/Paketo with Java/Spring Boot: How to configure different JDK download uri (e.g. no access to github.com)

有一个 Spring 引导应用程序,我尝试使用 spring-boot-maven-plugin 目标 mvn spring-boot:build-image 构建它。但是构建无法从 github.com 下载 bellsoft-jre11.0.9.1+1-linux-amd64.tar.gz,因为我无法从我的构建管道访问它:

...
Paketo BellSoft Liberica Buildpack 5.2.1
  https://github.com/paketo-buildpacks/bellsoft-liberica
  Build Configuration:
    $BP_JVM_VERSION              11.0.9          the Java version
  Launch Configuration:
    $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
    $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
    $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
    $JAVA_TOOL_OPTIONS                           the JVM launch flags
  BellSoft Liberica JDK 11.0.9: Contributing to layer
    Downloading from https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
unable to invoke layer creator
unable to get dependency jdk
unable to download https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
unable to request https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz
ERROR: failed to build: exit status 1

有没有一种方法可以将 bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 下载到我的构建管道可访问的位置并配置 bellsoft-liberica buildpack 来使用它?

According to the docs:

Paketo Buildpacks may download dependencies from the internet. For example, the Java Buildpack will download the BellSoft Liberica JRE from the Liberica github releases by default. If a dependency URI is inaccessible from the build environment, a binding can be used to map a new URI to a given dependency.

使用 spring-boot-maven-plugin(或 Gradle 插件)配置绑定需要 Spring Boot 2.5+。如果您使用的是旧版本,则需要升级或切换到 pack CLI.

=== 使用带绑定的 pack CLI 配置不同的 JDK 下载 uri ===

The pack docs告诉我们绑​​定目录的总体布局(/platform/bindings稍后在pack构建容器中创建):

/chooseYourBindingsName
├── key-name-of-our-buildpacks-binding-configuration
└── type-name-of-our-buildpacks-binding-configuration

1.创建绑定目录

所以让我们尝试创建一个完整的 运行 示例!为了将绑定配置移交给pack CLI,我们需要先创建一个目录:

mkdir bellsoft-jdk-config && cd bellsoft-jdk-config

2。创建文件类型,包含绑定键

现在我们需要在这个目录中创建一个名为 type 的文件,其中包含 the bellsoft-liberica binding type dependency-mapping:

的绑定密钥
echo "dependency-mapping" >> type

新文件 type 应该存在于包含字符串 dependency-mapping.

的目录中

3。从buildpack.toml

中选择JDK版本

因为我们想要更改 JDK 的 bellsoft-liberica 下载 uri,我们需要决定我们最想使用哪个 JDK 版本。 bellsoft-liberica buildpack 的 buildpack.toml 概述了 buildpack 中可用的 JRE/JDK 版本。对于此处的示例,我使用了最新的 JDK 版本 11,它在 buildpack.toml 中配置如下:

...

[[metadata.dependencies]]
id      = "jdk"
name    = "BellSoft Liberica JDK"
version = "11.0.9"
uri     = "https://github.com/bell-sw/Liberica/releases/download/11.0.9.1+1/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz"
sha256  = "786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502"
stacks  = [ "io.buildpacks.stacks.bionic", "org.cloudfoundry.stacks.cflinuxfs3" ]
...

4.下载 JDK

确定版本后,我们需要从 uri 字段中提供的位置下载 JDK 到我们稍后可以在构建环境中访问的位置(因为我们无法访问 github.com)。假设,我们已经下载了 JDK 并在 http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz.

可用

5.创建名为 sha256 的文件,包含 JDK uri

现在我们应该创建另一个 文件,该文件完全根据 JDK 版本的 [[metadata.dependencies]] 部分的 sha256 摘要值 命名选择里面的buildpack.toml。这个文件必须包含我们下载的uri JDK:

echo "http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz" >> 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502

最后我们的目录 bellsoft-jdk-config 应该符合 the pack CLI bindings directory docs 并且看起来像这样:

/bellsoft-jdk-config
├── 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502
└── type

6.使用 --volume 执行 pack CLI 以进行绑定 & BP_JVM_VERSION

我们终于可以发出 pack CLI 命令了。确保 pack CLI 安装在您的系统上。还要确保使用 --env BP_JVM_VERSION=exactJDKversionNumberHere 环境变量配置 提供准确的 JDK 版本号,它与您下载的 JDK 版本和buildpack.toml:

pack build your-application-name-here \
    --path . \
    --volume $(pwd)/bellsoft-jdk-config:/platform/bindings/bellsoft-jdk-config \
    --env BP_JVM_VERSION=11.0.9 \
    --builder paketobuildpacks/builder:base

现在 bellsoft-liberica 构建包将从 http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz:

下载 JDK tar.gz
...
Paketo BellSoft Liberica Buildpack 5.2.1
  https://github.com/paketo-buildpacks/bellsoft-liberica
  Build Configuration:
    $BP_JVM_VERSION              11.0.9          the Java version
  Launch Configuration:
    $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
    $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
    $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
    $JAVA_TOOL_OPTIONS                           the JVM launch flags
  BellSoft Liberica JDK 11.0.9: Contributing to layer
    Downloading from http://your-accessible-uri-to/bellsoft-jdk11.0.9.1+1-linux-amd64.tar.gz 
...

我创建绑定:

/bindings/bellsoft-jdk-config
├── 786c48fa6429d6a3f0afb189a65f0a43772e42afbab836852b9a1fdfdb8fc502
├── a3092627b082cb3cdbbe4b255d35687126aa604e6b613dcda33be9f7e1277162
├── be27df8838a6d069a2212de5f46da4e39f33f087f2e77c8a725d0f7ec8b5273e
├── d9ff2d84528a2154ff669b85e6dbdee7f244194dcc64e0a8a1bedc470b3bcf56
└── type

然后创建了一个复制这些绑定的 Dockerfile,并在之前的基础上构建了一个新平台:

FROM paketobuildpacks/builder:0.0.464-base-platform-api-0.3
 
COPY bindings /platform/bindings
 
CMD ["/bin/bash"]
docker build -t your-repo-url/java-builder-test:1 .
docker push your-repo-url/java-builder-test:1

然后我配置了 spring 插件来使用这个平台:

<configuration>
    <imageBuilder>your-repo-url/java-builder-test:1</imageBuilder>
    <layers>
        <enabled>true</enabled>
    </layers>
    <image>
        <name>your-repo-url/${project.artifactId}:${project.version}</name>
    </image>
</configuration>

此解决方法有效,但您必须使用正确的目录权限。

我将 pom.xml 中的 Java 版本更改为 17:

<properties>
    <java.version>17</java.version>
    <spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>

我将 pom.xml 文件中的 java 版本更改为 17,它成功了!

更改 pom.xml 中的 Java 版本,它将起作用。我已将其更改为 java 17。它对我有用。