使用 GCP 的数据存储时如何区分代码是模拟器中的 运行 还是 GKE 中的代码
How to differentiate whether the code is running in an emulator or in GKE when working with GCP's Datastore
按照
https://cloud.google.com/datastore/docs/tools/datastore-emulator
我无法连接到本地模拟器,除非我使用 DataStoreClient 显式创建数据存储。
DatastoreDb db = DatastoreDb.Create(projectId, string.Empty, new DatastoreClientImpl(
new Datastore.DatastoreClient(
new Channel("localhost", 8081, ChannelCredentials.Insecure)), new DatastoreSettings()));
而不只是 DatastoreDb.Create(projectId);
如果我们在 GKE 中进行生产,我们需要连接到实际的数据存储而不是模拟器,我们如何区分具有相同代码库的两个版本。
有没有办法检查代码是否是 运行 GKE,或者这是否应该通过环境变量来完成以获得最佳结果。
您可以在 Google.Api.Gax
命名空间(在 Google.Api.Gax
包中)中使用 Platform
class:
Platform platform = Platform.Instance();
switch (platform.Type)
{
case PlatformType.Gae:
// Code for AppEngine
break;
case PlatformType.Gce:
// Code for Compute Engine
break;
case PlatformType.Gke:
// Code for Google Kubernetes Engine
break;
default:
// Code for other contexts
break;
}
按照 https://cloud.google.com/datastore/docs/tools/datastore-emulator
我无法连接到本地模拟器,除非我使用 DataStoreClient 显式创建数据存储。
DatastoreDb db = DatastoreDb.Create(projectId, string.Empty, new DatastoreClientImpl(
new Datastore.DatastoreClient(
new Channel("localhost", 8081, ChannelCredentials.Insecure)), new DatastoreSettings()));
而不只是 DatastoreDb.Create(projectId);
如果我们在 GKE 中进行生产,我们需要连接到实际的数据存储而不是模拟器,我们如何区分具有相同代码库的两个版本。
有没有办法检查代码是否是 运行 GKE,或者这是否应该通过环境变量来完成以获得最佳结果。
您可以在 Google.Api.Gax
命名空间(在 Google.Api.Gax
包中)中使用 Platform
class:
Platform platform = Platform.Instance();
switch (platform.Type)
{
case PlatformType.Gae:
// Code for AppEngine
break;
case PlatformType.Gce:
// Code for Compute Engine
break;
case PlatformType.Gke:
// Code for Google Kubernetes Engine
break;
default:
// Code for other contexts
break;
}