我需要为服务结构服务创建服务可用性指标。如果所有节点都启动则需要报告 OK 状态,任何节点关闭则警告状态,多个节点关闭则错误状态。我对获取节点总数(UP+Down)有点困惑。我已经写了下面的代码,但它现在没有给出任何指标。
public async Task LogServiceFabricHealthMetrics(ServiceContext serviceContext, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var fabricClient = new FabricClient();
var nodeList = (await fabricClient.QueryManager.GetNodeListAsync()).ToList();
var serviceName = serviceContext.ServiceName.LocalPath.Split('/')[2];
var nodesRunningApplication = new List<Node>();
foreach (var node in nodeList)
{
var nodeApplicationList = await fabricClient.QueryManager.GetDeployedApplicationListAsync(node.NodeName);
var nodeApplication = nodeApplicationList.FirstOrDefault(p =>
p.ApplicationName.LocalPath.Split('/')[2] == serviceName);
if (nodeApplication != null)
{
nodesRunningApplication.Add(node);
}
}
if (nodesRunningApplication.Count == nodeList.Count)
{
//All nodes are up and report OK state
}
else
{
if (nodesRunningApplication.Count >= nodeList.Count - 2)
{
//One node is down and report Warning state
}
else
{
//More than one node is down and report Error state
}
}
await Task.Delay(this.interval * 6 * 60 * 1000);
}
}
//string currentNode = ServiceContext.NodeContext.NodeName;
var SfClient = new FabricClient();
var SFnodeList = (await SfClient.QueryManager.GetNodeListAsync()).ToList();
foreach (var node in SFnodeList)
{
Console.WriteLine($"Node Name: {node.NodeName}");
Console.WriteLine($"Node Status: {node.NodeStatus}");
}
var SvcName = ServiceContext.ServiceName.LocalPath.Split('/')[1];
var nodesRunningApp = new List<Node>();
foreach (var node in SFnodeList)
{
var nodeAppList = await SfClient.QueryManager.GetDeployedApplicationListAsync(node.NodeName);
var nodeApp = nodeAppList.FirstOrDefault(p =>
p.ApplicationName.LocalPath.Split('/')[1] == SvcName);
if (nodeApp != null)
{
nodesRunningApp.Add(node);
}
}
节点
获取所有正在运行的应用程序
var currentApps = await SfClient.QueryManager.GetApplicationListAsync();
var currentNodes = SfClient.QueryManager.GetNodeListAsync();
var nodeInformation = await SfClient.QueryManager.GetNodeLoadInformationAsync("nodeName");