denny 2 years ago
parent
commit
0c66e90312
1 changed files with 81 additions and 144 deletions
  1. 81 144
      src/InteractionCenter/VinnoServerService.cs

+ 81 - 144
src/InteractionCenter/VinnoServerService.cs

@@ -23,8 +23,6 @@ namespace WingCloudServer.InteractionCenter
     /// </summary>
     public class VinnoServerService : InteractionCenterService, IVinnoServerService
     {
-        private readonly IVinnoServerManager _fastestServerManager;
-
         private IDistributedServerInfoDBService _distributedServerInfoDBService;
 
         private string _fastServerHost = ConfigurationManager.GetParammeter<StringParameter>("FastServer", "ServerHost").Value;
@@ -40,15 +38,13 @@ namespace WingCloudServer.InteractionCenter
         /// </summary>
         public VinnoServerService()
         {
-            _fastestServerManager = new VinnoServerManager(LoadServerInfoFormDbWithId, LoadServerInfosFormDbWithIds);
-            CacheMaintenance.Instance.Register(typeof(IVinnoServerManager), _fastestServerManager);
-            _fastestServerManager.ServerInfoUpdate += OnServerInfoUpdate;
+            CacheMaintenance.Instance.Get<IDistributedServerInfosManager>().RegisterDBFunction(LoadServerInfoFormDbWithId, LoadServerInfosFormDbWithIds);
         }
 
         /// <summary>
         /// 根据id查询数据列表
         /// </summary>
-        ServerInformation LoadServerInfoFormDbWithId(string id)
+        private CacheDistributedServerInfosDTO LoadServerInfoFormDbWithId(string id)
         {
             var serverInformationList = GetServerInformationList(new List<string>() { id }).Result;
             if (serverInformationList?.Count > 0)
@@ -61,7 +57,7 @@ namespace WingCloudServer.InteractionCenter
         /// <summary>
         /// 根据ids查询数据列表
         /// </summary>
-        IList<ServerInformation> LoadServerInfosFormDbWithIds(IList<string> ids)
+        private IList<CacheDistributedServerInfosDTO> LoadServerInfosFormDbWithIds(IList<string> ids)
         {
             
             var serverInformationList = GetServerInformationList(ids).Result;
@@ -72,6 +68,64 @@ namespace WingCloudServer.InteractionCenter
             return null;
         }
 
+        /// <summary>
+        /// Get Server Information From DB
+        /// </summary>
+        private async Task<IList<CacheDistributedServerInfosDTO>> GetServerInformationList(IList<string> ids) 
+        {
+            try 
+            {
+                IList<CacheDistributedServerInfosDTO> serverInformationList = new List<CacheDistributedServerInfosDTO>();
+                if (_distributedServerInfoDBService == null) 
+                {
+                    throw new Exception("MongoDB Init Error");
+                }
+                if (ids == null || 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 CacheDistributedServerInfosDTO
+                        {
+                            Code = d.ServerCode,
+                            Name = d.Name,
+                            IsMaster = d.IsMaster,
+                            Description = d.Description,
+                            Enable = d.Enable,
+                            ServerType = (int)d.ServerType,
+                            ServerUrl = 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 CacheDistributedServerInfosDTO
+                        {
+                            Code = d.ServerCode,
+                            Name = d.Name,
+                            IsMaster = d.IsMaster,
+                            Description = d.Description,
+                            Enable = d.Enable,
+                            ServerType = (int)d.ServerType,
+                            ServerUrl = d.ServerUrl,
+                        }).ToList();
+                    }
+                }
+                return serverInformationList;
+            }
+            catch (Exception ex) 
+            {
+                throw ex;
+            }
+        }
+
         /// <summary>
         /// 初始化
         /// </summary>
@@ -81,7 +135,7 @@ namespace WingCloudServer.InteractionCenter
             _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();       
             _authenticationService = GetProxy<IAuthenticationService>();  
             _tokenDBService = GetProxy<ITokenDBService>();  
-            LoadDBServerInfo();   
+            //LoadDBServerInfo();   
             InitFastestServerService(_fastServerHost);           
         }
 
@@ -99,21 +153,6 @@ namespace WingCloudServer.InteractionCenter
             //异步监听客户端请求,当客户端的网络请求到来时会自动执行Result委托
             //该委托没有返回值,有一个IAsyncResult接口的参数,可通过该参数获取context对象
             _listener.BeginGetContext(Result, null);
-            // Task.Run(async () =>
-            // {
-            //     while (true)
-            //     {
-            //         try
-            //         {
-            //             var context = await _listener.GetContextAsync().ConfigureAwait(false);
-            //             HandleHttpContext(context);
-            //         }
-            //         catch (Exception ex)
-            //         {
-            //             Logger.WriteLineError($"ListenFastestServerError:{ex}");
-            //         }
-            //     }
-            // });
         }
 
         /// <summary>
@@ -171,7 +210,7 @@ namespace WingCloudServer.InteractionCenter
                     //拦截,处理doc文档
                     if (absPath.Contains("EchoRequest"))
                     {
-                        var echoResult = new FastestServerBaseDTO();
+                        var echoResult = new EchoResult();
                         ResponseContent(context, true, echoResult);
                         return;
                     }
