gpt4 book ai didi

amazon-ec2 - 如何检查应用程序在 AWS EC2 实例中的运行情况

转载 作者:行者123 更新时间:2023-12-03 23:24:53 29 4
gpt4 key购买 nike

如何查看我的应用程序运行的平台、AWS EC2 实例、Azure 角色实例和非云系统?
现在我这样做:

if(isAzure())
{
//run in Azure role instance
}
else if(isAWS())
{
//run in AWS EC2 instance
}
else
{
//run in the non-cloud system
}

//checked whether it runs in AWS EC2 instance or not.
bool isAWS()
{
string url = "http://instance-data";
try
{
WebRequest req = WebRequest.Create(url);
req.GetResponse();
return true;
}
catch
{
return false;
}
}

但是当我的应用程序在非云系统(如本地 Windows 系统)中运行时,我遇到了一个问题。执行 isAWS() 方法时变得非常缓慢。代码“req.GetResponse()”需要很长时间。所以我想知道我该如何处理?请帮我!提前致谢。

最佳答案

更好的方法是请求获取实例元数据。

来自 AWS Documentation :

To view all categories of instance metadata from within a running instance, use the following URI:

http://169.254.169.254/latest/meta-data/

On a Linux instance, you can use a tool such as cURL, or use the GET command, for example:

PROMPT> GET http://169.254.169.254/latest/meta-data/



这是使用 Python Boto 包装器的示例:
from boto.utils import get_instance_metadata

m = get_instance_metadata()

if len(m.keys()) > 0:
print "Running on EC2"

else:
print "Not running on EC2"

关于amazon-ec2 - 如何检查应用程序在 AWS EC2 实例中的运行情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907418/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com