Spring Boot项目托管在Heroku上 – Web进程在启动后的90秒内未能绑定到$PORT。

huangapple go评论81阅读模式
英文:

Spring Boot project hosted on Heroku - Web process failed to bind to $PORT within 90 seconds of launch

问题

我在Stackoverflow上阅读了许多类似的问题,但没有任何帮助。

我创建了一个Spring Boot Maven后端项目,并将其托管在Heroku上。

但是,当我尝试从前端调用时,浏览器显示以下错误。

GET https://hoyamu-springboot-backend.herokuapp.com/fetch/bag 503

当我查看Heroku的日志时,显示以下错误。

Web进程在启动后的90秒内未能绑定到$PORT

我尝试了一切,但什么都没有用。

我的文件如下:

Heroku应用程序日志

...(日志内容)

Procfile

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

application.properties

hibernate.dialect=org.hibernate.dialect.MySQL57Dialect

spring.datasource.url=jdbc:mysql://remotemysql.com:3306/MMgqawaw
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.show-sql=true

我应该怎么办?

英文:

I have read many similar questions on stackoverflow and nothing helped me.

I have created a spring boot maven backend project and hosted on heroku.

But, when I try to call from the frontend , it shows an error like this in the browser.
GET https://hoyamu-springboot-backend.herokuapp.com/fetch/bag 503

When I view the log of heroku it shows an error like this. Web process failed to bind to $PORT within 90 seconds of launch . I did everything and nothing works.

My files are as below,

Heroku Application Log

020-08-08T21:32:21.232432+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-08T21:32:25.103258+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=48348 $JAVA_OPTS -jar target/hoyamu-0.0.1-SNAPSHOT.jar`
2020-08-08T21:32:27.226023+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2020-08-08T21:32:27.230034+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8 
2020-08-08T21:32:28.818160+00:00 app[web.1]: 
2020-08-08T21:32:28.818210+00:00 app[web.1]:   .   ____          _            __ _ _
2020-08-08T21:32:28.818211+00:00 app[web.1]:  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2020-08-08T21:32:28.818214+00:00 app[web.1]: ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2020-08-08T21:32:28.818294+00:00 app[web.1]:  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2020-08-08T21:32:28.818323+00:00 app[web.1]:   '  |____| .__|_| |_|_| |_\__, | / / / /
2020-08-08T21:32:28.818414+00:00 app[web.1]:  =========|_|==============|___/=/_/_/_/
2020-08-08T21:32:28.819695+00:00 app[web.1]:  :: Spring Boot ::        (v2.2.6.RELEASE)
2020-08-08T21:32:28.819727+00:00 app[web.1]: 
2020-08-08T21:32:29.047717+00:00 app[web.1]: 2020-08-08 21:32:29.044  INFO 4 --- [           main] com.udith.hoyamu.HoyamuApplication       : Starting HoyamuApplication v0.0.1-SNAPSHOT on 50382c68-eaab-4b3c-ac0c-a881a18e4a0b with PID 4 (/app/target/hoyamu-0.0.1-SNAPSHOT.jar started by u8138 in /app)
2020-08-08T21:32:29.048863+00:00 app[web.1]: 2020-08-08 21:32:29.048  INFO 4 --- [           main] com.udith.hoyamu.HoyamuApplication       : No active profile set, falling back to default profiles: default
2020-08-08T21:32:30.083462+00:00 app[web.1]: 2020-08-08 21:32:30.081  INFO 4 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-08-08T21:32:30.224960+00:00 app[web.1]: 2020-08-08 21:32:30.224  INFO 4 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 130ms. Found 18 JPA repository interfaces.
2020-08-08T21:32:31.310818+00:00 app[web.1]: 2020-08-08 21:32:31.310  INFO 4 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 48348 (http)
2020-08-08T21:32:31.327532+00:00 app[web.1]: 2020-08-08 21:32:31.327  INFO 4 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-08T21:32:31.327720+00:00 app[web.1]: 2020-08-08 21:32:31.327  INFO 4 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-08-08T21:32:31.405048+00:00 app[web.1]: 2020-08-08 21:32:31.404  INFO 4 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-08T21:32:31.405289+00:00 app[web.1]: 2020-08-08 21:32:31.405  INFO 4 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2272 ms
2020-08-08T21:32:31.669531+00:00 app[web.1]: 2020-08-08 21:32:31.669  INFO 4 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-08T21:32:31.931057+00:00 app[web.1]: 2020-08-08 21:32:31.930  INFO 4 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-08-08T21:32:32.109028+00:00 app[web.1]: 2020-08-08 21:32:32.108  INFO 4 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-08T21:32:32.249430+00:00 app[web.1]: 2020-08-08 21:32:32.249  INFO 4 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-08-08T21:32:33.786599+00:00 app[web.1]: 2020-08-08 21:32:33.786  INFO 4 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-08-08T21:32:33.850851+00:00 app[web.1]: 2020-08-08 21:32:33.850  INFO 4 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-08-08T21:33:55.347291+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2020-08-08T21:33:55.368253+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-08-08T21:33:55.465503+00:00 heroku[web.1]: Process exited with status 137
2020-08-08T21:33:55.499546+00:00 heroku[web.1]: State changed from starting to crashed

2020-08-08T21:50:58.187914+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/fetch/bag" host=hoyamu-springboot-backend.herokuapp.com request_id=db721f39-aab3-4a3f-82e9-6495dc6bc7a8 fwd="112.135.40.125" dyno= connect= service= status=503 bytes= protocol=https

Procfile

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

application.properties

hibernate.dialect=org.hibernate.dialect.MySQL57Dialect

spring.datasource.url=jdbc:mysql://remotemysql.com:3306/MMgqawaw
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.show-sql=true


```
What should I do?


