如何检查的AWS EC2实例应用程序运行 [英] How to check application runs in AWS EC2 instance

查看:188
本文介绍了如何检查的AWS EC2实例应用程序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能检查哪些平台我的应用程序运行时,AWS EC2实例,Azure角色实例和非云系统? 现在我是这样做的:

How can I check which platform my app runs, AWS EC2 instance, Azure Role instance and non-cloud system? now I do that like this:

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()方法。的code'的req.GetResponse()'需要较长的时间。所以我想知道我该怎么处理呢?请帮助我!在此先感谢。

but I have one problem when my apps runs in the non-cloud system, like local windows system. It got very slowly while executing isAWS() method. the code 'req.GetResponse()' takes a long time. so I want to know how can I to deal with it? please help me! thanks in advance.

推荐答案

正如你所说类向WebRequest.Create()调用您的桌面上慢所以你真的需要检查网络通信(使用的网络监视器)实际确定了很长一段时间。这个请求,打开连接,连接到目标服务器,下载内容,然后关闭连接,所以这是好事,知道采取这个时候。

As you said the WebRequest.Create() call is slow on your desktop so you really need to check the network traffic (using Netmon) to actually determine what took long time. This request, opens connection, connects to target server, downloads the content and then close the connection so it is good to know where this time is taken.

另外,如果你只是想知道,如果任何URL(在Azure,EC2上或任何其他Web服务器是活的,并通过使用工作正常,你可以只要求只下载标题

Also if you just want to know if any URL (on Azure, on EC2 or any other web server is live and working fine you can just request to only download headers by using

string URI = "http://www.microsoft.com";
HttpWebRequest  req = (HttpWebRequest)WebRequest.Create(URI);
req.Method = WebRequestMethods.Http.Head;
var response = req.GetResponse();
int TotalSize = Int32.Parse(response.Headers["Content-Length"]);
// Now you can parse the headers for 200 OK and know that it is working.

您也可以使用GET只有一个范围内的数据,而不是完整的数据,以加快调用:

You can also use GET only a range of the data instead of full data to expedite to call:

HttpWebRequest myHttpWebReq =(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebReq.AddRange(-200, ContentLength); // return first 0-200 bytes
//Now you can send the request and then parse date for headers for 200 OK

以上任何方法将更快得到您的站点运行。

Any of the above method will be faster to get where your site is running.

这篇关于如何检查的AWS EC2实例应用程序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