在實際的系統中,可能須要多臺機器部署;然而,Signalr的鏈接信息是跟站點走的,舉個例子
推送系統部署了A、B兩個服務器,張三訪問A服務器,李四訪問B服務器,當張三經過A服務器向李四推送的時候,A服務器上是找不到李四的鏈接信息的,天然也就推送不過了,這個時候就須要有一個統一協調的玩意,signalr支持多種,Azure、Redis等,本節以Redis做爲底板,介紹如何在Signalr中使用Redis做爲底板來支持橫向擴展。git
Microsoft.AspNetCore.SignalR.StackExchangeRedisgithub
修改Startup中的ConfigureServices方法web
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配置等詳情以下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; } }
對應的配置文件以下json
{ "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做爲底板服務器
更多內容請經過快速導航查看下一篇app
標題 | 內容 |
---|---|
索引 | .net core 3.0 Signalr - 實現一個業務推送系統 |
上一篇 | .net core 3.0 Signalr - 03 使用MessagePack壓縮傳輸內容 |
下一篇 | .net core 3.0 Signalr - 05 使用jwt將用戶跟signalr關聯 |
源碼地址 | 源碼 |
官方文檔 | 官方文檔 |