Azure Web角色"热身"策略

时间:2014-07-10 12:08:50

标签: azure azure-web-roles iis-8 azure-configuration

我发现在不活动期间向我们的网络角色发出请求会导致请求非常慢(最多30秒)。在初始请求之后,角色将按预期执行。

经过大量谷歌搜索,我遇到了四种不同的策略(如下所列):

(a)在RoleEntryPoint.OnStart()

中禁用IIS空闲超时
public override bool OnStart()
{
    using (var server = new ServerManager())
    {
        server.ApplicationPoolDefaults.ProcessModel.IdleTimeout = TimeSpan.Zero;
        server.CommitChanges();
    }

    return base.OnStart();
}

这也要求角色在较高的水平上运行。

(b)在RoleEntryPoint.Run()

中执行常规请求
public override void Run()
{
    var localuri = new Uri(string.Format("https://{0}/Help", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpsIn"].IPEndpoint));

    while (true)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(localuri);
            request.Method = "GET";
            var response = request.GetResponse();
        }
        catch { }
        System.Threading.Thread.Sleep(3000);
    }
}

(c)在preloadEnabled

中设置startModeRoleEntryPoint.OnStart()
public override void OnStart()
{
    using (var serverManager = new ServerManager())
    {
        foreach (var application in serverManager.Sites.SelectMany(x => x.Applications))
        {
            application["preloadEnabled"] = true;
        }

        foreach (var applicationPool in serverManager.ApplicationPools)
        {
            applicationPool["startMode"] = "AlwaysRunning";
        }

        serverManager.CommitChanges();
    }

    return base.OnStart();
}

(d)最后,使用Azure的“Always On”(编辑:不幸的是,这仅适用于Azure网站!)

Azure Role Always On

我应该执行以下哪些策略?

2 个答案:

答案 0 :(得分:16)

我们使用了几个答案的组合,它对我们来说效果非常好,他们可以非常快速地进行更改和测试,但它似乎涵盖了所有基础。

public override bool OnStart()
{
    ServicePointManager.DefaultConnectionLimit = 12;
    if(!RoleEnvironment.IsEmulated)
    {
        using(ServerManager serverManager = new ServerManager())
        {
            foreach (var app in serverManager.Sites.SelectMany(x => x.Applications))
            {
                app["preloadEnabled"] = true;
            }
            foreach (var appPool in serverManager.ApplicationPools)
            {
                    appPool.AutoStart = true;
                    appPool["startMode"] = "AlwaysRunning";
                    appPool.ProcessModel.IdleTimeout = TimeSpan.Zero;
                    appPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero;
            }
            serverManager.CommitChanges();
        }
    }
    return base.OnStart();
}

答案 1 :(得分:3)

您是否考虑过使用Azure端点监控来监控和触发您的角色每5分钟响应一次?它内置于Azure中,无需代码。

http://azure.microsoft.com/en-us/documentation/articles/web-sites-monitor/