简体   繁体   English

Java Play 覆盖 application.conf 中的端口

[英]Java Play overwrites port in application.conf

I want to specify port on which Java Play exposes its endpoints in the application.conf file – complete snippet of the application.conf , format taken from Java Play documentation我想在application.conf文件中指定 Java Play 在其上公开其端点的端口 – application.conf的完整片段,格式取自 Java Play 文档

play.server.http.port = 5511

However, if I run command但是,如果我运行命令

  • sbt run

exposed port is 9000, not 5511.暴露的端口是 9000,而不是 5511。

I was able to achieve correct behaviour by running我能够通过运行来实现正确的行为

  • sbt run -Dhttp.port=5511

so there must be problem in the application.conf .所以application.conf肯定有问题。 If I try to read the value programmatically, say from controller, it also gets the wrong value of 9000. However, if I add some artificial value to the application.conf , say foo = "ABC" , it correctly reads this value.如果我尝试以编程方式读取该值,例如从控制器读取,它也会得到错误的值 9000。但是,如果我向application.conf添加一些人为的值,例如foo = "ABC" ,它会正确读取该值。


Java play version: 2.8.15 Java播放版本:2.8.15
SBT version: 1.6.2 SBT版本:1.6.2
Plugins: only java play 插件:只有java play

As per of the play documentation :根据播放文档

In run mode the HTTP server part of Play starts before the application has been compiled.在运行模式下,Play的 HTTP 服务器部分在应用程序编译之前启动。 This means that the HTTP server cannot access the application.conf file when it starts.这意味着 HTTP 服务器在启动时无法访问 application.conf 文件。 If you want to override HTTP server settings while using the run command you cannot use the application.conf file如果您想在使用 run 命令时覆盖 HTTP 服务器设置,则不能使用 application.conf 文件

I would propose you do this way sbt run -Dhttp.port=5511 as you stated earlier.我建议你这样做sbt run -Dhttp.port=5511正如你之前所说的那样。 If you are worried about passing everytime the arguments with run task then you can change your root project settings in the build.sbt file only once.如果您担心每次使用 run 任务传递参数,那么您只能在build.sbt文件中更改您的根项目设置一次。 Do not forget to to sbt reload so your build.sbt changes take effect.不要忘记sbt reload以便您的build.sbt更改生效。

lazy val root = (project in file(".")).enablePlugins(PlayJava)
   .settings(PlayKeys.playDefaultPort := 5511)

Another alternative : you can add the following line in your build.sbt file另一种选择:您可以在 build.sbt 文件中添加以下行

PlayKeys.devSettings += "play.server.http.port" -> "8080"

I have no direct explanations, but that's work if you put on your application.conf file the http.port variable, and not directly play.server.http.port .我没有直接的解释,但如果你在application.conf文件中http.port变量,而不是直接play.server.http.port ,那就可以了。

A clue may be found in the file, on Production Configuration page , presented by可以在文件中找到线索,在生产配置页面上,由

The configurations above are specific to the Akka HTTP and Netty server backend上面的配置特定于 Akka HTTP 和 Netty 服务器后端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM