检查或添加节点。主要服务于仅有跟踪数据的客户端接入;应用接入带有明确NodeCode,将以高精度匹配。
This commit is contained in:
parent
f40fa621f0
commit
f2f4ecccd5
|
@ -1,11 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using NewLife;
|
||||
using NewLife.Data;
|
||||
using NewLife.Log;
|
||||
using Stardust.Data.Nodes;
|
||||
using XCode;
|
||||
using XCode.Membership;
|
||||
using XCode.Shards;
|
||||
|
@ -69,6 +70,14 @@ namespace Stardust.Data.Monitors
|
|||
[Map(nameof(AppId))]
|
||||
public String AppName => App + "";
|
||||
|
||||
/// <summary>节点</summary>
|
||||
[XmlIgnore, ScriptIgnore, IgnoreDataMember]
|
||||
public Node Node => Extends.Get(nameof(Node), k => Node.FindByID(NodeId));
|
||||
|
||||
/// <summary>节点</summary>
|
||||
[Map(__.NodeId)]
|
||||
public String NodeName => Node?.Name;
|
||||
|
||||
/// <summary>关联项</summary>
|
||||
[XmlIgnore, IgnoreDataMember]
|
||||
public TraceData Link => Extends.Get(nameof(TraceData), k => FindById(LinkId));
|
||||
|
|
|
@ -108,7 +108,7 @@ public class AppOnlineService
|
|||
// 关联节点
|
||||
if (online.NodeId <= 0)
|
||||
{
|
||||
var node = GetNodeByIP(online.IP);
|
||||
var node = GetNodeByIP(online.IP, ip);
|
||||
node ??= GetOrAddNode(info, online.IP, ip);
|
||||
if (node != null)
|
||||
{
|
||||
|
@ -131,20 +131,29 @@ public class AppOnlineService
|
|||
return online;
|
||||
}
|
||||
|
||||
private Node GetNodeByIP(String ip)
|
||||
private Node GetNodeByIP(String localIp, String ip)
|
||||
{
|
||||
if (ip.IsNullOrEmpty()) return null;
|
||||
if (localIp.IsNullOrEmpty()) return null;
|
||||
|
||||
// 借助缓存,降低IP搜索节点次数
|
||||
if (_cache.TryGetValue<Node>(ip, out var node)) return node;
|
||||
var key = $"{localIp}-{ip}";
|
||||
if (_cache.TryGetValue<Node>(key, out var node)) return node;
|
||||
|
||||
node = Node.SearchByIP(ip).FirstOrDefault();
|
||||
// 根据本地IP找出所有符合节点,再找一个公网IP匹配的
|
||||
var list = Node.SearchByIP(localIp);
|
||||
node = list.FirstOrDefault(e => e.UpdateIP == ip);
|
||||
node ??= list.FirstOrDefault();
|
||||
|
||||
_cache.Add(ip, node, 3600);
|
||||
_cache.Add(key, node, 3600);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>检查或添加节点。主要服务于仅有跟踪数据的客户端接入</summary>
|
||||
/// <param name="inf"></param>
|
||||
/// <param name="localIp"></param>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public Node GetOrAddNode(AppInfo inf, String localIp, String ip)
|
||||
{
|
||||
// 根据节点IP规则,自动创建节点
|
||||
|
@ -154,32 +163,34 @@ public class AppOnlineService
|
|||
using var span = _tracer?.NewSpan("AddNodeForApp", rule);
|
||||
|
||||
var nodes = Node.SearchByIP(localIp);
|
||||
if (nodes.Count == 0)
|
||||
if (nodes.Count > 0) return nodes[0];
|
||||
|
||||
var node = new Node
|
||||
{
|
||||
var node = new Node
|
||||
{
|
||||
Code = Rand.NextString(8),
|
||||
Name = inf?.MachineName ?? rule.Name,
|
||||
ProductCode = inf?.Name ?? "App",
|
||||
Category = rule.Category,
|
||||
IP = localIp,
|
||||
Enable = true,
|
||||
};
|
||||
if (inf != null)
|
||||
{
|
||||
node.Version = inf.Version;
|
||||
node.MachineName = inf.MachineName;
|
||||
node.UserName = inf.UserName;
|
||||
}
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = rule.Category;
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = inf?.Name;
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = node.Code;
|
||||
node.Insert();
|
||||
Code = Rand.NextString(8),
|
||||
Name = inf?.MachineName ?? rule.Name,
|
||||
ProductCode = inf?.Name ?? "App",
|
||||
Category = rule.Category,
|
||||
IP = localIp,
|
||||
Enable = true,
|
||||
|
||||
node.WriteHistory("AppAddNode", true, inf.ToJson(), ip);
|
||||
|
||||
return node;
|
||||
CreateIP = ip,
|
||||
UpdateIP = ip,
|
||||
};
|
||||
if (inf != null)
|
||||
{
|
||||
node.Version = inf.Version;
|
||||
node.MachineName = inf.MachineName;
|
||||
node.UserName = inf.UserName;
|
||||
}
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = rule.Category;
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = inf?.Name;
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = node.Code;
|
||||
node.Insert();
|
||||
|
||||
node.WriteHistory("AppAddNode", true, inf.ToJson(), ip);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -49,53 +49,53 @@ public class RegistryService
|
|||
online.SaveAsync();
|
||||
}
|
||||
|
||||
// 根据节点IP规则,自动创建节点
|
||||
if (online.NodeId == 0)
|
||||
{
|
||||
var node = GetOrAddNode(inf, online.IP, ip);
|
||||
if (node != null)
|
||||
{
|
||||
online.NodeId = node.ID;
|
||||
online.SaveAsync();
|
||||
}
|
||||
}
|
||||
//// 根据节点IP规则,自动创建节点
|
||||
//if (online.NodeId == 0)
|
||||
//{
|
||||
// var node = GetOrAddNode(inf, online.IP, ip);
|
||||
// if (node != null)
|
||||
// {
|
||||
// online.NodeId = node.ID;
|
||||
// online.SaveAsync();
|
||||
// }
|
||||
//}
|
||||
|
||||
return online;
|
||||
}
|
||||
|
||||
public Node GetOrAddNode(AppModel inf, String localIp, String ip)
|
||||
{
|
||||
// 根据节点IP规则,自动创建节点
|
||||
var rule = NodeResolver.Instance.Match(null, localIp);
|
||||
if (rule != null && rule.NewNode)
|
||||
{
|
||||
using var span = _tracer?.NewSpan("AddNodeForApp", rule);
|
||||
//public Node GetOrAddNode(AppModel inf, String localIp, String ip)
|
||||
//{
|
||||
// // 根据节点IP规则,自动创建节点
|
||||
// var rule = NodeResolver.Instance.Match(null, localIp);
|
||||
// if (rule != null && rule.NewNode)
|
||||
// {
|
||||
// using var span = _tracer?.NewSpan("AddNodeForApp", rule);
|
||||
|
||||
var nodes = Node.SearchByIP(localIp);
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
var node = new Node
|
||||
{
|
||||
Code = Rand.NextString(8),
|
||||
Name = rule.Name,
|
||||
ProductCode = inf?.AppName ?? "App",
|
||||
Category = rule.Category,
|
||||
IP = localIp,
|
||||
Version = inf?.Version,
|
||||
Enable = true,
|
||||
};
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = inf?.AppName;
|
||||
if (node.Name.IsNullOrEmpty()) node.Name = node.Code;
|
||||
node.Insert();
|
||||
// var nodes = Node.SearchByIP(localIp);
|
||||
// if (nodes.Count == 0)
|
||||
// {
|
||||
// var node = new Node
|
||||
// {
|
||||
// Code = Rand.NextString(8),
|
||||
// Name = rule.Name,
|
||||
// ProductCode = inf?.AppName ?? "App",
|
||||
// Category = rule.Category,
|
||||
// IP = localIp,
|
||||
// Version = inf?.Version,
|
||||
// Enable = true,
|
||||
// };
|
||||
// if (node.Name.IsNullOrEmpty()) node.Name = inf?.AppName;
|
||||
// if (node.Name.IsNullOrEmpty()) node.Name = node.Code;
|
||||
// node.Insert();
|
||||
|
||||
node.WriteHistory("AppAddNode", true, inf.ToJson(), ip);
|
||||
// node.WriteHistory("AppAddNode", true, inf.ToJson(), ip);
|
||||
|
||||
return node;
|
||||
}
|
||||
}
|
||||
// return node;
|
||||
// }
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
// return null;
|
||||
//}
|
||||
|
||||
public (AppService, Boolean changed) RegisterService(App app, Service service, PublishServiceInfo model, String ip)
|
||||
{
|
||||
|
|
|
@ -57,7 +57,8 @@
|
|||
var url3 = $"?appid={entity.AppId}&clientId={HttpUtility.UrlEncode(entity.ClientId)}";
|
||||
|
||||
// 带缓存,避免重复查询节点
|
||||
if (!nodes.TryGetValue(entity.ClientId + "", out var node))
|
||||
var node = entity.Node;
|
||||
if (node == null && !nodes.TryGetValue(entity.ClientId + "", out node))
|
||||
{
|
||||
var online = AppOnline.FindByClient(entity.ClientId);
|
||||
if (online == null && entity.App != null && entity.App.AppId > 0)
|
||||
|
|
|
@ -6,7 +6,6 @@ using NewLife;
|
|||
using NewLife.Http;
|
||||
using NewLife.Log;
|
||||
using NewLife.Messaging;
|
||||
using NewLife.Net;
|
||||
using NewLife.Remoting;
|
||||
using Stardust.Models;
|
||||
|
||||
|
|
Loading…
Reference in New Issue