如何检查 Azure Service Fabric 是否已启动并且 运行?

How to check if Azure Service Fabric is up and running?

有没有办法以编程方式检查 Service Fabric 是否已启动并从外部客户端 运行?我考虑过只使用 try catch 块,但我很好奇是否有更优雅的方法。

没有单一的 "is the cluster running?" 命令。有不同的方式可以解释这个问题。

但是对于外部客户端,您可以做的最简单的检查就是尝试与集群通信。集群可能是 "running",但如果您的客户端无法与其通信,那么它可以就此停止。要以编程方式执行此操作,您必须捕获通信异常。这是一个例子:

FabricClient client = new FabricClient();

try
{
    await client.QueryManager.GetProvisionedFabricCodeVersionListAsync();
}
catch (FabricTransientException ex)
{
    if (ex.ErrorCode == FabricErrorCode.CommunicationError)
    {
         // can't communicate with the cluster!
    }
}

您基本上是在等待连接超时,因此完成此检查需要几秒钟。

如果您的问题是:"How can I check if My Service in Azure Service Fabric is up and running?" 那么您有一些选择。 (如果你的问题是:“我如何检查 Azure Service Fabric 集群 起床了运行?”那么你应该看看。)

创建 FabricClient 的新实例,现在您可以做很多有趣的事情,包括检查服务的运行状况。

如果您的集群是安全的(推荐),那么您需要提供 X509Credentials。您可以关注这篇文章 https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-connect-to-secure-cluster

    var connection = "myclustername.westeurope.cloudapp.azure.com:19000";
    var fabricClient = new FabricClient(GetCredentials(clientCertThumb, serverCertThumb, CommonName), connection);

使用关联的管理器客户端,您可以检查部署到集群的服务的健康状态。

使用 FabricClient.ServiceHealthManager 检查单个分区:

var serviceHealth = await fabricClient.HealthManager.GetServiceHealthAsync(serviceName);
foreach (var serviceHealthPartitionHealthState in serviceHealth.PartitionHealthStates)
{
    Console.WriteLine($"Health state: {serviceHealthPartitionHealthState.PartitionId} {serviceHealthPartitionHealthState.AggregatedHealthState}");
}

使用FabricClient.QueryManagerClient检查每项服务的总体健康状况:

var serviceList = await fabricClient.QueryManager.GetServiceListAsync(applicationName);
foreach (var service in serviceList)
{
    Console.WriteLine($"\tFound service {service.ServiceName} {service.ServiceStatus} {service.HealthState}");
}

如果其中任何一个的计算结果不是 System.Fabric.Health.HealthState.Ok,那么您的服务可能有问题。