【问题标题】:Does azure prevent that role instances are recycled at the same time?azure 是否会阻止角色实例同时被回收?
【发布时间】:2015-04-24 20:12:17
【问题描述】:
我有一个 Web 角色部署到两个实例,应用程序池回收超时设置为默认值 29 小时,应用程序池空闲超时设置为零。我想保持这个应用程序池回收超时,以确保我的应用程序随着时间的推移保持健康。但是,我不希望我的两个实例(意外)同时回收,以确保我的应用程序保持对用户的响应。
azure 是否注意多个实例的应用程序池不会同时被回收?或者:我怎样才能防止这种情况?
【问题讨论】:
标签:
asp.net-mvc
iis
azure
azure-web-roles
azure-cloud-services
【解决方案1】:
Azure 不监控 w3wp 或您的应用程序池,也不协调不同实例之间的回收时间。为了防止应用程序池一次在多个实例之间循环,您应该修改每个实例的时间,例如 这样 IN_0 将设置为 29 小时,IN_1 设置为 30, IN_2 在 31,等等。
我的一位同事提供了此代码:
using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.Web.Administration;
namespace RoleEntry
{
public class Role : RoleEntryPoint
{
public override bool OnStart()
{
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
int instanceScheduleTime = 0;
int.TryParse(RoleEnvironment.CurrentRoleInstance.Id.Substring(RoleEnvironment.CurrentRoleInstance.Id.LastIndexOf("_") + 1),out instanceScheduleTime);
string roleId = string.Format("{0:D2}",(instanceScheduleTime % 24));
TimeSpan scheduledTime = TimeSpan.Parse(roleId + ":00:00");
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
ConfigurationElement applicationPoolDefaultsElement = applicationPoolsSection.GetChildElement("applicationPoolDefaults");
ConfigurationElement recyclingElement = applicationPoolDefaultsElement.GetChildElement("recycling");
ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
bool alreadyScheduled = false;
foreach (ConfigurationElement innerSchedule in scheduleCollection)
{
if ((TimeSpan)innerSchedule["value"] == scheduledTime)
alreadyScheduled = true;
}
if (!alreadyScheduled)
{
ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
addElement1["value"] = scheduledTime;
scheduleCollection.Add(addElement1);
serverManager.CommitChanges();
}
}
return base.OnStart();
}
}
}