Ken*_*nji 46 azure azure-cloud-services
服务运行时中是否有任何地方可以告诉我当前是否正在运行'Staging'或'Production'?手动修改配置到生产和生产似乎有点麻烦.
Igo*_*rek 76
如果你在Prod或Staging中,你应该在你的基础上改变你的配置.暂存区域不是设计为"QA"环境,而是在部署生产之前仅设置保留区域.
当您上传新部署时,您上传程序包的当前部署插槽将被销毁,并且在上载和启动VM时会停机10-15分钟.如果您直接上传到生产中,则需要15分钟的生产停机时间.因此,发明了临时区域:您上传到临时,测试东西,然后单击"交换"按钮,您的临时环境神奇地变为生产(虚拟IP交换).因此,您的升级应该与您的生产完全相同.
我认为您正在寻找QA /测试环境?您应该使用自己的Prod/Staging为Testing环境打开一个新服务.在这种情况下,您将需要维护多个配置文件集,每个部署环境一组(生产,测试等)
管理配置的方法有很多种,特别是对于拥有.config文件的Azure,它自己的*.cscfg文件.我喜欢使用Azure项目的方式如下:设置一个小的Config项目,在那里创建匹配Deployment类型的文件夹.在每个文件夹中设置与特定部署环境匹配的*.config和*.cscfg文件:Debug,Test,Release ......这些也在Visual Studio中设置,作为构建目标类型.我有一个小的xcopy命令,在每次编译Config项目时发生,它将Config项目的Build Target文件夹中的所有文件复制到Config项目的根文件夹中.
然后解决方案中的每个其他项目,从Config项目的根文件夹LINKS到.config或.cscfg文件.
瞧,我的配置自动适应每个构建配置.我还使用.config转换来管理Release与非Release构建目标的调试信息.
如果您已经阅读了所有这些并仍希望在运行时获得Production vs. Staging状态,那么:Get deploymentIdfrom RoleEnvironment.DeploymentId
然后使用Management API并使用适当的方法X509 certificate来获取Azure structure of your Service并调用GetDeployments 方法(它是rest api但是有一个抽象图书馆).
希望这可以帮助
编辑:关于配置字符串的设置和环境之间切换的请求的博客文章@ http://blog.paraleap.com/blog/post/Managing-environments-in-a-distributed-Azure-or-other-cloud-based -NET解决方案
art*_*ker 51
有时我希望人们只回答这个问题......不解释道德或最佳实践......
微软已在此发布了一个代码示例:https://code.msdn.microsoft.com/windowsazure/CSAzureDeploymentSlot-1ce0e3b5


protected void Page_Load(object sender, EventArgs e)
{
// You basic information of the Deployment of Azure application.
string deploymentId = RoleEnvironment.DeploymentId;
string subscriptionID = "<Your subscription ID>";
string thrumbnail = "<Your certificate thumbnail print>";
string hostedServiceName = "<Your hosted service name>";
string productionString = string.Format(
"https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
subscriptionID, hostedServiceName, "Production");
Uri requestUri = new Uri(productionString);
// Add client certificate.
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = store.Certificates.Find(
X509FindType.FindByThumbprint, thrumbnail, false);
store.Close();
if (collection.Count != 0)
{
X509Certificate2 certificate = collection[0];
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(requestUri);
httpRequest.ClientCertificates.Add(certificate);
httpRequest.Headers.Add("x-ms-version", "2011-10-01");
httpRequest.KeepAlive = false;
HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;
// Get response stream from Management API.
Stream stream = httpResponse.GetResponseStream();
string result = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
if (result == null || result.Trim() == string.Empty)
{
return;
}
XDocument document = XDocument.Parse(result);
string serverID = string.Empty;
var list = from item
in document.Descendants(XName.Get("PrivateID",
"http://schemas.microsoft.com/windowsazure"))
select item;
serverID = list.First().Value;
Response.Write("Check Production: ");
Response.Write("DeploymentID : " + deploymentId
+ " ServerID :" + serverID);
if (deploymentId.Equals(serverID))
lbStatus.Text = "Production";
else
{
// If the application not in Production slot, try to check Staging slot.
string stagingString = string.Format(
"https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
subscriptionID, hostedServiceName, "Staging");
Uri stagingUri = new Uri(stagingString);
httpRequest = (HttpWebRequest)HttpWebRequest.Create(stagingUri);
httpRequest.ClientCertificates.Add(certificate);
httpRequest.Headers.Add("x-ms-version", "2011-10-01");
httpRequest.KeepAlive = false;
httpResponse = httpRequest.GetResponse() as HttpWebResponse;
stream = httpResponse.GetResponseStream();
result = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
if (result == null || result.Trim() == string.Empty)
{
return;
}
document = XDocument.Parse(result);
serverID = string.Empty;
list = from item
in document.Descendants(XName.Get("PrivateID",
"http://schemas.microsoft.com/windowsazure"))
select item;
serverID = list.First().Value;
Response.Write(" Check Staging:");
Response.Write(" DeploymentID : " + deploymentId
+ " ServerID :" + serverID);
if (deploymentId.Equals(serverID))
{
lbStatus.Text = "Staging";
}
else
{
lbStatus.Text = "Do not find this id";
}
}
httpResponse.Close();
stream.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
暂存是一个临时部署插槽,主要用于无停机升级和回滚升级的能力.
建议不要将您的系统(在代码或配置中)与此类Azure特性相结合.
自Windows Azure管理库以及感谢@GuaravMantri 回答另一个问题后,您可以这样做:
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Management.Compute;
using Microsoft.WindowsAzure.Management.Compute.Models;
namespace Configuration
{
public class DeploymentSlotTypeHelper
{
static string subscriptionId = "<subscription-id>";
static string managementCertContents = "<Base64 Encoded Management Certificate String from Publish Setting File>";// copy-paste it
static string cloudServiceName = "<your cloud service name>"; // lowercase
static string ns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration";
public DeploymentSlot GetSlotType()
{
var managementCertificate = new X509Certificate2(Convert.FromBase64String(managementCertContents));
var credentials = new CertificateCloudCredentials(subscriptionId, managementCertificate);
var computeManagementClient = new ComputeManagementClient(credentials);
var response = computeManagementClient.HostedServices.GetDetailed(cloudServiceName);
return response.Deployments.FirstOrDefault(d => d.DeploymentSlot == DeploymentSlot.Production) == null ? DeploymentSlot.Staging : DeploymentSlot.Production;
}
}
}
Run Code Online (Sandbox Code Playgroud)
解决此问题的一个简单方法是在您的实例中设置一个键来识别它正在运行的环境。
1) 在您的生产槽中设置:设置>>应用程序设置>>应用程序设置并创建一个名为 SLOT_NAME 的键和值“生产”。重要提示:检查插槽设置。
2) 在您的暂存槽中进行设置:设置设置 >> 应用程序设置 >> 应用程序设置 并创建一个名为 SLOT_NAME 的键和值“staging”。重要提示:检查插槽设置。
从您的应用程序访问变量并确定应用程序正在运行的环境。在 Java 中,您可以访问:
String slotName = System.getenv("APPSETTING_SLOT_NAME");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25449 次 |
| 最近记录: |