StarAgent向应用提供节点编码,应用登录星尘服务端时,关联节点
This commit is contained in:
parent
ebcc96f32b
commit
c6cce57a37
|
@ -187,7 +187,8 @@ namespace StarAgent
|
|||
Service = this,
|
||||
Host = Host,
|
||||
Manager = _Manager,
|
||||
Setting = StarSetting,
|
||||
StarSetting = StarSetting,
|
||||
AgentSetting = AgentSetting,
|
||||
Log = XTrace.Log
|
||||
}, null);
|
||||
|
||||
|
|
|
@ -35,7 +35,10 @@ namespace StarAgent
|
|||
public ServiceManager Manager { get; set; }
|
||||
|
||||
/// <summary>星尘设置</summary>
|
||||
public StarSetting Setting { get; set; }
|
||||
public StarSetting StarSetting { get; set; }
|
||||
|
||||
/// <summary>星尘代理设置</summary>
|
||||
public StarAgent.Setting AgentSetting { get; set; }
|
||||
#endregion
|
||||
|
||||
#region 业务
|
||||
|
@ -46,7 +49,7 @@ namespace StarAgent
|
|||
{
|
||||
XTrace.WriteLine(info.ToJson());
|
||||
|
||||
var set = Setting;
|
||||
var set = StarSetting;
|
||||
// 使用对方送过来的星尘服务端地址
|
||||
if (set.Server.IsNullOrEmpty() && !info.Server.IsNullOrEmpty())
|
||||
{
|
||||
|
@ -68,6 +71,7 @@ namespace StarAgent
|
|||
var ai = AgentInfo.GetLocal();
|
||||
ai.Server = set.Server;
|
||||
ai.Services = Manager?.Services.Select(e => e.Name).ToArray();
|
||||
ai.Code = AgentSetting.Code;
|
||||
|
||||
return ai;
|
||||
}
|
||||
|
|
|
@ -47,6 +47,12 @@ namespace Stardust.Data
|
|||
|
||||
if (isNew && !Dirtys[__.AutoActive]) AutoActive = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 已重载。显示友好名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override String ToString() => !DisplayName.IsNullOrEmpty() ? DisplayName : Name;
|
||||
#endregion
|
||||
|
||||
#region 扩展属性
|
||||
|
|
|
@ -9,6 +9,7 @@ using NewLife.Log;
|
|||
using NewLife.Serialization;
|
||||
using Stardust.Data;
|
||||
using Stardust.Data.Configs;
|
||||
using Stardust.Data.Nodes;
|
||||
using Stardust.Models;
|
||||
using Stardust.Server.Common;
|
||||
using Stardust.Server.Models;
|
||||
|
@ -65,10 +66,10 @@ namespace Stardust.Server.Controllers
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region 心跳
|
||||
#region 注册&心跳
|
||||
[ApiFilter]
|
||||
[HttpPost(nameof(Ping))]
|
||||
public PingResponse Ping(AppPingInfo inf)
|
||||
[HttpPost(nameof(Register))]
|
||||
public PingResponse Register(AppModel inf)
|
||||
{
|
||||
var rs = new PingResponse
|
||||
{
|
||||
|
@ -85,11 +86,48 @@ namespace Stardust.Server.Controllers
|
|||
app.UpdateIP = ip;
|
||||
app.SaveAsync();
|
||||
|
||||
//rs.Period = app.Period;
|
||||
|
||||
var clientId = inf.ClientId;
|
||||
if (clientId.IsNullOrEmpty()) clientId = _clientId;
|
||||
if (!clientId.IsNullOrEmpty())
|
||||
{
|
||||
var olt = AppOnline.GetOrAdd(clientId);
|
||||
olt.AppId = app.Id;
|
||||
olt.Name = app.Name;
|
||||
olt.Category = app.Category;
|
||||
olt.Version = inf.Version;
|
||||
olt.Token = _token;
|
||||
olt.PingCount++;
|
||||
if (olt.CreateIP.IsNullOrEmpty()) olt.CreateIP = ip;
|
||||
olt.Creator = Environment.MachineName;
|
||||
|
||||
// 关联节点
|
||||
var node = Node.FindByCode(inf.NodeCode);
|
||||
if (node != null) olt.NodeId = node.ID;
|
||||
|
||||
olt.SaveAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
[ApiFilter]
|
||||
[HttpPost(nameof(Ping))]
|
||||
public PingResponse Ping(AppInfo inf)
|
||||
{
|
||||
var rs = new PingResponse
|
||||
{
|
||||
//Time = inf.Time,
|
||||
ServerTime = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
var app = _app;
|
||||
if (app != null)
|
||||
{
|
||||
var ip = UserHost;
|
||||
|
||||
var clientId = _clientId;
|
||||
if (!clientId.IsNullOrEmpty())
|
||||
{
|
||||
var olt = AppOnline.GetOrAdd(clientId);
|
||||
olt.Name = app.Name;
|
||||
|
@ -100,8 +138,7 @@ namespace Stardust.Server.Controllers
|
|||
if (olt.CreateIP.IsNullOrEmpty()) olt.CreateIP = ip;
|
||||
olt.Creator = Environment.MachineName;
|
||||
|
||||
//olt.Save(null, inf, token, ip);
|
||||
olt.Fill(app, inf.Info);
|
||||
olt.Fill(app, inf);
|
||||
olt.SaveAsync();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,8 @@ namespace Stardust
|
|||
/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
|
||||
public String ClientId { get; set; }
|
||||
|
||||
///// <summary>客户端</summary>
|
||||
//public IApiClient Client { get; set; }
|
||||
/// <summary>节点编码</summary>
|
||||
public String NodeCode { get; set; }
|
||||
|
||||
private ConcurrentDictionary<String, Delegate> _commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
/// <summary>命令集合</summary>
|
||||
|
@ -100,9 +100,38 @@ namespace Stardust
|
|||
#region 方法
|
||||
/// <summary>开始客户端</summary>
|
||||
public void Start() => StartTimer();
|
||||
#endregion
|
||||
|
||||
#region 心跳
|
||||
/// <summary>注册</summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Object> Register()
|
||||
{
|
||||
try
|
||||
{
|
||||
var inf = new AppModel
|
||||
{
|
||||
AppId = AppId,
|
||||
AppName = AppName,
|
||||
ClientId = ClientId,
|
||||
Version = _version,
|
||||
NodeCode = NodeCode,
|
||||
};
|
||||
var rs = await PostAsync<PingResponse>("App/Register", inf);
|
||||
if (rs != null)
|
||||
{
|
||||
// 由服务器改变采样频率
|
||||
if (rs.Period > 0) _timer.Period = rs.Period * 1000;
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XTrace.WriteLine("心跳异常 {0}", ex.GetTrue().Message);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>心跳</summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Object> Ping()
|
||||
|
@ -114,15 +143,7 @@ namespace Stardust
|
|||
else
|
||||
_appInfo.Refresh();
|
||||
|
||||
var inf = new AppPingInfo
|
||||
{
|
||||
AppId = AppId,
|
||||
AppName = AppName,
|
||||
ClientId = ClientId,
|
||||
Version = _version,
|
||||
Info = _appInfo
|
||||
};
|
||||
var rs = await PostAsync<PingResponse>("App/Ping", inf);
|
||||
var rs = await PostAsync<PingResponse>("App/Ping", _appInfo);
|
||||
if (rs != null)
|
||||
{
|
||||
// 由服务器改变采样频率
|
||||
|
@ -174,6 +195,7 @@ namespace Stardust
|
|||
{
|
||||
DefaultSpan.Current = null;
|
||||
|
||||
await Register();
|
||||
await Ping();
|
||||
|
||||
var svc = _currentService;
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using NewLife;
|
||||
using NewLife.Http;
|
||||
using NewLife.Log;
|
||||
|
|
|
@ -27,17 +27,16 @@ namespace Stardust.Models
|
|||
/// <summary>命令参数</summary>
|
||||
public String Arguments { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 本地IP地址
|
||||
/// </summary>
|
||||
/// <summary>本地IP地址</summary>
|
||||
public String IP { get; set; }
|
||||
|
||||
/// <summary>服务端地址</summary>
|
||||
public String Server { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 应用服务
|
||||
/// </summary>
|
||||
/// <summary>节点编码</summary>
|
||||
public String Code { get; set; }
|
||||
|
||||
/// <summary>应用服务</summary>
|
||||
public String[] Services { get; set; }
|
||||
#endregion
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
namespace Stardust.Models
|
||||
{
|
||||
/// <summary>应用心跳信息</summary>
|
||||
public class AppPingInfo
|
||||
/// <summary>应用模型</summary>
|
||||
public class AppModel
|
||||
{
|
||||
/// <summary>应用标识</summary>
|
||||
public String AppId { get; set; }
|
||||
|
@ -12,10 +12,10 @@
|
|||
/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
|
||||
public String ClientId { get; set; }
|
||||
|
||||
/// <summary>节点编码</summary>
|
||||
public String NodeCode { get; set; }
|
||||
|
||||
/// <summary>版本</summary>
|
||||
public String Version { get; set; }
|
||||
|
||||
/// <summary>应用信息</summary>
|
||||
public AppInfo Info { get; set; }
|
||||
}
|
||||
}
|
|
@ -187,6 +187,7 @@ namespace Stardust
|
|||
AppId = AppId,
|
||||
AppName = AppName,
|
||||
ClientId = ClientId,
|
||||
NodeCode = Local?.Info?.Code,
|
||||
Filter = _tokenFilter
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue