【问题标题】:Deploying RESTful service to AWS with Spring boot使用 Spring Boot 将 RESTful 服务部署到 AWS
【发布时间】:2017-03-10 20:03:16
【问题描述】:

我正在尝试将一个简单的 Spring Boot RESTful 服务部署到 AWS beanstalk。我正在使用 mvn clean package 来打仗。这是我的代码:

 //@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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

package hello;

import java.util.concurrent.atomic.AtomicLong;

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

@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
    return "Hello, " + name + "!";
}
}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
    <tomcat.version>8.0.3</tomcat.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

但是当我尝试访问 (load-balancer-url)/hello/World 时,我收到 404 错误。对我做错了什么有帮助吗?谢谢。

【问题讨论】:

  • 此应用程序在您的本地环境中是否正常工作?
  • 是的,它可以工作,但我摆脱了战争包和 tomcat 属性。我这样部署它:java -jar target/gs-rest-service-0.1.0.jar,我可以通过localhost:8080/hello/World访问它。
  • 您的 aws url 使用 8080 端口?
  • 不,当我在本地主机上部署 jar 时,它可以工作。当我将其打包为 war 并将其部署到 Beanstalk 中时,出现 404 错误。

标签: java rest maven amazon-web-services spring-boot


【解决方案1】:

Spring Boot | Create a deployable war file

来自文档:

生成可部署的war文件的第一步是提供一个 SpringBootServletInitializer 子类并覆盖其配置 方法。这利用了 Spring Framework 的 Servlet 3.0 支持和 允许您在应用程序启动时对其进行配置 小服务程序容器。通常,您更新应用程序的主类 扩展 SpringBootServletInitializer:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}

【讨论】:

  • 我添加了这个方法,它就像一个魅力。百万谢谢!
【解决方案2】:

您也可以使用 ElasticBeanstalk 安装它。 首先你需要战争文件。进入应用文件夹,使用 Maven 打包如下。

mvn package -DskipTests

(在 windows 机器中,复制 mvnw.cmd 并将副本命名为 mvnw.bat)

打包任务成功后,你会在'target'文件夹中找到你的projectname-snapshot.war。

将war文件上传到Beanstalk后,点击配置更多选项 并转到 Software 部分,只需将 SERVER_PORT 设置为 5000,如下所示。设置好后,点击Create Environment

【讨论】: