現在「ステージング」または「本番」で実行しているかどうかを教えてくれるサービス ランタイムのどこかにありますか? 構成を本番環境との間で手動で変更するのは少し面倒です。
6 に答える
Prod または Staging のどちらにいるかに基づいて、構成を変更するべきではありません。ステージング領域は「QA」環境として設計されているのではなく、本番環境がデプロイされる前の保持領域としてのみ設計されています。
新しい展開をアップロードすると、パッケージをアップロードする現在の展開スロットが破棄され、VM のアップロードと開始が行われている間、10 ~ 15 分間ダウンします。本番環境に直接アップロードすると、15 分間の本番ダウンタイムになります。このようにして、ステージング領域が発明されました。ステージングにアップロードし、ものをテストし、「スワップ」ボタンをクリックすると、ステージング環境が魔法のように本番環境になります (仮想 IP スワップ)。したがって、ステージングは本番環境と 100% 同じでなければなりません。
あなたが探しているのは、QA/テスト環境ですか? 独自の製品/ステージングを使用して、テスト環境用の新しいサービスを開く必要があります。この場合、複数の構成ファイル セットを、展開環境 (運用、テストなど) ごとに 1 つのセットで維持する必要があります。
特に .config ファイルの上に独自の *.cscfg ファイルがある Azure で発生する、構成地獄を管理する方法は多数あります。私が Azure プロジェクトで好んで行う方法は次のとおりです。小さな Config プロジェクトをセットアップし、そこに展開タイプに一致するフォルダーを作成します。各フォルダー内で、特定の展開環境に一致する *.config および *.cscfg ファイルのセットをセットアップします: Debug、Test、Release... これらはビルド ターゲット タイプとして Visual Studio でもセットアップされます。Config プロジェクトのビルド ターゲット フォルダーからすべてのファイルを Config プロジェクトのルート フォルダーにコピーする、Config プロジェクトのすべてのコンパイル中に発生する小さな xcopy コマンドがあります。
次に、ソリューション内の他のすべてのプロジェクトが、Config プロジェクトのルート フォルダーから .config または .cscfg ファイルにリンクします。
ほら、私の構成は魔法のようにすべてのビルド構成に自動的に適応します。また、.config 変換を使用して、リリース ビルド ターゲットと非リリース ビルド ターゲットのデバッグ情報を管理します。
これをすべて読んでも、実行時に本番環境とステージングのステータスを取得したい場合は、
次の ようにdeploymentId
します:図書館)。RoleEnvironment.DeploymentId
X509 certificate
Azure structure of your Service
GetDeployments
お役に立てれば
編集: 構成文字列のセットアップと環境間の切り替えについて要求されたブログ投稿 @ http://blog.paraleap.com/blog/post/Managing-environments-in-a-distributed-Azure-or-other-cloud-based -NETソリューション
倫理やベストプラクティスを説明せずに、質問に答えてほしいと思うことがあります...
Microsoft は、まさにこれを行うコード サンプルをここに投稿しました: 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();
}
}
ステージングは、主にダウンタイムなしのアップグレードとアップグレードをロールバックする機能のために使用される一時的な展開スロットです。
システムを (コード内または構成内で) そのような Azure 固有のものと結合しないことをお勧めします。
Windows Azure Management Librariesと @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;
}
}
}
考慮すべき4つのポイントは次のとおりです
- VIP スワップは、サービスが外の世界に面している場合にのみ意味があります。別名、API を公開してリクエストに反応するとき。
- サービスがキューからメッセージをプルして処理するだけの場合、サービスはプロアクティブであり、VIP スワップは適切なソリューションではありません。
- サービスがリアクティブかつプロアクティブである場合は、設計を再検討することをお勧めします。おそらく、サービスを 2 つの異なるサービスに分割します。
- VIP スワップの前後に cscfg ファイルを変更するというエリックの提案は、サービスのプロアクティブな部分が短いダウンタイムで済む場合に適しています (最初にステージングとプロダクションの両方でメッセージをプルしないように構成し、次に VIP スワップを実行し、次にプロダクションの構成を更新して、メッセージのプルを開始します)。