瀏覽代碼

新增cap事件总线使用mysql和rabitmq示例

zhontai 2 年之前
父節點
當前提交
3a3691c67d

+ 1 - 1
src/hosts/ZhonTai.Host/Configs/appconfig.json

@@ -83,7 +83,7 @@
   //验证码
   "varifyCode": {
     //启用
-    "enable": false,
+    "enable": true,
     //字体列表
     "fonts": [ "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" ]
   },

+ 32 - 11
src/hosts/ZhonTai.Host/Program.cs

@@ -1,6 +1,10 @@
 using FreeScheduler;
 using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyModel;
 using Microsoft.Extensions.Hosting;
+using Savorboard.CAP.InMemoryMessageQueue;
+using System.Linq;
+using System.Reflection;
 using ZhonTai;
 using ZhonTai.Admin.Core;
 using ZhonTai.Admin.Core.Configs;
@@ -8,25 +12,42 @@ using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Core.Startup;
 using ZhonTai.Admin.Tools.TaskScheduler;
 using ZhonTai.ApiUI;
+using ZhonTai.Common.Helpers;
 
 new HostApp(new HostAppOptions
 {
 	//配置后置服务
 	ConfigurePostServices = context =>
 	{
-        //var appConfig = ConfigHelper.Get<AppConfig>("appconfig", context.Environment.EnvironmentName);
-        //Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
-        //    .Where(a => appConfig.AssemblyNames.Contains(a.Name))
-        //    .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
-        //context.Services.AddCap(config =>
-        //{
-        //    config.UseInMemoryStorage();
-        //    config.UseInMemoryMessageQueue();
-        //    config.UseDashboard();
-        //}).AddSubscriberAssembly(assemblies);
-
         //context.Services.AddTiDb(context);
 
+        //添加cap事件总线
+        var appConfig = ConfigHelper.Get<AppConfig>("appconfig", context.Environment.EnvironmentName);
+        Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
+            .Where(a => appConfig.AssemblyNames.Contains(a.Name))
+            .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
+
+        //var dbConfig = ConfigHelper.Get<DbConfig>("dbconfig", context.Environment.EnvironmentName);
+        //var rabbitMQ = context.Configuration.GetSection("CAP:RabbitMq").Get<RabbitMqConfig>();
+        context.Services.AddCap(config =>
+        {
+            config.UseInMemoryStorage();
+            config.UseInMemoryMessageQueue();
+            
+            //DotNetCore.CAP.MySql
+            //DotNetCore.CAP.RabbitMQ
+            
+            //config.UseMySql(dbConfig.ConnectionString);
+            //config.UseRabbitMQ(mqConfig => {
+            //    mqConfig.HostName = rabbitMQ.HostName;
+            //    mqConfig.Port = rabbitMQ.Port;
+            //    mqConfig.UserName = rabbitMQ.UserName;
+            //    mqConfig.Password = rabbitMQ.Password;
+            //    mqConfig.ExchangeName = rabbitMQ.ExchangeName;
+            //});
+            config.UseDashboard();
+        }).AddSubscriberAssembly(assemblies);
+
         //添加任务调度
         context.Services.AddTaskScheduler(DbKeys.AppDb, options =>
         {

+ 8 - 0
src/hosts/ZhonTai.Host/appsettings.json

@@ -6,6 +6,14 @@
     }
   },
   "AllowedHosts": "*",
+  "CAP": {
+    "RabbitMq": {
+      "HostName": "",
+      "Port": 5672,
+      "UserName": "",
+      "Password": ""
+    }
+  },
   "SlideCaptcha": {
     "ExpirySeconds": 300, // 缓存过期时长
     "StoreageKeyPrefix": "admin:captcha:", // 缓存前缀

+ 2 - 0
src/hosts/ZhonTai.Host/nlog.config

@@ -36,6 +36,8 @@
   <rules>
 	<!--跳过不重要的微软日志-->
 	<logger name="Microsoft.*" maxlevel="Info" final="true" />
+	<!--跳过不重要的CAP日志-->
+	<logger name="DotNetCore.CAP.*" maxlevel="Info" final="true" />
 	<!-- 数据库  -->
 	<logger name="db" writeTo="db" />
     <!-- 调试  -->

+ 27 - 0
src/platform/ZhonTai.Admin/Core/Configs/RabbitMqConfig.cs

@@ -0,0 +1,27 @@
+namespace ZhonTai.Admin.Core.Configs;
+
+/// <summary>
+/// RabbitMq配置
+/// </summary>
+public class RabbitMqConfig
+{
+    /// <summary>
+    /// 主机IP或Url地址
+    /// </summary>
+    public string HostName { get; set; } = string.Empty;
+
+    /// <summary>
+    /// 端口号
+    /// </summary>
+    public int Port { get; set; } = 5672;
+
+    /// <summary>
+    /// 用户名
+    /// </summary>
+    public string UserName { get; set; } = string.Empty;
+
+    /// <summary>
+    ///  密码
+    /// </summary>
+    public string Password { get; set; } = string.Empty;
+}

+ 25 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -865,6 +865,31 @@
             OSS配置列表
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Configs.RabbitMqConfig">
+            <summary>
+            RabbitMq配置
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.RabbitMqConfig.HostName">
+            <summary>
+            主机IP或Url地址
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.RabbitMqConfig.Port">
+            <summary>
+            端口号
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.RabbitMqConfig.UserName">
+            <summary>
+            用户名
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.RabbitMqConfig.Password">
+            <summary>
+             密码
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.UploadConfig">
             <summary>
             上传配置