How to differentiate whether the code is running in an emulator or in GKE when working with GCP's Datastore

时光毁灭记忆、已成空白 提交于 2019-12-12 16:41:22

问题


I am not sure whether I am missing any, after following the instructions given in https://cloud.google.com/datastore/docs/tools/datastore-emulator

I was not able to connect to local emulator unless I create Datastore explicitly using DataStoreClient.

DatastoreDb db = DatastoreDb.Create(projectId, string.Empty, new DatastoreClientImpl(
                new Datastore.DatastoreClient(
                    new Channel("localhost", 8081, ChannelCredentials.Insecure)), new DatastoreSettings()));

instead of just DatastoreDb.Create(projectId);

If we are working in GKE for production, we need to connect to actual Datastore not an emulator, how do we differentiate between the two versions with same code base.

Is there a way to check if code is running GKE, or is this something that should be done via by an environment variable for best results.


回答1:


You can use the Platform class in the Google.Api.Gax namespace (in the Google.Api.Gax package):

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://stackoverflow.com/questions/50589928/how-to-differentiate-whether-the-code-is-running-in-an-emulator-or-in-gke-when-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!