</details>


# 答案1
**得分**: 6

经过几次小的调查,我找到了解决我的问题的方法。

错误```Error R10 (Boot timeout) -&gt; Web process failed to bind to $PORT within 90 seconds of launch``` 可能会由于两个原因而发生。
1. 实际上,应用程序无法正确地绑定端口,因为它在 Procfile 中没有正确定义。
   您可以通过阅读一些 Stack Overflow 的问题来找到解决方法。
   简而言之,Procfile 应该如下所示:

   (在部署后将&lt;BUILT_APP_NAME_HERE&gt;替换为您的应用程序名称)

   ```web: java $JAVA_OPTS -jar -Dserver.port=$PORT target/&lt;BUILT_APP_NAME_HERE&gt;.jar```

   [或]

   在使用 * 时,您不需要指定名称

   ```web: java $JAVA_OPTS -jar -Dserver.port=$PORT target/*.jar```

   [或]

   ```web: java $JAVA_OPTS -jar -Dspring.profiles.active=production target/&lt;BUILT_APP_NAME_HERE&gt;.jar --port $PORT```

2. 第二个原因是,如错误所示,在启动后的90秒(对于某些应用程序为60秒)后,尝试绑定端口,即使应用程序尚未完全启动。默认情况下,对于 Java 应用程序,Heroku 设置了90秒的启动时间限制。如果在那段时间内 Heroku 无法绑定端口,应用程序将被标记为崩溃。
   在我的项目中,与远程数据库建立连接需要一些时间,但是 Heroku 不会等到数据库连接建立,因为其启动时间为90秒。

   解决方法是,我们必须增加应用程序的启动时间。您可以通过以下链接执行此操作。(有时此链接会出错,然后请尝试使用另一个浏览器或您的手机浏览器)

   https://tools.heroku.support/limits/boot_timeout

   但是,不建议将启动时间增加到120秒以上。

   但是,在实际情况中,您应该找到为何需要花费这么多时间来建立数据库连接的解决方法。

<details>
<summary>英文:</summary>

After several small surveys, I could find the solution for my issue. 

The error ```Error R10 (Boot timeout) -&gt; Web process failed to bind to $PORT within 90 seconds of launch```  can happen  due to two reasons. 
1.  Actually the app cannot bind the port as it is not defined in the procfile properly.
   You can find solutions for that by reading some stackoverflow questions.
  In brief, procfile should look like this: 

(replace &lt;BUILT_APP_NAME_HERE&gt; with your app name after deployed)
 
  ```web: java $JAVA_OPTS -jar -Dserver.port=$PORT target/&lt;BUILT_APP_NAME_HERE&gt;.jar```

[or]

When giving * , you don&#39;t need to specify the name

``` web: java $JAVA_OPTS -jar -Dserver.port=$PORT target/*.jar ```

[or]

```web: java $JAVA_OPTS -jar -Dspring.profiles.active=production target/&lt;BUILT_APP_NAME_HERE&gt;.jar --port $PORT ```

2. Second reason is, as shown in the error, it tries to bind the port after 90 seconds (for some apps it is 60 secs)  of launch, even though the app has not yet fully started. By default, for java apps ,heroku has set a limit of 90 secs as the boot time. If Heroku cannot bind the port during that time, the app is marked as crashed.
 Here , in my project, it takes another little time for making the connection with remote database , but heroku doesn&#39;t wait till the db connection is made as its boot time is 90 secs.
 
The solution for this is, we have to increase the boot time of the app.  you can do this by following this link. ( Sometimes this link gives errors, then try with another browser or with your mobile phone&#39;s browser)

https://tools.heroku.support/limits/boot_timeout

However, it is not recommended to increase the boot time more 120secs.

But, in real case, you should find the solution  why it takes much time to make the db connection.

</details>



# 答案2
**得分**: 0

我可以运行我的Spring MVC应用程序,使用以下Procfile内容:

web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

这是部署的应用程序链接:http://duckingrace.herokuapp.com/web/canvasv1

这是源代码链接:https://github.com/fajaralmu/java-websocket1

<details>
<summary>英文:</summary>

i was able to run my spring mvc app with this Procfile content:

web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

this is the deployed app: http://duckingrace.herokuapp.com/web/canvasv1

this is the source code: https://github.com/fajaralmu/java-websocket1

</details>



huangapple
  • 本文由 发表于 2020年8月9日 05:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定