Explorar o código

支持多Redis

林豪 左 %!s(int64=2) %!d(string=hai) anos
pai
achega
f47bd751d7

+ 5 - 0
WCS.Core/Configs.cs

@@ -18,6 +18,11 @@ namespace WCS.Core
         /// </summary>
         public static List<DataBaseConnectionString>? DbConnectionStrings { get; set; } = null!;
 
+        /// <summary>
+        /// Redis连接字符串集合
+        /// </summary>
+        public static List<DataBaseConnectionString>? RedisConnectionStrings { get; set; } = null!;
+
         #endregion 配置中心
 
         /// <summary>

+ 35 - 6
WCS.Core/Redis/RedisHelper.cs

@@ -18,15 +18,29 @@ namespace WCS.Core.Redis
         /// <summary>
         /// 默认上下文类类型
         /// </summary>
-        public static string DefaultDbContextType { get; private set; } = null!;
+        public static string DefaultContextType { get; private set; } = null!;
+
+        /// <summary>
+        /// 默认上下文类类型
+        /// </summary>
+        public static string MonitorContextType { get; private set; } = null!;
 
         /// <summary>
         /// 设置默认链接
         /// </summary>
         /// <param name="key"></param>
-        public static void SetDefaultDbContextType(string key)
+        public static void SetDefaultContextType(string key)
+        {
+            DefaultContextType = key;
+        }
+
+        /// <summary>
+        /// 设置监控链接
+        /// </summary>
+        /// <param name="key"></param>
+        public static void SetMonitorContextType(string key)
         {
-            DefaultDbContextType = key;
+            MonitorContextType = key;
         }
 
         /// <summary>
@@ -37,9 +51,24 @@ namespace WCS.Core.Redis
             get
             {
                 {
-                    if (DefaultDbContextType == null)
+                    if (DefaultContextType == null)
                         throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
-                    return Context(DefaultDbContextType);
+                    return Context(DefaultContextType);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 监控连接
+        /// </summary>
+        public static RedisClient Monitor
+        {
+            get
+            {
+                {
+                    if (MonitorContextType == null)
+                        throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
+                    return Context(MonitorContextType);
                 }
             }
         }
@@ -59,7 +88,7 @@ namespace WCS.Core.Redis
             RedisClients.Add(ctx);
             if (defaultClient)
             {
-                SetDefaultDbContextType(key);
+                SetDefaultContextType(key);
             }
             return ctx.Client;
         }

+ 0 - 1
WCS.Service/Program.cs

@@ -61,7 +61,6 @@ namespace WCS.Service
                     })
                     .ConfigureServices((_, services) =>
                     {
-                        //services.AddDbContext<WCSDB>();
                         services.AddHostedService<Worker>();
                         services.AddHttpClient();
                     });

+ 35 - 16
WCS.Service/ProtocolProxy.cs

@@ -92,19 +92,38 @@ namespace WCS.Service
         static ProtocolProxy()
         {
             MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
-            //RedisHelper.CreateContext("127.0.0.1,database=0", "default", true);
-            RedisHelper.CreateContext("127.0.0.1,database=10", "3D");
-            RedisHelper.CreateContext("127.0.0.1,database=1", "ConcurrencyControlRedis");
-            RedisHelper.Default.Serialize = obj =>
+
+            #region 初始化Redis连接
+
+            var redisConnectionStrings = RedisHelper.Default.Check("RedisConnectionStrings") ?? throw new Exception("请在Redis中配置RedisConnectionStrings");
+            Configs.RedisConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);
+            if (Configs.RedisConnectionStrings != null)
             {
-                var bytes = MessagePackSerializer.Serialize(obj);
-                return bytes;
-            };
-            RedisHelper.Default.DeserializeRaw = (bytes, type) =>
+                if (Configs.RedisConnectionStrings.All(v => v.Key != "Monitor")) throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");
+            }
+
+            foreach (var redisConnection in Configs.RedisConnectionStrings!)
             {
-                var obj = MessagePackSerializer.Deserialize(type, bytes);
-                return obj;
-            };
+                RedisHelper.CreateContext(redisConnection.ConnectionString, redisConnection.Key);
+                switch (redisConnection.Key)
+                {
+                    case "Monitor":
+                        RedisHelper.SetMonitorContextType(redisConnection.Key);
+                        RedisHelper.Monitor.Serialize = obj =>
+                        {
+                            var bytes = MessagePackSerializer.Serialize(obj);
+                            return bytes;
+                        };
+                        RedisHelper.Monitor.DeserializeRaw = (bytes, type) =>
+                        {
+                            var obj = MessagePackSerializer.Deserialize(type, bytes);
+                            return obj;
+                        };
+                        break;
+                }
+            }
+
+            #endregion 初始化Redis连接
         }
 
         public override void Publish(string code, WCS_PROTOCOLDATA obj)
@@ -185,23 +204,23 @@ namespace WCS.Service
 
                 var sw = new Stopwatch();
                 sw.Start();
-                RedisHelper.Default.Set(nameof(DeviceDataPack), pack);
-                RedisHelper.Default.RPush("Packs", pack);
+                RedisHelper.Monitor.Set(nameof(DeviceDataPack), pack);
+                RedisHelper.Monitor.RPush("Packs", pack);
                 sw.Stop();
                 Console.ForegroundColor = ConsoleColor.Blue;
                 Console.WriteLine($"Redis耗时{sw.ElapsedMilliseconds}");
                 Console.ResetColor();
 
-                var len = RedisHelper.Default.LLen("Packs");
+                var len = RedisHelper.Monitor.LLen("Packs");
                 if (len > 150000)
                 {
-                    RedisHelper.Default.LTrim("Packs", 20000, len);
+                    RedisHelper.Monitor.LTrim("Packs", 20000, len);
                 }
 
                 #endregion 存入Redis
 
                 var converter = new System.Text.UnicodeEncoding();
-               
+
                 var plcRawData = new PlcRawData
                 {
                     CONTENT = converter.GetBytes(JsonConvert.SerializeObject(pack)),

+ 1 - 1
WCS.WorkEngineering/Extensions/DeviceExtension.cs

@@ -542,7 +542,7 @@ namespace WCS.Service.Extensions
         /// <returns></returns>
         public void BcrStationIsForbid()
         {
-            var config = RedisHelper.Default.Get("ForbidTubuEnter").Split(",");
+            var config = RedisHelper.Default.Get("ForbidTubuEnter")?.Split(",");
             if (config.Contains(Entity.CODE)) throw new WarnException("当前入库口已被禁用,请联系机修人员了解具体情况");
         }
     }