java 如何获取 appengine 应用的当前服务器 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1015442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current server URL of appengine app?
提问by aloo
I'm trying to get the server URL of a currently running appengine java app from code. That is, if the app is running on my local dev machine I would like to somehow get returned "http://localhost:8080" but if it is running in prod I'd like to be returned "http://myappid.appspot.com". Are there any java or appengine API's that can do this? I'd like to not have a to manually change and read from a config file or a constant.
我正在尝试从代码中获取当前正在运行的 appengine java 应用程序的服务器 URL。也就是说,如果应用程序在我的本地开发机器上运行,我希望以某种方式返回“ http://localhost:8080”,但如果它在 prod 中运行,我希望返回“ http://myappid 。 appspot.com”。是否有任何 java 或 appengine API 可以做到这一点?我不想手动更改和读取配置文件或常量。
Thanks.
谢谢。
- Aleem
- 阿莱姆
采纳答案by pjesi
You should be able to use getServerName():
您应该能够使用 getServerName():
boolean local = "localhost".equals(httpServletRequest.getServerName());
There are other methods you can also use if you need more info, e.g. getServerPort().
如果您需要更多信息,还可以使用其他方法,例如 getServerPort()。
回答by Sandokan
This is working for me in Java on appengine:
这在 appengine 上的 Java 中对我有用:
String hostUrl;
String environment = System.getProperty("com.google.appengine.runtime.environment");
if (StringUtils.equals("Production", environment)) {
String applicationId = System.getProperty("com.google.appengine.application.id");
String version = System.getProperty("com.google.appengine.application.version");
hostUrl = "http://"+version+"."+applicationId+".appspot.com/";
} else {
hostUrl = "http://localhost:8888";
}
回答by Alex Martelli
Here's a couple of way to do it in your request handler (if you're using the provided webappelementary framework):
这是在您的请求处理程序中执行此操作的几种方法(如果您使用提供的webapp基本框架):
def get(self):
self.response.out.write(self.request.headers.get('host', 'no host'))
self.response.out.write('<br>\n')
who = wsgiref.util.request_uri(self.request.environ)
self.response.out.write(who + '<br>\n')
This emits 'localhost:8081' or 'blabla.appspot.com' as the first line, and as the second like the complete URI instead, e.g. 'http://localhost:8081/zup' or 'http://blabla.appspot.com/zup'.
这将 'localhost:8081' 或 'blabla.appspot.com' 作为第一行,而作为第二行则是完整的 URI,例如“ http://localhost:8081/zup”或“ http://blabla”。 appspot.com/zup'。
More generally, you can use wsgiref.utilto easily extract info out of any WSGI environment, and since App Engine serves on top of WSGI there should always be easy ways to pry such an environment from the clutches of whatever framework you've chosen;-)
更一般地说,您可以使用wsgiref.util它轻松地从任何 WSGI 环境中提取信息,并且由于 App Engine 在 WSGI 之上提供服务,因此应该总是有简单的方法可以从您选择的任何框架的魔爪中撬出这样的环境;-)
回答by gdt
I know this has been answered, but another one to throw in to the mix;
我知道这已经得到了回答,但另一个需要加入;
import com.google.apphosting.api.ApiProxy;
...
final ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
final Map<String, Object> attributes = env.getAttributes();
final String hostAndPort = (String) attributes.get("com.google.appengine.runtime.default_version_hostname");
final String url = "http://" + hostAndPort + "/";
参考:https: //cloud.google.com/appengine/docs/java/appidentity/#Java_Asserting_identity_to_other_App_Engine_apps

