在實際的系統中,可能需要多臺機器部署;然而,Signalr的連接資訊是跟站點走的,舉個例子
推送系統部署了A、B兩個服務器,張三訪問A服務器,李四訪問B服務器,當張三通過A服務器向李四推送的時候,A服務器上是找不到李四的連接資訊的,自然也就推送不過了,這個時候就需要有一個統一協調的玩意,signalr支持多種,Azure、Redis等,本節以Redis作為底板,介紹如何在Signalr中使用Redis作為底板來支持橫向擴展,
引入Redis
- 先引入NuGet包
Microsoft.AspNetCore.SignalR.StackExchangeRedis - 修改Startup中的ConfigureServices方法
var appSection = Configuration.GetSection("App"); services.Configure<AppSetting>(option => appSection.Bind(option)); var appSetting = appSection.Get<AppSetting>(); // 添加Signalr services.AddSignalR(config => { if (_webEnv.IsDevelopment()) { config.EnableDetailedErrors = true; } }) // 支持MessagePack .AddMessagePackProtocol() // 使用redis做底板 支持橫向擴展 Scale-out .AddStackExchangeRedis(o => { o.ConnectionFactory = async writer => { var config = new ConfigurationOptions { AbortOnConnectFail = false, ChannelPrefix = "__signalr_", }; config.DefaultDatabase = appSetting.SignalrRedisCache.DatabaseId; var connection = await ConnectionMultiplexer.ConnectAsync(appSetting.SignalrRedisCache.ConnectionString, writer); connection.ConnectionFailed += (_, e) => { Console.WriteLine("Connection to Redis failed."); }; if (connection.IsConnected) { Console.WriteLine("connected to Redis."); } else { Console.WriteLine("Did not connect to Redis"); } return connection; }; });
可以自定義Redis相關配置,此處的appSetting為已經定義好的配置物體,包括了,主要配置、CROS配置、Jwt配置、redis配置等詳情如下
/// <summary>
/// 對應appsettings中的App節點的配置資訊
/// </summary>
public class AppSetting
{
public JwtSetting JwtSetting { set;get;}
public RedisCache RedisCache { set;get;}
public RedisCache SignalrRedisCache { set; get; }
public string CORS { set;get;}
/// <summary>
/// 是否主站點(用于運行清理任務等)
/// </summary>
public bool MainSite { set;get;}
}
/// <summary>
/// JWT設定
/// </summary>
public class JwtSetting
{
/// <summary>
/// 發行者 表示token是誰頒發的
/// </summary>
public string Issuer { set; get; }
/// <summary>
/// 表示哪些客戶端可以使用這個token
/// </summary>
public string Audience { set; get; }
/// <summary>
/// 加密的Key 必須大于16位
/// </summary>
public string SecretKey { set; get; }
}
public class RedisCache
{
public string ConnectionString { set;get;}
public int DatabaseId { set; get; }
}
對應的組態檔如下
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore.SignalR": "Trace",
"Microsoft.AspNetCore.Http.Connections": "Trace"
}
},
"App": {
"RedisCache": {
"ConnectionString": "127.0.0.1:6379,password=#####,ssl=false,abortConnect=true,connectTimeout=5000",
"DatabaseId": 10
},
"SignalrRedisCache": {
"ConnectionString": "127.0.0.1:6379,password=#####,ssl=false,abortConnect=true,connectTimeout=5000",
"DatabaseId": 10
},
"CORS": "https://localhost:60000,http://localhost:60001",
"MainSite": true,
"JwtSetting": {
"Issuer": "http://localhost:50000", //頒發者
"Audience": "http://localhost:50000", //使用者
"SecretKey": "Wetrial20196666666" // key 大于16位
}
}
}
首先,將組態檔跟物體物件映射,下次在其他地方使用的時候可以直接通過DI注入,然后通過AddStackExchangeRedis配置使用redis作為底板
更多內容請通過快速導航查看下一篇
快速導航
| 標題 | 內容 |
|---|---|
| 索引 | .net core 3.0 Signalr - 實作一個業務推送系統 |
| 上一篇 | .net core 3.0 Signalr - 03 使用MessagePack壓縮傳輸內容 |
| 下一篇 | .net core 3.0 Signalr - 05 使用jwt將用戶跟signalr關聯 |
| 原始碼地址 | 原始碼 |
| 官方檔案 | 官方檔案 |

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/110620.html
標籤:.NET Core
