문제

현재 실행중인 Appengine Java 앱의 서버 URL을 코드에서 얻으려고합니다. 즉, 앱이 내 로컬 개발자 기계에서 실행중인 경우 어떻게 든 반환하고 싶습니다. "http : // localhost : 8080"하지만 Prod에서 실행 중이면 반품을 원합니다"http://myappid.appspot.com".이 작업을 수행 할 수있는 Java 또는 Appengine API가 있습니까? 구성 파일 또는 상수에서 수동으로 변경하고 읽을 수 없습니다.

감사.

  • 알레
도움이 되었습니까?

해결책

GetServerName ()를 사용할 수 있어야합니다.

boolean local = "localhost".equals(httpServletRequest.getServerName());

더 많은 정보가 필요한 경우 사용할 수있는 다른 방법이 있습니다 (예 : GetServerport).

다른 팁

이것은 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";
}

요청 처리기에서 수행하는 몇 가지 방법이 있습니다 (제공된 것을 사용하는 경우 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')

이것은 'localhost : 8081'또는 'blabla.appspot.com'을 첫 번째 줄로 방출하고, 두 번째는 완전한 URI와 같이 대신 '예를 들어'입니다.http : // localhost : 8081/zup' 또는 'http://blabla.appspot.com/zup'.

보다 일반적으로 사용할 수 있습니다 wsgiref.util WSGI 환경에서 정보를 쉽게 추출하고 App Engine이 WSGI 위에 제공되기 때문에 항상 선택한 프레임 워크의 클러치에서 그러한 환경을 뿌릴 수있는 방법이 항상 있어야합니다. ;-)

나는 이것이 대답되었다는 것을 알고 있지만, 또 다른 하나는 믹스에 던져 버렸다.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top