在Play Framework 2应用中检索端口号 [英] Retrieving port number in Play Framework 2 app

查看:78
本文介绍了在Play Framework 2应用中检索端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:无论如何定义(配置,命令行参数,或根本不定义),我如何检索正在播放的端口号.

The question is: how do I retrieve the port number that play is listening on, no matter how it was defined (configuration, command line argument, or not at all).

此答案使用Play Framework在应用程序中检索端口号必须适用于Play v1,因为该代码无法编译.

This answer Retrieving port number in-application using Play Framework must be for Play v1 because that code doesn't compile.

在Play 2 Java中,有:

In Play 2 Java there is:

Integer port = Play.application().configuration().getInt("http.port");

这不会返回实际的端口号,至少不是在所有情况下都如此.如果Play的运行方式为:

That doesn't return the actual port number, at least not in all cases. If Play is run as:

run 80 -Dhttp.address=example.com

然后返回null.

如果Play的运行方式为

If Play is run as

run -Dhttp.port 80 -Dhttp.address=example.com

然后从默认端口(9000)开始播放.

Then Play starts on the default port (9000).

正如biesior所指出的那样,通过两次提及该端口可以起作用:

As biesior pointed out it works by mentioning the port twice:

play -Dhttp.port=80 "run 80"

这当然不是最佳选择,因为一个是Play实际使用的东西,另一个是它报告的东西.

Which of course is not optimal since one is what Play actually uses, and the other is what it reports.

但是它将回答我关于开发人员模式的问题.但是,如文档所述,Play绝对不能在产品中使用run运行.那么start命令的等效项是什么,没有更好,更安全的方法了吗? (我对Java版本感兴趣,但是其他人也可能想了解Scala.)

But it would answer my question for dev mode. However, as the documentation says, Play should never be run using run in prod. So what's the equivalent for the start command, and is there no better, safer way? (I'm interested in the Java version but others might like to know about Scala too.)

推荐答案

在开发人员模式下运行,您需要使用加倍符号

Running in dev mode you need to use doubled notation

play -Dhttp.port=80 "run 80"

不幸的是,您必须对默认端口执行相同的操作

Unfortunately you have to do the same for default port

play -Dhttp.port=9000 "run 9000"

因此,我只建议编写shell脚本(或Windows中的.bat),以轻松开始所有必需的参数.

So I just suggest to write shell script (or .bat in Windows) for easy starting with all required params.

这篇关于在Play Framework 2应用中检索端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