Skip to content

Commit

Permalink
[feat] StarAgent启动后从配置中心获取PluginServer,并通过AgentInfo提供给本地应用
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 10, 2023
1 parent a1c23ed commit 59e5f39
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 69 deletions.
9 changes: 8 additions & 1 deletion StarAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ public void StartFactory()
if (_factory == null)
{
var server = StarSetting.Server;
if (!server.IsNullOrEmpty()) _factory = new StarFactory(server, "StarAgent", null);
if (!server.IsNullOrEmpty())
{
_factory = new StarFactory(server, "StarAgent", null);

// 激活配置中心,获取PluginServer
var config = _factory.GetConfig();
if (config != null) ThreadPoolX.QueueUserWorkItem(() => config.LoadAll());
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions StarAgent/StarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public AgentInfo Info(AgentInfo info)
ai.Services = Manager?.Services.Select(e => e.Name).ToArray();
ai.Code = AgentSetting.Code;

// 返回插件服务器地址
var core = NewLife.Setting.Current;
if (!core.PluginServer.IsNullOrEmpty() && !core.PluginServer.Contains("x.newlifex.com"))
{
ai.PluginServer = core.PluginServer;
}

return ai;
}

Expand Down
28 changes: 14 additions & 14 deletions Stardust/AppClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
public String AppId { get; set; }

/// <summary>应用名</summary>
public String AppName { get; set; }
public String? AppName { get; set; }

/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
public String ClientId { get; set; }
public String? ClientId { get; set; }

/// <summary>节点编码</summary>
public String NodeCode { get; set; }
public String? NodeCode { get; set; }

/// <summary>WebSocket长连接。建立长连接后,可以实时感知配置更新和注册服务更新,默认false</summary>
public Boolean UseWebSocket { get; set; }
Expand All @@ -49,8 +49,8 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
/// <summary>最大失败数。超过该数时,新的数据将被抛弃,默认120</summary>
public Int32 MaxFails { get; set; } = 120;

private AppInfo _appInfo;
private readonly String _version;
private AppInfo? _appInfo;
private readonly String? _version;

/// <summary>已发布服务,记录下来,定时注册刷新</summary>
private readonly ConcurrentDictionary<String, PublishServiceInfo> _publishServices = new();
Expand Down Expand Up @@ -146,10 +146,10 @@ public void Start()
StartTimer();
}

private String _appName;
private String? _appName;
/// <summary>注册</summary>
/// <returns></returns>
public async Task<Object> Register()
public async Task<Object?> Register()
{
try
{
Expand All @@ -174,7 +174,7 @@ public async Task<Object> Register()
catch (Exception ex)
{
if (ex is HttpRequestException)
Log?.Info("注册异常[{0}] {1}", Source, ex.GetTrue().Message);
Log?.Info("注册异常[{0}] {1}", Source, ex.GetTrue()?.Message);
else
Log?.Info(ex.ToString());

Expand Down Expand Up @@ -340,7 +340,7 @@ public virtual Boolean WriteEvent(String type, String name, String remark)
#endregion

#region 长连接
private TimerX _timer;
private TimerX? _timer;
private void StartTimer()
{
if (_timer == null)
Expand Down Expand Up @@ -428,8 +428,8 @@ private async Task DoPing(Object state)
}

#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
private WebSocket _websocket;
private CancellationTokenSource _source;
private WebSocket? _websocket;
private CancellationTokenSource? _source;
private async Task DoPull(WebSocket socket, CancellationToken cancellationToken)
{
DefaultSpan.Current = null;
Expand Down Expand Up @@ -523,7 +523,7 @@ protected virtual async Task OnReceiveCommand(CommandModel model)
/// <summary>上报服务调用结果</summary>
/// <param name="model"></param>
/// <returns></returns>
public virtual async Task<Object> CommandReply(CommandReplyModel model) => await PostAsync<Object>("App/CommandReply", model);
public virtual async Task<Object?> CommandReply(CommandReplyModel model) => await PostAsync<Object>("App/CommandReply", model);
#endregion

#region 发布、消费
Expand Down Expand Up @@ -597,7 +597,7 @@ public PublishServiceInfo CreatePublishService(String serviceName)
/// <param name="tag">特性标签</param>
/// <param name="health">健康监测接口地址</param>
/// <returns></returns>
public async Task<PublishServiceInfo> RegisterAsync(String serviceName, String address, String tag = null, String health = null)
public async Task<PublishServiceInfo> RegisterAsync(String serviceName, String address, String? tag = null, String? health = null)
{
if (address == null) throw new ArgumentNullException(nameof(address));

Expand All @@ -619,7 +619,7 @@ public async Task<PublishServiceInfo> RegisterAsync(String serviceName, String a
/// <param name="tag">特性标签</param>
/// <param name="health">健康监测接口地址</param>
/// <returns></returns>
public PublishServiceInfo Register(String serviceName, Func<String> addressCallback, String tag = null, String health = null)
public PublishServiceInfo Register(String serviceName, Func<String> addressCallback, String? tag = null, String? health = null)
{
if (addressCallback == null) throw new ArgumentNullException(nameof(addressCallback));

Expand Down
15 changes: 8 additions & 7 deletions Stardust/Configs/StarHttpConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@
using Stardust.Models;
using Stardust.Registry;
using Stardust.Services;
using static System.Net.WebRequestMethods;

namespace Stardust.Configs;

internal class StarHttpConfigProvider : HttpConfigProvider
{
public ConfigInfo ConfigInfo { get; set; }
public ConfigInfo? ConfigInfo { get; set; }

const String REGISTRY = "$Registry:";
private Boolean _useWorkerId;

protected override IDictionary<String, Object> GetAll()
protected override IDictionary<String, Object?>? GetAll()
{
try
{
var rs = base.GetAll();
var inf = Info;

if (rs != null && rs.Count > 0)
if (rs != null && rs.Count > 0 && inf != null)
{
var inf = Info;
var ci = ConfigInfo = JsonHelper.Convert<ConfigInfo>(inf);

if (ci != null && ci.Configs != null && ci.Configs.Count > 0)
Expand Down Expand Up @@ -56,7 +55,9 @@ protected override IDictionary<String, Object> GetAll()
{
var asm = AssemblyX.Entry;
var set = NewLife.Setting.Current;
if (!svr.IsNullOrEmpty() && !svr.EqualIgnoreCase(set.PluginServer) && !asm.Name.EqualIgnoreCase("StarWeb", "StarServer"))
if (!svr.IsNullOrEmpty() &&
!svr.EqualIgnoreCase(set.PluginServer) &&
(asm == null || !asm.Name.EqualIgnoreCase("StarWeb", "StarServer")))
{
XTrace.WriteLine("插件服务器PluginServer变更为 {0}", svr);
set.PluginServer = svr;
Expand Down Expand Up @@ -86,7 +87,7 @@ protected override IDictionary<String, Object> GetAll()
/// <param name="key"></param>
/// <param name="createOnMiss"></param>
/// <returns></returns>
protected override IConfigSection Find(String key, Boolean createOnMiss)
protected override IConfigSection? Find(String key, Boolean createOnMiss)
{
if (key.StartsWithIgnoreCase(REGISTRY))
{
Expand Down
22 changes: 12 additions & 10 deletions Stardust/LocalStarClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using NewLife;
using NewLife.Http;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Net;
using NewLife.Remoting;
using Stardust.Models;
#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
Expand All @@ -22,16 +21,16 @@ public class LocalStarClient
{
#region 属性
/// <summary>本机代理信息</summary>
public AgentInfo Info { get; private set; }
public AgentInfo? Info { get; private set; }

/// <summary>本地服务端地址</summary>
public String Server { get; set; }
public String? Server { get; set; }

/// <summary>本地星尘代理服务地址</summary>
public static Int32 Port { get; set; } = 5500;

private AgentInfo _local;
private ApiClient _client;
private AgentInfo? _local;
private ApiClient? _client;
#endregion

#region 构造
Expand All @@ -44,6 +43,9 @@ public LocalStarClient()
#endregion

#region 方法
#if !NET40
[MemberNotNull(nameof(_client))]
#endif
private void Init()
{
if (_client != null) return;
Expand All @@ -63,7 +65,7 @@ private void Init()

/// <summary>获取信息</summary>
/// <returns></returns>
public AgentInfo GetInfo()
public AgentInfo? GetInfo()
{
//!!! 通过进程名判断是否存在,可能会误判,因为获取其它dotnet进程命令行需要管理员权限
//// 检测进程是否存在,如果进程都不存在,没必要获取信息
Expand Down Expand Up @@ -94,7 +96,7 @@ public AgentInfo GetInfo()

/// <summary>获取信息</summary>
/// <returns></returns>
public async Task<AgentInfo> GetInfoAsync()
public async Task<AgentInfo?> GetInfoAsync()
{
Init();

Expand Down Expand Up @@ -363,7 +365,7 @@ private Boolean RunAgentOnDotnet(String fileName, String target, Boolean inServi
/// <param name="url">zip包下载源</param>
/// <param name="version">版本号</param>
/// <param name="target">目标目录</param>
public static Task ProbeAsync(String url = null, String version = null, String target = null)
public static Task ProbeAsync(String? url = null, String? version = null, String? target = null)
{
return TaskEx.Run(() =>
{
Expand Down Expand Up @@ -470,6 +472,6 @@ public static IEnumerable<AgentInfo> Scan(AgentInfo local = null, Int32 timeout
/// <summary>写日志</summary>
/// <param name="format"></param>
/// <param name="args"></param>
public void WriteLog(String format, params Object[] args) => Log?.Info(format, args);
public void WriteLog(String format, params Object?[] args) => Log?.Info(format, args);
#endregion
}
19 changes: 11 additions & 8 deletions Stardust/Models/AgentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ public class AgentInfo
public Int32 ProcessId { get; set; }

/// <summary>进程名称</summary>
public String ProcessName { get; set; }
public String? ProcessName { get; set; }

/// <summary>版本</summary>
public String Version { get; set; }
public String? Version { get; set; }

/// <summary>文件路径</summary>
public String FileName { get; set; }
public String? FileName { get; set; }

/// <summary>命令参数</summary>
public String Arguments { get; set; }
public String? Arguments { get; set; }

/// <summary>本地IP地址</summary>
public String IP { get; set; }
public String? IP { get; set; }

/// <summary>服务端地址</summary>
public String Server { get; set; }
public String? Server { get; set; }

/// <summary>插件服务器</summary>
public String? PluginServer { get; set; }

/// <summary>节点编码</summary>
public String Code { get; set; }
public String? Code { get; set; }

/// <summary>应用服务</summary>
public String[] Services { get; set; }
public String[]? Services { get; set; }
#endregion

#region 构造
Expand Down
12 changes: 6 additions & 6 deletions Stardust/Monitors/StarTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ public class StarTracer : DefaultTracer
{
#region 属性
/// <summary>应用标识</summary>
public String AppId { get; set; }
public String? AppId { get; set; }

/// <summary>应用名</summary>
public String AppName { get; set; }
public String? AppName { get; set; }

/// <summary>实例。应用可能多实例部署,ip@proccessid</summary>
public String ClientId { get; set; }
public String? ClientId { get; set; }

/// <summary>最大失败数。超过该数时,新的数据将被抛弃,默认120</summary>
public Int32 MaxFails { get; set; } = 120;

/// <summary>要排除的操作名</summary>
public String[] Excludes { get; set; }
public String[]? Excludes { get; set; }

/// <summary>Api客户端</summary>
public IApiClient Client { get; set; }
public IApiClient? Client { get; set; }

/// <summary>剔除埋点调用自己。默认true</summary>
public Boolean TrimSelf { get; set; } = true;
Expand Down Expand Up @@ -285,7 +285,7 @@ public void AttachGlobal()
/// <summary>全局注册星尘性能追踪器</summary>
/// <param name="server">星尘监控中心地址,为空时自动从本地探测</param>
/// <returns></returns>
public static StarTracer Register(String server = null)
public static StarTracer? Register(String? server = null)
{
if (server.IsNullOrEmpty())
{
Expand Down
Loading

0 comments on commit 59e5f39

Please sign in to comment.