Browse Source

Merge branch 'master' of http://git.ius.plus/Project-Wing/WingCloudServer

Jeremy 2 years ago
parent
commit
4756e3bdbc

+ 201 - 0
src/InteractionCenter/FastestServerInteractionCenterService.cs

@@ -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;
+        }           
+    }
+}

+ 95 - 0
src/InteractionCenter/IFastestServerManager.cs

@@ -0,0 +1,95 @@
+using System.Threading.Tasks;
+using System.Collections.Generic;
+using WingInterfaceLibrary.DTO.ServerInfo;
+using WingServerCommon.Interfaces.Cache;
+using System;
+using System.Linq;
+
+namespace WingCloudServer.InteractionCenter
+{
+    interface IFastestServerManager : IBaseCacheManager<ServerInformation>
+    {
+        /// <summary>
+        /// Get All ServerInfos 
+        /// </summary>
+        /// <returns></returns>
+        IList<ServerInformation> GetAllServerInfos();   
+
+        /// <summary>
+        /// Get ServerInfos with specified client id
+        /// </summary>
+        /// <param name="clientId">User code, admin code or device code</param>
+        /// <returns></returns>
+        IList<ServerInformation> GetServerInfos(string code);       
+
+        // <summary>
+        /// Get tokens with specified client ids
+        /// </summary>
+        /// <param name="clientId">User code, admin code or device code</param>
+        /// <returns></returns>
+        IList<ServerInformation> GetServerInfosByCodes(List<string> codes);
+
+        /// <summary>
+        /// The ServerInfo info update event
+        /// </summary>
+        event EventHandler<IList<ServerInformation>> ServerInfoUpdate;
+    }
+    
+    /// <summary>
+    /// Internal Server token, just used in server session module
+    /// </summary>
+    internal class ServerInformation : ServerInfoDTO, ICacheObject
+    {
+        public ServerInformation()
+        {
+
+        }
+
+        //
+        // 摘要:
+        //     The token string value
+        public string Code { get; set; }
+    }
+
+    
+    internal class FastestServerManager : CacheManager<ServerInformation>, IFastestServerManager
+    {
+        private bool _isWatching = true;
+        public event EventHandler<IList<ServerInformation>> ServerInfoUpdate;
+
+        public FastestServerManager(Func<string, ServerInformation> loadCacheFromDbWithId, Func<IList<string>, IList<ServerInformation>> loadCachesFromDbWithIds) : base(loadCacheFromDbWithId, loadCachesFromDbWithIds)
+        {
+
+        }
+
+        /// <summary>
+        /// Get tokens with specified client id
+        /// </summary>
+        /// <param name="clientId">User code, admin code or device code</param>
+        /// <returns></returns>
+        public IList<ServerInformation> GetServerInfos(string code)
+        {
+            return Where(x => x.Code == code)?.ToList() ?? new List<ServerInformation>();
+        }
+
+        /// <summary>
+        /// Get tokens with specified client ids
+        /// </summary>
+        /// <param name="clientId">User code, admin code or device code</param>
+        /// <returns></returns>
+        public IList<ServerInformation> GetServerInfosByCodes(List<string> codes)
+        {
+            return Where(x => codes.Contains(x.Code))?.ToList() ?? new List<ServerInformation>();
+        }
+
+        /// <summary>
+        /// Get tokens with specified client id
+        /// </summary>
+        /// <param name="clientId">User code, admin code or device code</param>
+        /// <returns></returns>
+        public IList<ServerInformation> GetAllServerInfos()
+        {
+            return Where(x => !string.IsNullOrEmpty(x.Name) && !string.IsNullOrEmpty(x.Host))?.ToList() ?? new List<ServerInformation>();
+        }
+    }
+}

+ 3 - 3
src/appsettings.json

@@ -6,11 +6,11 @@
     "MasterUrl": ""
   },
   "Gateway": {
-    "Host": "http://172.21.0.15:8303/"
+    "Host": "http://192.168.6.69/"    
   },
   "Log": {
     "Level": "info",
-    "Debug": false
+    "Debug": true
   },
   "Services": {
     "InProcess": "WingEmailModule,WingMongoDBModule,WingSessionModule,WingSMSModule,WingNotificationModule,WingAIDiagnosisService",
@@ -50,7 +50,7 @@
     "Sercret":"4sQhdJBl5HI7f3oLbZIlwqXOyILyLWP0",
     "Bucket": "flyinsono-bj-1300984704",
     "CFSServerUrl":"https://",
-    "CDNServerUrl":"http://cdn-bj.fis.plus/"
+    "CDNServerUrl":"http://cdn-bj.fis.plus"
   },
   "Authentication": {
     "Admin" : 60,