2015-11-15 101 views
4

我在AWS和Spring Framework上都很新。如何将Spring引导程序部署到Amazon Elastic Beanstalk?

我已经按照Spring网站入门指南构建了a simple web program。此外,我已经使我的Elastic Beanstalk应用程序和环境运行Tomcat 8和Java 8.我正在尝试的是;

  1. 使用Gradle构建这个简单的程序并生成一个可执行的jar(或war)。
  2. 从我的Elastic Beanstalk仪表板上载'upload and deploy'按钮的可执行jar(或war)。

我现在正在使用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' 
} 

此外,我的节目只有两个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!"; 
    } 
} 

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); 
    } 

} 

作为总结:

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

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

如果您有任何进一步的解释或信息,请让我知道。 谢谢!

+0

你解决了吗?我有一些基于我自己的测试的想法。 –

+0

Beanstalk使用Nginx作为您的服务的反向代理,并且在后端默认使用端口5000。尝试在Beanstalk配置中将SERVER_PORT设置为5000。无论如何,错误应该是503 ... – charli

回答

6

我也有类似的问题,你需要做的是先为您应用类扩展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); 
    } 

} 

这将初始化使用Spring Framework的支持Servlet您的应用程序(Tomcat将能够登记该servlet并启动应用程序)。

然后你必须告诉Gradle环境运行时将由应用程序服务器提供。要做到这一点依赖下添加以下行:

dependencies { 
    // … 
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' 
    // … 
} 

建立一个新的战争(build war)并部署它。

对于这里的详细信息是如何部署春季启动的应用程序作为战争的文件:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

+0

完美。非常感谢。 – Gaurav

+0

大感谢这个答案,它解决了我的问题太...虽然我使用maven,所以我不得不把它添加到我的依赖关系:' \t \t \t org.springframework.boot \t \t \t 春天-boot-起动的Tomcat \t \t \t 提供 \t \t' – Trevor

相关问题