Heroku Spring Boot 部署成功但状态为 503 服务不可用

Heroku SprinBoot Deployment is sucess but Status is 503 Service Unavailable

您好,我正在尝试将 Spring 启动应用程序部署到 Heroku。构建成功,但当我尝试 运行 应用程序时,出现 Status 503: Service Unavailable 错误。

我提到了类似的 post 但这对我没有帮助。

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.2 com.sample.app 示例应用程序 0.0.1-快照 示例应用程序 示例项目 11 罐 org.springframework.boot spring-boot-starter-data-jdbc org.springframework.boot spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.networknt</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>1.0.57</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>

这是来自 heroku logs --tail

的错误日志
Create a Procfile to customize the command used to run this process: https://devcenter.heroku.com/articles/procfile
2021-09-02T07:19:39.158915+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2021-09-02T07:19:39.162760+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2021-09-02T07:19:39.289159+00:00 app[web.1]: no main manifest attribute, in target/XXX.jar
2021-09-02T07:19:39.341327+00:00 heroku[web.1]: Process exited with status 1
2021-09-02T07:19:39.636801+00:00 heroku[web.1]: State changed from starting to crashed
2021-09-02T12:45:48.327297+00:00 heroku[web.1]: State changed from crashed to starting
2021-09-02T12:45:52.218984+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=28736 $JAVA_OPTS -jar target/XXX.jar`
2021-09-02T12:45:54.207103+00:00 app[web.1]: Create a Procfile to customize the command used to run this process: https://devcenter.heroku.com/articles/procfile
2021-09-02T12:45:54.250429+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2021-09-02T12:45:54.256963+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2021-09-02T12:45:54.426793+00:00 app[web.1]: no main manifest attribute, in target/XXX.jar
2021-09-02T12:45:54.485594+00:00 heroku[web.1]: Process exited with status 1
2021-09-02T12:45:54.641187+00:00 heroku[web.1]: State changed from starting to crashed
2021-09-02T15:51:38.369534+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=springboot-exp.herokuapp.com request_id=8c88bc1a-aae7-4c0e-9daa-b7ef602d2c47 fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https
2021-09-02T15:51:38.626347+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=springboot-exp.herokuapp.com request_id=9e2333ec-26a8-46f5-b770-2565a59ef57d fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https
2021-09-02T15:53:34.736916+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=springboot-exp.herokuapp.com request_id=0025da95-bb22-41b7-a085-63bf5664a7c5 fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https
2021-09-02T15:53:34.885977+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=springboot-exp.herokuapp.com request_id=bbc9f8b4-a65f-4c95-9d2a-ce9d1b69ab02 fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https
2021-09-02T15:53:54.458780+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=springboot-exp.herokuapp.com request_id=ceabd4ad-f5f3-4e35-b6e7-d0ca003df0b9 fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https
2021-09-02T15:53:54.599062+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=springboot-exp.herokuapp.com request_id=463f5577-efbd-4d79-ad85-70462d20eaca fwd="71.69.151.195" dyno= connect= service= status=503 bytes= protocol=https

我遇到了同样的问题!对于我的情况,最简单的解决方案是只使用 Java 1.8 项目 - Heroku 自动默认为 1.8。

根据Java Heroku文档here,支持的版本如下:

  • Java 7 - 1.7.0_312
  • Java 8 - 1.8.0_302
  • Java 11 - 11.0.12
  • Java 13 - 13.0.8
  • Java 15 - 15.0.4
  • Java 16 - 16.0.2

您似乎正在使用 Java 11. 要将此告诉 Heroku,请在您的资源文件夹中创建一个名为 system.properties 的文件。在 system.properties 中添加 java.runtime.version=11。我相信你也可以做到 java.runtime.version=11.0.12.

希望对您有所帮助!查看文档以获取更多信息。

事实证明,失败日志提到 Heroku 需要 Procfile 和 Spring 引导应用程序。

摘自 Spring 文档: 要自定义 Heroku 构建,我们需要提供一个 Procfile,它提供部署应用程序所需的指令。 Heroku 分配一个端口供 Java 应用程序使用,然后确保到外部 URI 的路由有效。

我们必须配置我们的应用程序以侦听正确的端口。这是入门 REST 应用程序的 Procfile:

web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar