java.net.BindException:无法在Heroku上分配请求的地址

时间:2015-11-21 06:59:48

标签: java sockets heroku spark-java

部署到Heroku后,Spark服务器因此异常而崩溃。我的Procfile如下所示:

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

据我所知,Heroku每次部署应用程序时都会分配一个新的PORT。但是,我从Heroku设置的环境变量中获取PORT值,那么为什么我不能得到这个请求的地址呢?

编辑:将Procfile更正为-Dserver.port = $ PORT后,这些是我的错误日志。

2015-11-21T16:45:02.832568+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8
2015-11-21T16:45:04.061714+00:00 app[web.1]: [Thread-1] ERROR spark.webserver.JettySparkServer - ignite failed
2015-11-21T16:45:03.918669+00:00 app[web.1]: [Thread-1] INFO org.eclipse.jetty.util.log - Logging initialized @1084ms
2015-11-21T16:45:03.989340+00:00 app[web.1]: [Thread-1] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
2015-11-21T16:45:03.989452+00:00 app[web.1]: [Thread-1] INFO spark.webserver.JettySparkServer - >> Listening on myappname.herokuapp.com:27925
2015-11-21T16:45:03.998409+00:00 app[web.1]: [Thread-1] INFO org.eclipse.jetty.server.Server - jetty-9.3.z-SNAPSHOT
2015-11-21T16:45:04.062462+00:00 app[web.1]: java.net.BindException: Cannot assign requested address
2015-11-21T16:45:04.062702+00:00 app[web.1]:    at sun.nio.ch.Net.bind0(Native Method)
2015-11-21T16:45:04.065229+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:433)
2015-11-21T16:45:04.065305+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:425)
2015-11-21T16:45:04.065472+00:00 app[web.1]:    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
2015-11-21T16:45:04.065412+00:00 app[web.1]:    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
2015-11-21T16:45:04.065575+00:00 app[web.1]:    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:326)
2015-11-21T16:45:04.065628+00:00 app[web.1]:    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
2015-11-21T16:45:04.065688+00:00 app[web.1]:    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:244)
2015-11-21T16:45:04.065758+00:00 app[web.1]:    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
2015-11-21T16:45:04.067368+00:00 app[web.1]:    at org.eclipse.jetty.server.Server.doStart(Server.java:384)
2015-11-21T16:45:04.067370+00:00 app[web.1]:    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
2015-11-21T16:45:04.067370+00:00 app[web.1]:    at spark.webserver.JettySparkServer.ignite(JettySparkServer.java:131)
2015-11-21T16:45:04.067371+00:00 app[web.1]:    at spark.SparkInstance.lambda$init$0(SparkInstance.java:341)
2015-11-21T16:45:04.067372+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:745)
2015-11-21T16:45:05.052415+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-21T16:45:05.036728+00:00 heroku[web.1]: Process exited with status 100

添加--port $ PORT后发生这种情况。基本上,我认为错误没有变化。

2015-11-21T17:51:14.666062+00:00 app[web.1]: [Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @230ms
2015-11-21T17:51:14.779260+00:00 app[web.1]: java.net.BindException: Cannot assign requested address
2015-11-21T17:51:14.779449+00:00 app[web.1]:    at sun.nio.ch.Net.bind0(Native Method)
2015-11-21T17:51:14.721317+00:00 app[web.1]: [Thread-0] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
2015-11-21T17:51:14.779526+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:433)
2015-11-21T17:51:14.779573+00:00 app[web.1]:    at sun.nio.ch.Net.bind(Net.java:425)
2015-11-21T17:51:14.721319+00:00 app[web.1]: [Thread-0] INFO spark.webserver.JettySparkServer - >> Listening on myappname.herokuapp.com:29822
2015-11-21T17:51:14.779601+00:00 app[web.1]:    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
2015-11-21T17:51:14.779661+00:00 app[web.1]:    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
2015-11-21T17:51:14.721320+00:00 app[web.1]: [Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.3.z-SNAPSHOT
2015-11-21T17:51:14.779796+00:00 app[web.1]:    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
2015-11-21T17:51:14.779732+00:00 app[web.1]:    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:326)
2015-11-21T17:51:14.778639+00:00 app[web.1]: [Thread-0] ERROR spark.webserver.JettySparkServer - ignite failed
2015-11-21T17:51:14.779884+00:00 app[web.1]:    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:244)
2015-11-21T17:51:14.779949+00:00 app[web.1]:    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
2015-11-21T17:51:14.779980+00:00 app[web.1]:    at org.eclipse.jetty.server.Server.doStart(Server.java:384)
2015-11-21T17:51:14.780034+00:00 app[web.1]:    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
2015-11-21T17:51:14.780071+00:00 app[web.1]:    at spark.webserver.JettySparkServer.ignite(JettySparkServer.java:131)
2015-11-21T17:51:14.780110+00:00 app[web.1]:    at spark.SparkInstance.lambda$init$0(SparkInstance.java:341)
2015-11-21T17:51:14.780153+00:00 app[web.1]:    at java.lang.Thread.run(Thread.java:745)
2015-11-21T17:51:15.393560+00:00 heroku[web.1]: Process exited with status 100
2015-11-21T17:51:15.399506+00:00 heroku[web.1]: State changed from starting to crashed

2 个答案:

答案 0 :(得分:1)

我发现问题是我在main方法中设置域的IP地址。似乎我们不应该设置它,而只是通过从环境变量$ PORT获取它来设置PORT。

答案 1 :(得分:0)

在Procfile中使用-Dserver.port=$PORT而不是-Dserver=$PORT并部署应用。 如果错误仍然存​​在,请发布您在日志中获得的确切错误。