Przeglądaj źródła

添加hangfire的redis持久化连接

yuqian 8 lat temu
rodzic
commit
4209b34000

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

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

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

6
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7
   <PropertyGroup>
7
   <PropertyGroup>
8
     <TimeStampOfAssociatedLegacyPublishXmlFile />
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
   </PropertyGroup>
10
   </PropertyGroup>
11
 </Project>
11
 </Project>

+ 20 - 23
Demo/WebApplication1/Startup.cs

12
 using WebApplication1.Filter;
12
 using WebApplication1.Filter;
13
 using NLog.Extensions.Logging;
13
 using NLog.Extensions.Logging;
14
 using NLog.Web;
14
 using NLog.Web;
15
-
16
 using System.Transactions;
15
 using System.Transactions;
16
+using StackExchange.Redis;
17
 
17
 
18
 namespace WebApplication1
18
 namespace WebApplication1
19
 {
19
 {
30
         public void ConfigureServices(IServiceCollection services)
30
         public void ConfigureServices(IServiceCollection services)
31
         {
31
         {
32
             services.AddMvc();
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
             services.AddLogging();
46
             services.AddLogging();
52
         }
47
         }
53
 
48
 
62
 
57
 
63
 
58
 
64
             #region HangFire
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
             var jobOptions = new BackgroundJobServerOptions
70
             var jobOptions = new BackgroundJobServerOptions
69
             {
71
             {
70
                 Queues = new[] { "test", "default" },//队列名称,只能为小写
72
                 Queues = new[] { "test", "default" },//队列名称,只能为小写
71
                 WorkerCount = Environment.ProcessorCount * 5, //并发任务数
73
                 WorkerCount = Environment.ProcessorCount * 5, //并发任务数
72
                 ServerName = "hangfire1",//服务器名称
74
                 ServerName = "hangfire1",//服务器名称
75
+
73
             };
76
             };
74
             //启动Hangfire服务
77
             //启动Hangfire服务
75
             app.UseHangfireServer(jobOptions);
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
             #endregion
80
             #endregion
84
 
81
 
85
             #region NLog
82
             #region NLog

+ 4 - 1
Demo/WebApplication1/WebApplication1.csproj

11
   <ItemGroup>
11
   <ItemGroup>
12
     <PackageReference Include="HangFire" Version="1.6.17" />
12
     <PackageReference Include="HangFire" Version="1.6.17" />
13
     <PackageReference Include="Hangfire.Autofac.Core" Version="1.0.1" />
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
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
16
     <PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
16
   </ItemGroup>
17
   </ItemGroup>
17
 
18
 
23
     <None Include="StaticConfig\nlog.config" />
24
     <None Include="StaticConfig\nlog.config" />
24
   </ItemGroup>
25
   </ItemGroup>
25
 
26
 
27
+  <ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="" /></VisualStudio></ProjectExtensions>
28
+
26
 </Project>
29
 </Project>

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

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

+ 4 - 0
Demo/WebApplication1/appsettings.json

11
         "Default": "Warning"
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
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7
   <PropertyGroup>
7
   <PropertyGroup>
8
     <TimeStampOfAssociatedLegacyPublishXmlFile />
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
   </PropertyGroup>
10
   </PropertyGroup>
11
 </Project>
11
 </Project>