@@ -180,10 +219,13 @@ namespace WingCloudServer.InteractionCenter
                         await LoginSuccess(context);
                         return;
                     }
-                    else if (absPath.Contains("GetServerListRequest"))
+                    else if (absPath.Contains("ServerListRequest"))
                     {
-                        var req = new QueryServerInfoRequest();
-                        var model = new FastestServerDTO()
+                        var req = new QueryServerInfoRequest()
+                        {
+                            Codes = new List<string>()
+                        };
+                        var model = new ServerListResult()
                         {
                             IsDistributed = ConfigurationManager.IsDistributed,
                             ServerList = await GetServerInfoFromCacheAsync(req)
@@ -232,8 +274,8 @@ namespace WingCloudServer.InteractionCenter
                 var res = await _tokenDBService.UpdateTokenAsync(tokenInfo.Code, tokenInfo);
                 if (res) 
                 {
-                    _fastestServerManager.Remove(tokenInfo.Code);
-                    var response = new FastestServerBaseDTO();
+                    CacheMaintenance.Instance.Get<IDistributedServerInfosManager>().Remove(tokenInfo.Code);
+                    var response = new LoginSuccessResult();
                     ResponseContent(context, true, response);
                     return true;
                 }
@@ -309,7 +351,7 @@ namespace WingCloudServer.InteractionCenter
             {
                 if (data == null) 
                 {
-                    data = new FastestServerBaseDTO() 
+                    data = new BaseServerResult() 
                     { 
                         Code = 1 
                     };
@@ -333,116 +375,8 @@ namespace WingCloudServer.InteractionCenter
                 Logger.WriteLineError($"ResponseContent Error:{ex}");
             }
         }
-
-
         #endregion 
 
-        /// <summary>
-        /// 加载ServerInfo到内存
-        /// </summary>
-        /// <returns></returns>
-        private void LoadDBServerInfo()
-        {
-            Task.Run(async () =>
-           {
-               try
-               {
-                    //await Task.Delay(5000);
-                    IList<ServerInformation> serverInformationList = null;
-                    while (true) 
-                    {
-                        try 
-                        {
-                            serverInformationList = await GetServerInformationList(null);
-                        }
-                        catch (Exception ex) 
-                        {
-                            Logger.WriteLineError($"ServerInfo Cache Init Error:{ex}");
-                        }
-                        if (serverInformationList != null) 
-                        {
-                            break;
-                        }
-                        await Task.Delay(500);
-                    }
-                    _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) 
-        {
-            try 
-            {
-                IList<ServerInformation> serverInformationList = new List<ServerInformation>();
-                if (_distributedServerInfoDBService == null) 
-                {
-                    throw new Exception("MongoDB Init Error");
-                }
-                if (ids == null || 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
-                        {
-                            Code = d.ServerCode,
-                            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
-                        {
-                            Code = d.ServerCode,
-                            Name = d.Name,
-                            Host = d.ServerUrl
-                        }).ToList();
-                    }
-                }
-                return serverInformationList;
-            }
-            catch (Exception ex) 
-            {
-                throw ex;
-            }
-        }
-
-        /// <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);
-                }
-                catch (Exception ex)
-                {
-                    Logger.WriteLineError($"ServerInfo Update err:{ex}");
-                }
-            }
-
-        }
-        
         /// <summary>
         /// Get op logs from master server
         /// </summary>
@@ -455,7 +389,7 @@ namespace WingCloudServer.InteractionCenter
             {
                 foreach (var code in request.Codes) 
                 {
-                    _fastestServerManager.Remove(code);
+                    CacheMaintenance.Instance.Get<IDistributedServerInfosManager>().Remove(code);
                 }
                 return true;
             }
@@ -471,20 +405,23 @@ namespace WingCloudServer.InteractionCenter
         {            
             //这里应该是批量查询,请求参数要改
             var list = new List<ServerInfoDTO>();
-            IList<ServerInformation> serverInformationList = new List<ServerInformation>();
+            var serverInformationList = new List<CacheDistributedServerInfosDTO>();
             if (request.Codes != null  && request.Codes.Any()) 
             {
-                serverInformationList = _fastestServerManager.GetServerInfosByCodes(request.Codes);
+                serverInformationList = CacheMaintenance.Instance.Get<IDistributedServerInfosManager>()
+                .Where(x => request.Codes.Contains(x.Code))?.ToList();
             }
             else
             {
-                serverInformationList = _fastestServerManager.GetAllServerInfos();
+                serverInformationList = CacheMaintenance.Instance.Get<IDistributedServerInfosManager>()
+                .Where(x => !string.IsNullOrEmpty(x.Name) && !string.IsNullOrEmpty(x.ServerUrl))?.ToList();
             }
             if (serverInformationList != null && serverInformationList.Any()) 
             {
-                list = serverInformationList.Select(c => new ServerInfoDTO() {
+                list = serverInformationList.Select(c => new ServerInfoDTO() 
+                {
                     Name = c.Name,
-                    Host = c.Host
+                    Host = c.ServerUrl
                 }).ToList();
             }
             return list;