Explorar o código

监控IP文件热更新重新加载内存中

MIke %!s(int64=2) %!d(string=hai) anos
pai
achega
215467bdc3

+ 65 - 36
src/InteractionCenter/VinnoServerService.cs

@@ -17,6 +17,7 @@ using WingInterfaceLibrary.DB.Request;
 using System.IO;
 using WingServerCommon.Config;
 using WingServerCommon.Config.Parameters;
+using WingCloudServer.Loader;
 
 namespace WingCloudServer.InteractionCenter
 {
@@ -31,7 +32,9 @@ namespace WingCloudServer.InteractionCenter
 
         private ITokenDBService _tokenDBService;
 
-        private string _folderBase = AppDomain.CurrentDomain.BaseDirectory + "Resource\\IP\\"; 
+        private IPFilesWatcher _iPFilesWatcher;//监控IP文件热更新重新加载内存中
+
+        private string _folderBase = AppDomain.CurrentDomain.BaseDirectory + "Resource\\IP\\";
 
         private bool _isEnableIPLocation = ConfigurationManager.GetParammeter<BoolParameter>("IPLocation", "Enable").Value;
 
@@ -148,6 +151,33 @@ namespace WingCloudServer.InteractionCenter
             LoadDBServerInfo();
         }
 
+        /// <summary>
+        /// 加载IP文件
+        /// </summary>
+        private void LoadDataIPFiles()
+        {
+            if (_isEnableIPLocation)
+            {
+                Task.Run(() =>
+                           {
+                               try
+                               {
+
+                                   var ipManager = CacheMaintenance.Instance.Get<IIPManager>();
+                                   if (ipManager != null)
+                                   {
+                                       ipManager.LoadData(_folderBase);
+                                       Logger.WriteLineInfo($"VinnoServerService LoadDataIPFiles end");
+                                   }
+                               }
+                               catch (Exception ex)
+                               {
+                                   Logger.WriteLineWarn($"VinnoServerService LoadDataIPFiles err, {ex}");
+                               }
+                           });
+            }
+
+        }
         /// <summary>
         /// 加载ServerInfo到内存
         /// </summary>
@@ -158,44 +188,41 @@ namespace WingCloudServer.InteractionCenter
            {
                try
                {
-                    int tryCount = 1;
-                    IList<CacheDistributedServerInfosDTO> serverInformationList = null;
-                    while (true)
-                    {
-                        try
-                        {
-                            serverInformationList = await GetServerInformationList(null);
-                        }
-                        catch (Exception ex)
-                        {
-                            Logger.WriteLineError($"ServerInfo Cache Init Error:{ex}");
-                        }
-                        if (serverInformationList != null)
-                        {
-                            break;
-                        }
-                        tryCount++;
-                        if (tryCount > 10)
-                        {
-                            break;
-                        }
-                        await Task.Delay(500);
-                    }
-                    CacheMaintenance.Instance.Get<IDistributedServerInfosManager>().LoadFromDbOject(serverInformationList);
-                    if (_isEnableIPLocation) 
-                    {
-                        var ipManager = CacheMaintenance.Instance.Get<IIPManager>();
-                        if (ipManager != null)
-                        {
-                            ipManager.LoadData(_folderBase);
-                        }
-                    }
+                   int tryCount = 1;
+                   IList<CacheDistributedServerInfosDTO> serverInformationList = null;
+                   while (true)
+                   {
+                       try
+                       {
+                           serverInformationList = await GetServerInformationList(null);
+                       }
+                       catch (Exception ex)
+                       {
+                           Logger.WriteLineError($"ServerInfo Cache Init Error:{ex}");
+                       }
+                       if (serverInformationList != null)
+                       {
+                           break;
+                       }
+                       tryCount++;
+                       if (tryCount > 10)
+                       {
+                           break;
+                       }
+                       await Task.Delay(500);
+                   }
+                   CacheMaintenance.Instance.Get<IDistributedServerInfosManager>().LoadFromDbOject(serverInformationList);
                }
                catch (Exception ex)
                {
                    Logger.WriteLineWarn($"VinnoServerService LoadDBServerInfo err, {ex}");
                }
            });
+            LoadDataIPFiles();
+            if (_isEnableIPLocation) //IP 文件变动热更新
+            {
+                _iPFilesWatcher=new IPFilesWatcher(_folderBase, LoadDataIPFiles);
+            }
         }
 
         /// <summary>
@@ -250,7 +277,8 @@ namespace WingCloudServer.InteractionCenter
         /// <returns>是否成功</returns>
         public async Task<bool> UpdateServerIPListAsync(UpdateServerIPListRequest request)
         {
-            if (string.IsNullOrEmpty(request.Name) || request.IpRange == null || request.IpRange.Count <= 0) {
+            if (string.IsNullOrEmpty(request.Name) || request.IpRange == null || request.IpRange.Count <= 0)
+            {
                 Logger.WriteLineError($"UpdateServerIPListAsync Empty Error");
                 return false;
             }
@@ -267,7 +295,7 @@ namespace WingCloudServer.InteractionCenter
                 var endIp = newIpRange[1];
                 var longStartIp = IpToLong(startIp);
                 var longEndIp = IpToLong(endIp);
-                var ipAddressInfo = new IPAddressInfoDTO() 
+                var ipAddressInfo = new IPAddressInfoDTO()
                 {
                     StartIp = startIp,
                     EndIp = endIp,
@@ -276,7 +304,8 @@ namespace WingCloudServer.InteractionCenter
                 };
                 ipRangeList.Add(ipAddressInfo);
             }
-            var dbRequest = new UpdateServerInfoDBRequest() {
+            var dbRequest = new UpdateServerInfoDBRequest()
+            {
                 Name = request.Name,
                 IpRangeList = ipRangeList
             };

+ 23 - 0
src/Loader/IPFilesWatcher.cs

@@ -0,0 +1,23 @@
+using Microsoft.Extensions.FileProviders;
+using Microsoft.Extensions.Primitives;
+using System;
+
+namespace WingCloudServer.Loader
+{
+    public class IPFilesWatcher
+    {
+        private IFileProvider _fileProvider;
+        /// <summary>
+        /// IP文件的监视加载
+        /// </summary>
+        /// <param name="path"></param>
+        /// <param name="ipFilesLoaderAction"></param>
+        public IPFilesWatcher(string path, Action ipFilesLoaderAction)
+        {
+            _fileProvider = new PhysicalFileProvider(path);
+            ChangeToken.OnChange(
+                () => _fileProvider.Watch("*.json"),
+                 ipFilesLoaderAction);
+        }
+    }
+}

+ 0 - 1
src/Plugin/ServerListPlugin.cs

@@ -95,7 +95,6 @@ namespace WingCloudServer.Plugin
                 {
                     ipAddress = context.RemoteEndPoint?.Address.ToString();
                 }
-
                 var serverInformationList = new List<CacheDistributedServerInfosDTO>();
                 List<ServerInfoDTO> fastServerList = new List<ServerInfoDTO>();
                 //Logger.WriteLineInfo($"Client Request Ip Address:{ipAddress},IsDistributed:" + ConfigurationManager.IsDistributed);