Просмотр исходного кода

!799 SignalR增加集群配置
Merge pull request !799 from koy0755/SignalR增加集群配置

zuohuaijun 2 лет назад
Родитель
Сommit
a6b1653618

+ 3 - 0
Admin.NET/Admin.NET.Application/Admin.NET.Application.csproj

@@ -36,6 +36,9 @@
 	  <None Update="Configuration\App.json">
 		  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 	  </None>
+	  <None Update="Configuration\Cluster.json">
+	    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+	  </None>
 	  <None Update="Configuration\Captcha.json">
 	    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 	  </None>

+ 2 - 2
Admin.NET/Admin.NET.Application/Configuration/Cache.json

@@ -1,9 +1,9 @@
-{
+{
   "$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
 
   "Cache": {
     "Prefix": "adminnet_", // 全局缓存前缀
-    "CacheType": "Memory", // Memory、Redis
+    "CacheType": "Redis", // Memory、Redis
     "Redis": {
       "Configuration": "server=127.0.0.1:6379;password=;db=5;", // Redis连接字符串
       "Prefix": "adminnet_" // Redis前缀(目前没用)

+ 12 - 0
Admin.NET/Admin.NET.Application/Configuration/Cluster.json

@@ -0,0 +1,12 @@
+{
+  "$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
+  "Cluster": { // 集群配置
+    "EnableCluster": true, //启用了集群,应该把cache.json也使用redis方式,oss.json中不能使用本地方式保存文件
+    "ServerId": "Cluster1", //服务器标识,有些接口可能需要记录一下从哪个节点执行的
+    "ServerIp": "", //服务器IP,有些接口可能需要记录一下从哪个IP执行的,然后调用其它的服务(预留)
+    "SignalR": { //配置Redis保存Signal的连接信息
+      "RedisConfiguration": "127.0.0.1:6379,ssl=false,password=,defaultDatabase=5",
+      "ChannelPrefix": "signalrPrefix_"
+    }
+  }
+}

+ 1 - 0
Admin.NET/Admin.NET.Web.Core/Admin.NET.Web.Core.csproj

@@ -13,6 +13,7 @@
 
   <ItemGroup>
     <PackageReference Include="IGeekFan.AspNetCore.Knife4jUI" Version="0.0.16" />
+    <PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="6.0.22" />
     <PackageReference Include="System.Security.Cryptography.Pkcs" Version="7.0.3" />
   </ItemGroup>
 

+ 26 - 8
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -1,4 +1,4 @@
-// 麻省理工学院许可证
+// 麻省理工学院许可证
 //
 // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司  联系电话/微信:18020030720  QQ:515096995
 //
@@ -155,13 +155,31 @@ public class Startup : AppStartup
         // 模板引擎
         services.AddViewEngine();
 
-        // 即时通讯
-        services.AddSignalR(options =>
-            {
-                options.KeepAliveInterval = TimeSpan.FromSeconds(5);
-            })
-            .AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings));
-
+        // 即时通讯
+        // 如果支持集群,把SignalR配置地为支持集群的模式
+        if (App.GetConfig<bool>("Cluster:EnableCluster"))
+        {
+            string redisConnectionString = App.GetConfig<string>("Cluster:SignalR:RedisConfiguration");
+            string channelPrefix = App.GetConfig<string>("Cluster:SignalR:ChannelPrefix");
+            services.AddSignalR(options =>
+                {
+                    options.KeepAliveInterval = TimeSpan.FromSeconds(5);
+                })
+                .AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings))
+                .AddStackExchangeRedis(redisConnectionString, options =>
+                {
+                    options.Configuration.ChannelPrefix = channelPrefix;
+                });
+
+        }
+        else
+        {
+            services.AddSignalR(options =>
+                {
+                    options.KeepAliveInterval = TimeSpan.FromSeconds(5);
+                })
+                .AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings));
+        }
         // 系统日志
         services.AddLoggingSetup();