我使用Typesafe Config配置来获取这样的环境变量:
service {
host = "localhost"
host = ${?HOST}
port = 8080
port = ${?PORT}
ports {
tcp = 6969
ws = 6696
}
}
在我写的Boot.scala
中:
val host = config.getString("service.host")
val portHTTP = config.getInt("service.port")
val rootService = system.actorOf(Props(new RootService()))
IO(Http) ! Http.Bind(rootService, interface = host, port = portHTTP)
这应该没问题!我看到了heroku log
信息:
2014-08-30T17:39:38.867872+00:00 app[web.1]: [spray-blog-akka.actor.default-dispatcher-4] [akka://spray-blog/user/IO-HTTP/listener-0] Bound to localhost/127.0.0.1:25870
2014-08-30T17:40:37.007696+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
似乎我已经绑定了一个特定的端口:Bound to localhost/127.0.0.1:25870
,但60秒后Heroku仍然说我没有及时绑定它!
我从Heroku提供的示例应用中复制并粘贴了我的启动脚本:web: target/start com.mturk.Boot -Dhttp.port=${PORT} -Dconfig.file=conf/application.conf
我无法弄清问题是什么!太沮丧!!
答案 0 :(得分:3)
我相信Heroku告诉你必须使用的端口。您可以使用额外的代码行
从Boot.scala中的Heroku中获取$ PORT环境变量val myPort = Properties.envOrElse(“PORT”,“8080”)。toInt // for Heroku兼容性
IO(Http)(系统)! Http.Bind(rootService,“0.0.0.0”,port = myPort)
当然,您可能需要额外导入:
import util.Properties
答案 1 :(得分:0)
免责声明只是提示您如何潜在解决问题。如果答案真的没有用,我道歉。
可能是你在日志中看到的端口是Spray自动生成的随机端口,而Heroku只检查自己的端口,因此错误中出现的不匹配?只是猜测。
我使用以下build.sbt
,因此我可以使用Typesafe配置:
libraryDependencies += "com.typesafe" % "config" % "1.2.1"
项目中没有其他文件。
我使用SBT_OPTS="-Dhttp.port=10876"
运行sbt来模仿Heroku如何通过系统属性将配置传递给应用程序。
➜ typesafe-config SBT_OPTS="-Dhttp.port=10876" xsbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to typesafe-config (in build file:/Users/jacek/sandbox/typesafe-config/)
> console
[info] Updating {file:/Users/jacek/sandbox/typesafe-config/}typesafe-config...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigFactory
scala> val conf = ConfigFactory.load()
conf: com.typesafe.config.Config = Config(SimpleConfigObject({"awt":{"toolkit":"sun.lwawt.macosx.LWCToolkit"},"file":{"encoding":...
scala> conf.getInt("http.port")
res0: Int = 10876
您是否可以尝试使用http.port
而不是HOST
来检查环境变量是否已传递到您的应用程序?