Java 11 在 AWS beanstalk 上用于 Spring 引导项目

Java 11 on AWS beanstalk for Spring boot project

我是 Spring Boot 的新手,我正在尝试开发一个应用程序以便稍后将其部署到 AWS beanstalk 上。 我开始这个项目使用 java 11 但后来我发现 AWS 只支持 java 8。 是否可以将 pom.xml 中的 'maven.compiler.target' 设置为 1.8 以使其正确 运行 ? 我是否必须使用 Java 1.8 进行开发和编译? 我想使用新的 Java 功能和库。 如果有人有同样的问题,我想发表一些意见。 谢谢。 镉

当您编译具有特定 Java 版本的 Java 项目时,您只能 运行 使用比您以前使用的版本更高(或等于)的版本编译它。不能做相反的事情,至少如果您使用的是更高版本中存在的语言功能,则不能。

例如您不能使用 Java 11 中的功能,而是 运行 Java 8

中的应用程序

既然你正在使用 Java 11 为什么不利用 Java 和 Elastic Bean Stalks docker 支持并使用 JDK11 创建一个 docker 图像并使用它部署?

如果您选择不走这条路,并且想要以较低版本的 java 为目标,以将弹性 beantalk 与 Java 8 一起使用,您可以尝试这样的事情。

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>11</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs. In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch

请记住,如果您在 Java 8 上编译和 运行 您的代码,则不能使用已添加到 Java 标准中的 类 Java 11 中的库,因为那些不会出现在 Java 8 中。 link

搜索时发现可能会支持OpenJDK11。 we re-affirm that the OpenJDK 8 and OpenJDK 11 Java runtimes in Amazon Linux 2 will continue to receive free long-term support from Amazon until at least June 30, 2023Link

您可以使用 ebextensions 在您的实例上安装 java 11。 只需在您的源包中创建一个文件夹 .ebextensions 并在其中添加一个名称为 10_java.config 且内容为:[=13= 的文件]

[更新:修复了 yaml 文件的格式]

container_commands:
    100-remove-old-java:
        command: "sudo yum remove -y java-1.8.0-openjdk-headless"
    200-download-rpm-package:
        command: "wget https://d3pxv6yz143wms.cloudfront.net/11.0.4.11.1/java-11-amazon-corretto-devel-11.0.4.11-1.x86_64.rpm "
    300-install-java:
        command: "sudo yum localinstall -y java-11-amazon-corretto-devel-11.0.4.11-1.x86_64.rpm"

这将删除默认的 java 8 并安装 AWS 的 distribution of java 11。

截至 2020 年 5 月,64 位 Amazon Linux 2 上的 Corretto 11 运行 现在是 Elastic Beanstalk 中的托管平台。以下是可用的 Java SE 平台的参考:

https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.javase