为什么Heroku没有绑定到Java HTTP服务器的端口?

时间:2018-12-18 09:13:46

标签: heroku

我已经在Java中实现了一个基本的HTTP服务器,我正尝试将其部署到Heroku。在localhost上运行时,服务器的行为符合预期,但是在Heroku上,我的日志中出现以下错误:

2018-12-18T09:03:28.323430+00:00 heroku[web.1]: State changed from crashed to starting
2018-12-18T09:03:32.152747+00:00 heroku[web.1]: Starting process with command `java -jar build/http-server.jar`
2018-12-18T09:03:34.496251+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-12-18T09:03:34.517683+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2018-12-18T09:05:02.493578+00:00 heroku[web.1]: State changed from starting to crashed
2018-12-18T09:05:02.344933+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
2018-12-18T09:05:02.344933+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-12-18T09:05:02.475449+00:00 heroku[web.1]: Process exited with status 137

我已将PORT环境变量设置为5000,这是我的服务器当前绑定到的变量(是的,目前已进行了硬编码)。例如,如果将服务器端口更改为80,则可以在日志中看到服务器无法绑定到该端口,因此很高兴它可以绑定到正确的端口。我对the docs的理解是,我应该能够使用PORT环境变量来设置所需的端口号。

那么,为什么Heroku无法绑定到该端口?我还应该采取其他措施,即从443代理到5000吗?

1 个答案:

答案 0 :(得分:0)

  

我将PORT环境变量设置为5000,这是我的服务器当前绑定到的环境变量(是的,目前已进行了硬编码)。

你已经倒退了。

The PORT environment variable is set by Heroku and your code must bind to that value。您不能对端口进行硬编码,也不能自己选择。您必须从环境中拿走它。