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

添加hangfire的redis持久化连接

yuqian лет назад: 8
Родитель
Сommit
4209b34000

+ 1 - 2
Demo/WebApplication1/Controllers/ValuesController.cs

@@ -60,8 +60,7 @@ namespace WebApplication1.Controllers
60 60
         public string Continuations(string jobId)
61 61
         {
62 62
             BackgroundJob.ContinueWith(
63
-                jobId,
64
-                 () => Console.WriteLine("Continuation!"));
63
+                jobId, () => Console.WriteLine("Continuation!"));
65 64
             return $"延续性任务执行  Continuation ";
66 65
         }
67 66
         #endregion

+ 1 - 1
Demo/WebApplication1/Properties/PublishProfiles/FolderProfile.pubxml.user

@@ -6,6 +6,6 @@
6 6
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7 7
   <PropertyGroup>
8 8
     <TimeStampOfAssociatedLegacyPublishXmlFile />
9
-    <_PublishTargetUrl>E:\Project\DemoProject\DemoHangfire\WebApplication1\bin\Release\PublishOutput</_PublishTargetUrl>
9
+    <_PublishTargetUrl>E:\Project\WorkProject\Service-TaskScheduling\Demo\WebApplication1\bin\Release\PublishOutput</_PublishTargetUrl>
10 10
   </PropertyGroup>
11 11
 </Project>

+ 20 - 23
Demo/WebApplication1/Startup.cs

@@ -12,8 +12,8 @@ using Hangfire;
12 12
 using WebApplication1.Filter;
13 13
 using NLog.Extensions.Logging;
14 14
 using NLog.Web;
15
-
16 15
 using System.Transactions;
16
+using StackExchange.Redis;
17 17
 
18 18
 namespace WebApplication1
19 19
 {
@@ -30,24 +30,19 @@ namespace WebApplication1
30 30
         public void ConfigureServices(IServiceCollection services)
31 31
         {
32 32
             services.AddMvc();
33
-            services.AddHangfire(x => x.UseSqlServerStorage("Data Source=.;User ID=sa;pwd=123456;Initial Catalog=HangFire;"));
34
-
35
-
36
-            //services.AddHangfire(x => x.UseStorage(new MySqlStorage("Driver={MySQL};Server=localhost;Option=16834;Database=hangfire;", new MySqlStorageOptions
37
-            //{
38
-            //    TransactionIsolationLevel = IsolationLevel.ReadCommitted,
39
-            //    QueuePollInterval = TimeSpan.FromSeconds(15),
40
-            //    JobExpirationCheckInterval = TimeSpan.FromHours(1),
41
-            //    CountersAggregateInterval = TimeSpan.FromMinutes(5),
42
-            //    PrepareSchemaIfNecessary = true,
43
-            //    DashboardJobListLimit = 50000,
44
-            //    TransactionTimeout = TimeSpan.FromMinutes(1),
45
-            //}
46
-            //)));
47 33
 
34
+            //添加 Hangfire 组件,并且指定使用 SQL Server 进行持久化存储
35
+            services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("hangfire.sqlserver")));
48 36
 
37
+            //添加 Hangfire 组件,并且指定使用 Redis 进行持久化存储
38
+            services.AddHangfire(x =>
39
+            {
40
+                //从appsettings中获取 Redis 连接字符串
41
+                x.UseRedisStorage(ConnectionMultiplexer.Connect(Configuration.GetConnectionString("hangfire.redis")));
42
+            });
49 43
 
50 44
 
45
+            //添加日志组件
51 46
             services.AddLogging();
52 47
         }
53 48
 
@@ -62,24 +57,26 @@ namespace WebApplication1
62 57
 
63 58
 
64 59
             #region HangFire
60
+            //启动hangfire面板
61
+            app.UseHangfireDashboard();
62
+            //配置hangfire面板访问权限
63
+            var options = new DashboardOptions
64
+            {
65
+                Authorization = new[] { new HangfireAuthorizationFilter() }
66
+            };
67
+            app.UseHangfireDashboard("/hangfire", options);
65 68
 
66
-            app.UseHangfireDashboard();//启动hangfire面板
67
-
69
+            //hangfire 配置项
68 70
             var jobOptions = new BackgroundJobServerOptions
69 71
             {
70 72
                 Queues = new[] { "test", "default" },//队列名称,只能为小写
71 73
                 WorkerCount = Environment.ProcessorCount * 5, //并发任务数
72 74
                 ServerName = "hangfire1",//服务器名称
75
+
73 76
             };
74 77
             //启动Hangfire服务
75 78
             app.UseHangfireServer(jobOptions);
76 79
 
77
-            var options = new DashboardOptions
78
-            {
79
-                Authorization = new[] { new HangfireAuthorizationFilter() }
80
-            };
81
-            app.UseHangfireDashboard("/hangfire", options);
82
-
83 80
             #endregion
84 81
 
85 82
             #region NLog

+ 4 - 1
Demo/WebApplication1/WebApplication1.csproj

@@ -11,7 +11,8 @@
11 11
   <ItemGroup>
12 12
     <PackageReference Include="HangFire" Version="1.6.17" />
13 13
     <PackageReference Include="Hangfire.Autofac.Core" Version="1.0.1" />
14
-    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
14
+    <PackageReference Include="Hangfire.Redis.StackExchange.StrongName" Version="1.7.0" />
15
+    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
15 16
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
16 17
   </ItemGroup>
17 18
 
@@ -23,4 +24,6 @@
23 24
     <None Include="StaticConfig\nlog.config" />
24 25
   </ItemGroup>
25 26
 
27
+  <ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="" /></VisualStudio></ProjectExtensions>
28
+
26 29
 </Project>

+ 1 - 1
Demo/WebApplication1/WebApplication1.csproj.user

@@ -5,7 +5,7 @@
5 5
   </PropertyGroup>
6 6
   <PropertyGroup>
7 7
     <ActiveDebugProfile>IIS Express</ActiveDebugProfile>
8
-    <ShowAllFiles>true</ShowAllFiles>
8
+    <ShowAllFiles>false</ShowAllFiles>
9 9
     <NameOfLastUsedPublishProfile>FolderProfile</NameOfLastUsedPublishProfile>
10 10
   </PropertyGroup>
11 11
 </Project>

+ 4 - 0
Demo/WebApplication1/appsettings.json

@@ -11,5 +11,9 @@
11 11
         "Default": "Warning"
12 12
       }
13 13
     }
14
+  },
15
+  "ConnectionStrings": {
16
+    "hangfire.redis": "localhost,abortConnect=false",
17
+    "hangfire.sqlserver": "Data Source=.;User ID=sa;pwd=123456;Initial Catalog=HangFire;"
14 18
   }
15 19
 }

+ 1 - 1
Demo/WebApplication2/Properties/PublishProfiles/FolderProfile.pubxml.user

@@ -6,6 +6,6 @@
6 6
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7 7
   <PropertyGroup>
8 8
     <TimeStampOfAssociatedLegacyPublishXmlFile />
9
-    <_PublishTargetUrl>E:\Project\DemoProject\DemoHangfire\WebApplication2\bin\Release\PublishOutput</_PublishTargetUrl>
9
+    <_PublishTargetUrl>E:\Project\WorkProject\Service-TaskScheduling\Demo\WebApplication2\bin\Release\PublishOutput</_PublishTargetUrl>
10 10
   </PropertyGroup>
11 11
 </Project>