为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?

Posted

技术标签:

【中文标题】为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?【英文标题】:Why spring-boot app is not working in AWS but works fine in local?为什么 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好? 【发布时间】:2020-12-18 17:19:26 【问题描述】:

我创建了一个简单的 Hello World Spring Boot 应用程序,它在本地运行良好,但在 AWS ElasticBeanstalk 中部署时出现 404 错误。

HelloController.java

@RestController
@RequestMapping("/")
public class HelloController 
    
    @GetMapping("hello")
    public String result() 
        return "Hello World";
    


application.properties

server.port = 8080
spring.datasource.url=<RDS ENDPOINT>
spring.datasource.username=<username>
spring.datasource.password=<password>

在 EBS 中,我创建了一个 Tomcat 环境,并将这场战争部署在 Tomcat 环境中。

我做错了什么?

【问题讨论】:

在用@SpringBootApplication 注释的类中,你是否也继承自SpringBootServletInitializer。这是允许 Tomcat 启动应用程序上下文所必需的。 【参考方案1】:

假设您的 WAR 文件名为 test.war,那么要访问的正确 URL 应该是:

http://your-domain:port/test/hello

port 应该是你的 tomcat 端口,如果你没有更改它是 80,而不是你的 application.properties 中设置的server.port

【讨论】:

@AnamikAdhikary 很难说是什么导致了这个问题,这篇文章可能会有所帮助:mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat【参考方案2】:

您应该将您在 aws 中的端口更改为端口 5000。

【讨论】:

以上是关于为啥 spring-boot 应用程序无法在 AWS 中运行,但在本地运行良好?的主要内容,如果未能解决你的问题,请参考以下文章

为啥必须在 spring-boot 中为 web 应用程序提供单独的 @Controller 类文件?

为啥我在 Spring-boot 应用程序中通过 SessionFactory 有循环依赖?

为啥spring-boot应用程序不需要@EnableWebMvc

为啥我用 spring-boot 得到 404 休息

为啥 spring-boot 和 postgres 连接会在一段时间后断开?

无法在 IntelliJ Idea 中启动 spring-boot 应用程序