简单的springboot无法在aws上运行

时间:2018-04-18 19:50:09

标签: java amazon-web-services spring-boot elastic-beanstalk

我使用示例默认项目创建一个弹性的benstalk并且工作正常,但是在我上传我的spring启动项目后它不起作用,但在localhost上运行良好。 为了检查错误,我做了一个非常简单的春季启动项目,只有Web依赖和一个简单的端点,但还没有工作

的pom.xml

<?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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <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>
    </dependencies>

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


</project>

我只创建这个类

package com.example.demo.Testing;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestingAWS {

    @GetMapping()
    public ResponseEntity<?> sayOk(){
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

和@SpringBootApplication

package com.example.demo;

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

@SpringBootApplication public class DemoApplication {

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

我尝试在application.properties中运行没有配置并使用:

server.port=8080

我使用

发动战争
mvn clean install

并且在mvn spring-boot:run部署到localhost并且运行良好之后,端点返回状态200

但是我在控制台中给出了这个错误

2018-04-18 16:30:55.468  INFO 19224 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 16:30:55.469  INFO 19224 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

这个应用程序不适用于aws。错误记录:

https://elasticbeanstalk-us-east-1-770207322946.s3.amazonaws.com/resources/environments/logs/tail/e-vnewtwgp2g/i-0f4edca74953a1e67/TailLogs-1524080692420.out?AWSAccessKeyId=AKIAIOUOORMVUTXOJUHQ&Expires=1524167093&Signature=RILCgkY%2B0pJqEgnHNooejNEa9NU%3D

我放了一个链接因为非常大而且stackoverflow说我超出了caracteres限制

但在健康方面说服务器还可以

enter image description here

但如果尝试请求我会收到此错误:

<html>
    <head>
        <title>502 Bad Gateway</title>
    </head>
    <body bgcolor="white">
        <center>
            <h1>502 Bad Gateway</h1>
        </center>
        <hr>
        <center>nginx/1.12.1</center>
    </body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

2 个答案:

答案 0 :(得分:0)

您是否创建了@SpringBootApplication?

你有没有试过更换?对于方法上的String只是为了查看它是否产生了问题?

如果您只想返回HttpStatus,可能应将方法更改为:

public HttpStatus sayOk(){
   return HttpStatus.ok;
}

答案 1 :(得分:0)

此:

2018-04-18 16:30:55.468  INFO 19224 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 16:30:55.469  INFO 19224 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

不是错误。这意味着Spring Boot正在使用端点/error注册控制器。是Spring Boot的默认行为,如here所述:

  

Spring Boot会自动注册一个BasicErrorController bean   不要在配置中指定任何自定义实现。

您还可以看到日志级别为INFO,而不是ERROR

可能Spring Boot不是错误502 Bad Gateway的原因。导致此错误的原因有很多。我建议您创建另一个问题,重点关注您的AWS设置。