|
@@ -0,0 +1,201 @@
|
|
|
+using WingServerCommon.Service;
|
|
|
+using WingInterfaceLibrary.OpLog;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Collections.Generic;
|
|
|
+using WingInterfaceLibrary.Interface;
|
|
|
+using WingInterfaceLibrary.Request.FastestServer;
|
|
|
+using WingInterfaceLibrary.DTO.ServerInfo;
|
|
|
+using WingServerCommon.Interfaces.Cache;
|
|
|
+using System;
|
|
|
+using WingServerCommon.Log;
|
|
|
+using WingInterfaceLibrary.Interface.DBInterface;
|
|
|
+using WingInterfaceLibrary.Request.DBRequest;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace WingCloudServer.InteractionCenter
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 最快服务器响应服务
|
|
|
+ /// </summary>
|
|
|
+ public class FastestServerInteractionCenterService : InteractionCenterService, IFastestServerInteractionCenterService
|
|
|
+ {
|
|
|
+ private readonly IFastestServerManager _fastestServerManager;
|
|
|
+
|
|
|
+ private IDistributedServerInfoDBService _distributedServerInfoDBService;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 默认构造
|
|
|
+ /// </summary>
|
|
|
+ public FastestServerInteractionCenterService()
|
|
|
+ {
|
|
|
+ _fastestServerManager = new FastestServerManager(LoadServerInfoFormDbWithId, LoadServerInfosFormDbWithIds);
|
|
|
+ CacheMaintenance.Instance.Register(typeof(IFastestServerManager), _fastestServerManager);
|
|
|
+ _fastestServerManager.ServerInfoUpdate += OnServerInfoUpdate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据id查询数据列表
|
|
|
+ /// </summary>
|
|
|
+ ServerInformation LoadServerInfoFormDbWithId(string id)
|
|
|
+ {
|
|
|
+ var serverInformationList = GetServerInformationList(new List<string>() { id }).Result;
|
|
|
+ if (serverInformationList?.Count > 0)
|
|
|
+ {
|
|
|
+ return serverInformationList.FirstOrDefault();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据ids查询数据列表
|
|
|
+ /// </summary>
|
|
|
+ IList<ServerInformation> LoadServerInfosFormDbWithIds(IList<string> ids)
|
|
|
+ {
|
|
|
+ var serverInformationList = GetServerInformationList(ids).Result;
|
|
|
+ if (serverInformationList?.Count > 0)
|
|
|
+ {
|
|
|
+ return serverInformationList;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化
|
|
|
+ /// </summary>
|
|
|
+ public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
+ {
|
|
|
+ base.Load(jsonRpcClientPool);
|
|
|
+ _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
|
|
|
+ LoadDBServerInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 加载ServerInfo到内存
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ private void LoadDBServerInfo()
|
|
|
+ {
|
|
|
+ Task.Run(async () =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await Task.Delay(5000);
|
|
|
+ var serverInformationList = await GetServerInformationList(null);
|
|
|
+ _fastestServerManager.LoadFromDbOject(serverInformationList);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineWarn($"FastestServerInteractionCenterService LoadDBServerInfo err, {ex}");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Get Server Information From DB
|
|
|
+ /// </summary>
|
|
|
+ private async Task<IList<ServerInformation>> GetServerInformationList(IList<string> ids)
|
|
|
+ {
|
|
|
+ IList<ServerInformation> serverInformationList = new List<ServerInformation>();
|
|
|
+ if (ids?.Count > 0) {
|
|
|
+ var request = new GetDistributedServerInfoDBPagesRequest(){
|
|
|
+ CurrentPage = 1,
|
|
|
+ PageSize = 1000
|
|
|
+ };
|
|
|
+ var serverPageInfo = await _distributedServerInfoDBService.GetDistributedServerInfoPagesAsync(request);
|
|
|
+ if (serverPageInfo != null && serverPageInfo.TotalCount > 0 && serverPageInfo.PageData != null && serverPageInfo.PageData.Any()) {
|
|
|
+ serverInformationList = serverPageInfo.PageData.Select(d => new ServerInformation{
|
|
|
+ Name = d.Name,
|
|
|
+ Host = d.ServerUrl
|
|
|
+ }).ToList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //根据id查询
|
|
|
+ var request = new GetDistributedServerInfoDBByCodesRequest() {
|
|
|
+ Codes = ids.ToList()
|
|
|
+ };
|
|
|
+ var serverPageInfo = await _distributedServerInfoDBService.GetDistributedServerInfoDBByCodesAsync(request);
|
|
|
+ if (serverPageInfo != null && serverPageInfo.Any()) {
|
|
|
+ serverInformationList = serverPageInfo.Select(d => new ServerInformation{
|
|
|
+ Name = d.Name,
|
|
|
+ Host = d.ServerUrl
|
|
|
+ }).ToList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return serverInformationList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Get op logs from master server
|
|
|
+ /// </summary>
|
|
|
+ private async void OnServerInfoUpdate(object sender, IList<ServerInformation> serverInformations)
|
|
|
+ {
|
|
|
+ foreach (var serverInformation in serverInformations)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ _fastestServerManager.Remove(serverInformation.Code);
|
|
|
+ // var tokenDB = await _tokenDBService.FindTokenByValueAsync(token.Code);
|
|
|
+ // if (tokenDB != null && !string.IsNullOrWhiteSpace(tokenDB.Code))
|
|
|
+ // {
|
|
|
+ // tokenDB.Expiration = token.Expiration;
|
|
|
+ // await _tokenDBService.UpdateTokenAsync(token.Code, tokenDB);
|
|
|
+ // _serverTokenManager.Remove(tokenDB.Code);
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"ServerInfo Update err:{ex}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Get op logs from master server
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns>The op log list</returns>
|
|
|
+ public async Task<bool> UpdateServerInfoToCacheAsync(UpdateServerInfoRequest request)
|
|
|
+ {
|
|
|
+ //这里应该是批量移除,请求参数要改
|
|
|
+ if (request.Codes != null && request.Codes.Any())
|
|
|
+ {
|
|
|
+ foreach (var code in request.Codes)
|
|
|
+ {
|
|
|
+ _fastestServerManager.Remove(code);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Synchronize op log to master server
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<List<ServerInfoDTO>> GetServerInfoFromCacheAsync(QueryServerInfoRequest request)
|
|
|
+ {
|
|
|
+ //这里应该是批量查询,请求参数要改
|
|
|
+ var list = new List<ServerInfoDTO>();
|
|
|
+ IList<ServerInformation> serverInformationList = new List<ServerInformation>();
|
|
|
+ if (request.Codes != null && request.Codes.Any())
|
|
|
+ {
|
|
|
+ serverInformationList = _fastestServerManager.GetServerInfosByCodes(request.Codes);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ serverInformationList = _fastestServerManager.GetAllServerInfos();
|
|
|
+ }
|
|
|
+ if (serverInformationList != null && serverInformationList.Any())
|
|
|
+ {
|
|
|
+ list = serverInformationList.Select(c => new ServerInfoDTO() {
|
|
|
+ Name = c.Name,
|
|
|
+ Host = c.Host
|
|
|
+ }).ToList();
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|