简体   繁体   English

用于 Spring 启动项目的 AWS beanstalk 上的 Java 11

[英]Java 11 on AWS beanstalk for Spring boot project

I'm new to Spring Boot and I'm trying to develop an application to later deploy it on AWS beanstalk.我是 Spring Boot 的新手,我正在尝试开发一个应用程序,以便稍后将其部署在 AWS beanstalk 上。 I started the project using java 11 but later I discovered that AWS only support java 8. Is it possible to set 'maven.compiler.target' in pom.xml to 1.8 to make it running correctly?我开始使用 java 11 的项目,但后来我发现 AWS 只支持 java 8。是否可以将 pom.xml 中的 'maven.compiler.target' 设置为 1.8 以使其正常运行? Should I have to use Java 1.8 for both development and compile?我是否必须使用 Java 1.8 进行开发和编译? I would like to use new Java features and library.我想使用新的 Java 功能和库。 I would like to have some opinion if someone have same problems.如果有人有同样的问题,我想发表一些意见。 Thanks.谢谢。 Cd光盘

Since you're using Java 11 why not take advantage of Java and Elastic Bean Stalks docker support and create a docker image with JDK11 and use this to deploy?既然您使用的是 Java 11,为什么不利用 Java 和 Elastic Bean Stalks docker 支持并使用 JDK11 创建一个 docker 映像并使用它进行部署?

If you choose not to go down this path and you want to want to target a lower version of java to use elastic beanstalk with Java 8 you can try something like this.如果您选择不走这条路,并且希望针对较低版本的 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.仅设置 target 选项并不能保证您的代码实际运行在具有指定版本的 JRE 上。 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.陷阱是意外使用仅存在于后续 JRE 中的 API,这会使您的代码在运行时失败并出现链接错误。 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.为避免此问题,您可以配置编译器的引导类路径以匹配目标 JRE,或者使用 Animal Sniffer Maven 插件来验证您的代码没有使用意外的 API。 In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version.同样,设置 source 选项并不能保证您的代码实际上在具有指定版本的 JDK 上编译。 To compile your code with a specific JDK version, different than the one used to launch使用特定的 JDK 版本编译您的代码,不同于用于启动的版本

Keep in mind though that if you compile and run your code on Java 8, you cannot use classes that have been added to Java's standard library in Java 11, because those will not be present on Java 8. link但请记住,如果您在 Java 8 上编译和运行代码,则不能使用已添加到 Java 11 中的 Java 标准库中的类,因为这些类不会出现在 Java 8 上。 链接

While searching, I found that support for OpenJDK11 may be coming.在搜索时,我发现对 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, 2023 Link 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, 2023 链接

You can install java 11 on your instances using ebextensions .您可以使用ebextensions在您的实例上安装 java 11。 Just create a folder .ebextensions in your source bundle and add there one file with following name 10_java.config and content:只需在源包中创建一个文件夹.ebextensions并在其中添加一个具有以下名称10_java.config和内容的文件:

[UPDATE: fixed formatting of the yaml file] [更新:修复了 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"

This will remove default java 8 and install AWS' distribution of java 11.这将删除默认的Java 8和安装AWS” 分布的Java 11。

As of May 2020, Corretto 11 running on 64bit Amazon Linux 2 is now a managed platform in Elastic Beanstalk.截至 2020 年 5 月,在 64 位 Amazon Linux 2 上运行的 Corretto 11 现在是 Elastic Beanstalk 中的托管平台。 Here is a reference to the Java SE platforms available:以下是对可用 Java SE 平台的参考:

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

When you compile a Java project with a specific Java version, you can only run it with a version that is greater (or equal) than the one you used to compile it.当您使用特定的 Java 版本编译 Java 项目时,您只能使用大于(或等于)用于编译它的版本来运行它。 The opposite cannot be done, at least not if you are using features of the language that are present in later versions.不能做相反的事情,至少如果您正在使用更高版本中存在的语言功能,则不能。

Eg you cannot use features from Java 11 but to run the application in Java 8例如,您不能使用 Java 11 的功能,但可以在 Java 8 中运行应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Elastic Beanstalk-Tomcat Java Spring Boot应用程序出现问题 - AWS Elastic Beanstalk - Problem with tomcat java spring boot application 将现有 Spring 引导项目从 java 1.8 更新到 java 11 - Update existing Spring Boot project from java 1.8 to java 11 AWS Beanstalk上的Spring Boot抛出404 - Spring Boot on AWS Beanstalk throwing 404 AWS Beanstalk + Kotlin + Spring 启动:NoSuchMethodException main() - AWS Beanstalk + Kotlin + Spring boot: NoSuchMethodException main() Spring Boot 2 + AWS Beanstalk设置堆大小 - Spring boot 2 + AWS Beanstalk setting heapsize 当我在 aws 弹性 beantalk 中部署 Spring Boot 项目时,如何压缩 Spring Boot 中的 MultipartFie[] 大小? - How can i compress the MultipartFie[] size in spring boot when i deploy the spring boot project in aws elastic beanstalk.? 无法在 Java 上使用 JSTL 导入标记库 11 Spring 引导项目 - Unable to import taglibs using JSTL on Java 11 Spring Boot project 如何将我的 JAVA/Spring boot + MySQL + Angular 项目上传到 AWS - How to upload my JAVA/Spring boot + MySQL + Angular Project to AWS 我可以在 java-11 spring boot 2 项目中使用 java-8 编译的项目/库吗? - Can I use java-8 compiled project/library in a java-11 spring boot 2 project? aws 弹性 beantalk 上的 HTTP 到 HTTPS 不适用于 Spring Boot - HTTP to HTTPS on aws elastic beanstalk not working for Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM