Skip to content

Commit

Permalink
向服务端发送心跳后,再向本地发送心跳
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 25, 2023
1 parent 364580a commit 01c5dc7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 7 deletions.
Binary file modified Doc/星尘分布式.emmx
Binary file not shown.
Binary file modified Doc/星尘分布式.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Doc/本地通信.emmx
Binary file not shown.
5 changes: 5 additions & 0 deletions StarAgent/StarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public AgentInfo Info(AgentInfo info)
return ai;
}

public PingResponse Ping(AppInfo appInfo)
{
return null;
}

/// <summary>设置星尘服务端地址</summary>
/// <returns></returns>
[Api(nameof(SetServer))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected override IEnumerable<AppMeter> Search(Pager p)
Title = new ChartTitle { Text = app.Name + "#" + clientId },
Height = 400,
};
chart.SetX(list2, _.CreateTime, e => e.CreateTime.ToString("HH:mm"));
chart.SetX(list2, _.Time, e => e.Time.ToString("HH:mm"));
//chart.SetY("指标");
chart.YAxis = new[] {
new { name = "指标", type = "value" },
Expand Down
32 changes: 27 additions & 5 deletions Stardust/AppClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using Stardust.Registry;
using Stardust.Services;
using NewLife.Caching;
using System;
using System.Net.Http;
#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
using System.Net.WebSockets;
using TaskEx = System.Threading.Tasks.Task;
Expand All @@ -39,6 +37,9 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
/// <summary>WebSocket长连接。建立长连接后,可以实时感知配置更新和注册服务更新,默认false</summary>
public Boolean UseWebSocket { get; set; }

/// <summary>星尘工厂</summary>
public StarFactory Factory { get; set; }

private ConcurrentDictionary<String, Delegate> _commands = new(StringComparer.OrdinalIgnoreCase);
/// <summary>命令集合</summary>
public IDictionary<String, Delegate> Commands => _commands;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void Start()

/// <summary>心跳</summary>
/// <returns></returns>
public async Task<Object> Ping()
public async Task<Object?> Ping()
{
try
{
Expand All @@ -194,7 +195,7 @@ public async Task<Object> Ping()
else
_appInfo.Refresh();

PingResponse rs = null;
PingResponse? rs = null;
try
{
rs = await PostAsync<PingResponse>("App/Ping", _appInfo);
Expand Down Expand Up @@ -257,6 +258,17 @@ public async Task<Object> Ping()
}
}

/// <summary>向本地StarAgent发送心跳</summary>
/// <param name="appInfo"></param>
/// <returns></returns>
public async Task<Object?> PingLocal(AppInfo? appInfo)
{
var local = Factory?.Local;
if (local == null || local.Info == null) return null;

return await local.PingAsync(appInfo);
}

private TimeSpan _span;
/// <summary>获取相对于服务器的当前时间,避免两端时间差</summary>
/// <returns></returns>
Expand Down Expand Up @@ -391,7 +403,17 @@ private async Task DoPing(Object state)
if (rs == null) return;
}

await Ping();
// 向服务端发送心跳后,再向本地发送心跳
try
{
await Ping();
await PingLocal(null);
}
catch
{
await PingLocal(_appInfo);
throw;
}

await RefreshPublish();
await RefreshConsume();
Expand Down
15 changes: 14 additions & 1 deletion Stardust/LocalStarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ private void Init()
throw;
}
}

/// <summary>向StarAgent发送心跳</summary>
/// <returns></returns>
public async Task<PingResponse?> PingAsync(AppInfo? appInfo)
{
Init();

return await _client.InvokeAsync<PingResponse>("Ping", appInfo);
}
#endregion

#region 进程控制
Expand Down Expand Up @@ -161,7 +170,11 @@ public Boolean ProbeAndInstall(String? url = null, String? version = null, Strin
var set = NewLife.Setting.Current;
url = set.PluginServer.EnsureEnd("/");
url += "star/";
if (Environment.Version.Major >= 6)
if (Environment.Version.Major >= 8)
url += "staragent80.zip";
else if (Environment.Version.Major >= 7)
url += "staragent70.zip";
else if (Environment.Version.Major >= 6)
url += "staragent60.zip";
else if (Environment.Version.Major >= 5)
url += "staragent50.zip";
Expand Down
1 change: 1 addition & 0 deletions Stardust/StarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ private Boolean Valid()

var client = new AppClient(Server)
{
Factory = this,
AppId = AppId,
AppName = AppName,
ClientId = ClientId,
Expand Down

0 comments on commit 01c5dc7

Please sign in to comment.