C# 使用HangFire
第一章:.net Framework 4.6 WebAPI 使用Hangfire
第二章:net 8使用hangfire实现库存同步任务
文章目录
- C# 使用HangFire
- 前言
- 项目源码
- 一、项目架构
- 二、项目服务介绍
- HangFire服务结构解析
- HangfireCollectionExtensions 类
- Model
- HangfireSettings
- HttpAuthInfo
- UserInfo
- appsetting.json
- NLog.config
- Program.cs
- StockChangeServers服务结构解析
- 项目内结构
- 结构介绍
- 项目用到的包
- 服务示例
- 示例Demo
- appsetting.config
- 总结
前言
在第一章中我介绍了如何在.net Framework 4.6 WebAPI 中使用Hangfire,实现Hangfire定时调用我们开发的API接口,但是在实际业务中调用接口往往需要处理耗时任务,如果还像第一章中的方式去调用的话就会出现请求超时的情况。今天我就以电商系统库存同步业务为例子给大家讲解如何使用hangfire调用耗时任务。
后续我也会把项目放在CSDN上供大家下载学习。
项目源码
下载项目源码
一、项目架构
项目采用微服务的方式开发,分为以下两个服务:
- HangFireServers(调用和管理后面两个服务)
- StockChangeServers(同步库存信息至电商平台)
- SkuServers(从电商平台拉取商品信息)
二、项目服务介绍
该服务用于定时调用库存同步服务。作为库存同步服务和商品SKU更新服务的管理。
HangFire服务结构解析
该服务中用到的包:
项目内结构:
HangfireCollectionExtensions 类
该类的主要作用是配置hangfire的监控面板 也就是这里的样式,还配置了用户的账户密码和Hangfire最大开启线程以及是否只读等信息。
using Hangfire;
using Hangfire.Console;
using Hangfire.Dashboard.BasicAuthorization;
using Hangfire.Heartbeat;
using Hangfire.Heartbeat.Server;
using Hangfire.HttpJob;
using Hangfire.Redis.StackExchange;
using Hangfire.Server;
using Hangfire.Tags;
using Hangfire.Tags.Redis.StackExchange;
using HangfireServers.Model;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using Spring.Core.TypeConversion;
using StackExchange.Redis;namespace HangfireServers.Extensions
{public static class HangfireCollectionExtensions{private const string HangfireSettingsKey = "Hangfire:HangfireSettings";private const string HttpJobOptionsKey = "Hangfire:HttpJobOptions";private const string HangfireConnectStringKey = "Hangfire:HangfireSettings:ConnectionString";private const string HangfireLangKey = "Hangfire:HttpJobOptions:Lang";public static IServiceCollection AddSelfHangfire(this IServiceCollection services, IConfiguration configuration){var hangfireSettings = configuration.GetSection(HangfireSettingsKey);var httpJobOptions = configuration.GetSection(HttpJobOptionsKey);services.Configure<HangfireSettings>(hangfireSettings);services.Configure<HangfireHttpJobOptions>(httpJobOptions);services.AddTransient<IBackgroundProcess, ProcessMonitor>();services.AddHangfire(globalConfiguration =>{services.ConfigurationHangfire(configuration, globalConfiguration);});services.AddHangfireServer((provider, config) =>{var settings = provider.GetService<IOptions<HangfireSettings>>()?.Value;ConfigFromEnv(settings ?? new HangfireSettings());var queues = settings?.JobQueues.Select(m => m.ToLower()).Distinct().ToList();var workerCount = Math.Max(Environment.ProcessorCount, settings.WorkerCount); //工作线程数,当前允许的最大线程,默认20config.ServerName = settings.ServerName;config.ServerTimeout = TimeSpan.FromMinutes(4);config.SchedulePollingInterval = TimeSpan.FromSeconds(1);//秒级任务需要配置短点,一般任务可以配置默认时间,默认15秒config.ShutdownTimeout = TimeSpan.FromMinutes(30); //超时时间config.Queues = queues?.ToArray(); //队列config.WorkerCount = workerCount;});return services;}public static void ConfigurationHangfire(this IServiceCollection services, IConfiguration configuration,IGlobalConfiguration globalConfiguration){var serverProvider = services.BuildServiceProvider();var langStr = configuration.GetSection(HangfireLangKey).Get<string>();var envLangStr = GetEnvConfig<string>("Lang");if (!string.IsNullOrEmpty(envLangStr)) langStr = envLangStr;if (!string.IsNullOrEmpty(langStr)){Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(langStr);}var hangfireSettings = serverProvider?.GetService<IOptions<HangfireSettings>>()?.Value;ConfigFromEnv(hangfireSettings??new HangfireSettings());var httpJobOptions = serverProvider?.GetService<IOptions<HangfireHttpJobOptions>>()?.Value;ConfigFromEnv(httpJobOptions??new HangfireHttpJobOptions());