您可以使用com.google.cloud.ServiceOptions
帮助程序获取默认的项目ID。它将为您提供一些抽象内容,以便能够project-id
根据您的上下文和当前的应用程序引擎运行时进行检索。
例如,Java11运行时不支持某些旧的api。并获得project-id
通过由HTTP请求的元数据是一个通用的解决方案。
在这种情况下,这ServiceOptions.getDefaultProjectId
是保持代码通用和简单的好方法。
Spring Cloud GCP正在幕后使用此方法进行检索project-id
。
导入google-cloud-core
依赖项:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.94.0</version>
</dependency>
然后使用ServiceOptions
:
import com.google.cloud.ServiceOptions;
...
String projectId = ServiceOptions.getDefaultProjectId();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句