如何将Spring启动程序部署到Amazon Elastic Beanstalk?

byr*_*1st 4 java spring tomcat amazon-web-services

我在AWS和Spring Framework上都很新.

我在Spring网站的入门指南之后构建了一个简单的Web程序.此外,我已经使我的Elastic Beanstalk应用程序和环境运行Tomcat 8和Java 8.我正在尝试的是;

  1. 使用Gradle构建这个简单的程序并生成一个可执行的jar(或战争).
  2. 从我的Elastic Beanstalk仪表板中的"上传和部署"按钮上传此可执行jar(或战争).

我现在正在使用IntelliJ,如果我双击生成的可执行jar,我的程序运行良好.但是,在我将此jar上传到我的Elastic Beanstalk后,此程序只会产生HTTP 404错误.

我不明白的是,如果我使用Gradle'build'任务或'bootRun'任务,生成的可执行jar文件工作正常.但是,如果我通过Gradle'war'任务构建war文件并将其放在我的本地Tomcat上,则服务器会产生与AWS相同的HTTP 404错误.

这是我的build.gradle文件.

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
        classpath("org.apache.tomcat:tomcat-catalina:8.0.28")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

jar {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

war {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}
Run Code Online (Sandbox Code Playgroud)

此外,我的程序只有两个Java类:src/main/java/hello/HomeController.javasrc/main/java/hello/Application.java

src/main/java/hello/HomeController.java 是:

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String index() {
        return "Welcome to the home page!";
    }
}
Run Code Online (Sandbox Code Playgroud)

src/main/java/hello/Application.java 是:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

作为总结:

  • build'bootRun' - >浏览"localhost:8080":工作正常.
  • build'build' - >生成'xxx.jar(可执行文件)' - >双击执行这个jar - >浏览"localhost:8080":工作正常
  • 构建'build' - >生成'xxx.jar(可执行文件') - >上传到AWS - >浏览"xx.elasticbeanstalk.com":HTTP 404错误
  • 构建'war' - >生成'xxx.war' - >使用IntelliJ在本地Tomcat上运行此战争 - >浏览"localhost:8080":HTTP 404错误
  • 构建'war' - >生成'xxx.war' - >上传到AWS - >浏览"xx.elasticbeanstalk.com":HTTP 404错误

我想要做的是1)在IntelliJ上开发,2)在Gradle上构建它,3)在本地测试它,4)将它上传到AWS上运行.请帮我.

如果您有任何进一步的解释或信息,请告诉我.谢谢!

Ben*_*chi 6

我有一个类似的问题,你需要做的是首先使你的Application类扩展SpringBootServletInitializer:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

它将使用Spring Framework的Servlet支持初始化您的应用程序(Tomcat将能够注册servlet并启动应用程序).

然后,您必须告诉Gradle应用程序服务器将提供环境运行时.为此,在依赖项下添加以下行:

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}
Run Code Online (Sandbox Code Playgroud)

构建一个新的war(build war)并部署它.

有关更多信息,请参阅有关如何将Spring Boot应用程序部署为战争的文档:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html