向服务端发送心跳后,再向本地发送心跳

This commit is contained in:
大石头 2023-10-25 17:57:41 +08:00
parent 364580a2f0
commit 01c5dc797f
8 changed files with 48 additions and 7 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 763 KiB

After

Width:  |  Height:  |  Size: 815 KiB

BIN
Doc/本地通信.emmx Normal file

Binary file not shown.

View File

@ -92,6 +92,11 @@ public class StarService : DisposeBase, IApi
return ai; return ai;
} }
public PingResponse Ping(AppInfo appInfo)
{
return null;
}
/// <summary>设置星尘服务端地址</summary> /// <summary>设置星尘服务端地址</summary>
/// <returns></returns> /// <returns></returns>
[Api(nameof(SetServer))] [Api(nameof(SetServer))]

View File

@ -112,7 +112,7 @@ public class AppMeterController : EntityController<AppMeter>
Title = new ChartTitle { Text = app.Name + "#" + clientId }, Title = new ChartTitle { Text = app.Name + "#" + clientId },
Height = 400, 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.SetY("指标");
chart.YAxis = new[] { chart.YAxis = new[] {
new { name = "指标", type = "value" }, new { name = "指标", type = "value" },

View File

@ -11,8 +11,6 @@ using Stardust.Models;
using Stardust.Registry; using Stardust.Registry;
using Stardust.Services; using Stardust.Services;
using NewLife.Caching; using NewLife.Caching;
using System;
using System.Net.Http;
#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD #if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
using System.Net.WebSockets; using System.Net.WebSockets;
using TaskEx = System.Threading.Tasks.Task; using TaskEx = System.Threading.Tasks.Task;
@ -39,6 +37,9 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
/// <summary>WebSocket长连接。建立长连接后可以实时感知配置更新和注册服务更新默认false</summary> /// <summary>WebSocket长连接。建立长连接后可以实时感知配置更新和注册服务更新默认false</summary>
public Boolean UseWebSocket { get; set; } public Boolean UseWebSocket { get; set; }
/// <summary>星尘工厂</summary>
public StarFactory Factory { get; set; }
private ConcurrentDictionary<String, Delegate> _commands = new(StringComparer.OrdinalIgnoreCase); private ConcurrentDictionary<String, Delegate> _commands = new(StringComparer.OrdinalIgnoreCase);
/// <summary>命令集合</summary> /// <summary>命令集合</summary>
public IDictionary<String, Delegate> Commands => _commands; public IDictionary<String, Delegate> Commands => _commands;
@ -185,7 +186,7 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
/// <summary>心跳</summary> /// <summary>心跳</summary>
/// <returns></returns> /// <returns></returns>
public async Task<Object> Ping() public async Task<Object?> Ping()
{ {
try try
{ {
@ -194,7 +195,7 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
else else
_appInfo.Refresh(); _appInfo.Refresh();
PingResponse rs = null; PingResponse? rs = null;
try try
{ {
rs = await PostAsync<PingResponse>("App/Ping", _appInfo); rs = await PostAsync<PingResponse>("App/Ping", _appInfo);
@ -257,6 +258,17 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
} }
} }
/// <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; private TimeSpan _span;
/// <summary>获取相对于服务器的当前时间,避免两端时间差</summary> /// <summary>获取相对于服务器的当前时间,避免两端时间差</summary>
/// <returns></returns> /// <returns></returns>
@ -391,7 +403,17 @@ public class AppClient : ApiHttpClient, ICommandClient, IRegistry, IEventProvide
if (rs == null) return; if (rs == null) return;
} }
// 向服务端发送心跳后,再向本地发送心跳
try
{
await Ping(); await Ping();
await PingLocal(null);
}
catch
{
await PingLocal(_appInfo);
throw;
}
await RefreshPublish(); await RefreshPublish();
await RefreshConsume(); await RefreshConsume();

View File

@ -113,6 +113,15 @@ public class LocalStarClient
throw; throw;
} }
} }
/// <summary>向StarAgent发送心跳</summary>
/// <returns></returns>
public async Task<PingResponse?> PingAsync(AppInfo? appInfo)
{
Init();
return await _client.InvokeAsync<PingResponse>("Ping", appInfo);
}
#endregion #endregion
#region #region
@ -161,7 +170,11 @@ public class LocalStarClient
var set = NewLife.Setting.Current; var set = NewLife.Setting.Current;
url = set.PluginServer.EnsureEnd("/"); url = set.PluginServer.EnsureEnd("/");
url += "star/"; 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"; url += "staragent60.zip";
else if (Environment.Version.Major >= 5) else if (Environment.Version.Major >= 5)
url += "staragent50.zip"; url += "staragent50.zip";

View File

@ -271,6 +271,7 @@ public class StarFactory : DisposeBase
var client = new AppClient(Server) var client = new AppClient(Server)
{ {
Factory = this,
AppId = AppId, AppId = AppId,
AppName = AppName, AppName = AppName,
ClientId = ClientId, ClientId = ClientId,