Modules API 还提供可用于检索模块、版本或实例的地址的函数。这样一来,应用就可以在开发和生产环境中将请求从一个实例发送到另一个实例。
以下代码示例演示了如何获取请求的模块名称和实例 ID:
importcom.google.appengine.api.modules.ModulesService;importcom.google.appengine.api.modules.ModulesServiceFactory;ModulesServicemodulesApi=ModulesServiceFactory.getModulesService();// Get the service name handling the current request.StringcurrentModuleName=modulesApi.getCurrentModule();// Get the instance handling the current request.intcurrentInstance=modulesApi.getCurrentInstance();
自动扩缩模块的实例 ID 将作为唯一的 Base64 编码值返回,例如 e4b565394caa。
您可以在同一个应用的不同模块之间通信,只需提取目标模块的主机名即可:
以下代码示例演示了如何获取请求的模块名称和实例 ID:
importcom.google.appengine.api.modules.ModulesService;importcom.google.appengine.api.modules.ModulesServiceFactory;importjava.net.MalformedURLException;importjava.net.URL;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.IOException;// ...ModulesServicemodulesApi=ModulesServiceFactory.getModulesService();// ...try{URLurl=newURL("http://"+modulesApi.getVersionHostname("my-backend-service","v1")+"/fetch-stats");BufferedReaderreader=newBufferedReader(newInputStreamReader(url.openStream()));Stringline;while((line=reader.readLine())!=null){// Do something...}reader.close();}catch(MalformedURLExceptione){// ...}catch(IOExceptione){// ...}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\u003cp\u003eThe Modules API provides functions to retrieve information about the current operating environment, including module, version, and instance details.\u003c/p\u003e\n"],["\u003cp\u003eThis API enables applications to obtain the address of a specific module, version, or instance, facilitating communication between instances in both development and production.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eModulesServiceFactory\u003c/code\u003e class is used to access the Modules API, allowing developers to get the current module name and instance ID, among other information.\u003c/p\u003e\n"],["\u003cp\u003eModules can communicate with each other by fetching the hostname of the target module using \u003ccode\u003egetVersionHostname\u003c/code\u003e function, which will then require the use of a URL to retrieve it.\u003c/p\u003e\n"],["\u003cp\u003eFor those updating to the App Engine Java 11/17 runtime, there is a specific migration guide that outlines the migration options for legacy bundled services.\u003c/p\u003e\n"]]],[],null,[]]