Asp.Net Core 集成 Hangfire 配置使用 Redis 存储
Hangfire 官方支持 MSSQL 与 Redis(Hangfire.Pro.Redis) 两种 ,由于我的数据库是 MYSQL ,粗略查询了一下文档,现在对 .NET Core 支持的并不够好,所有就选择了 Redis;当然也可以使用第三方来支持 PostgreSql,Mongo等
安装 Redis
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:chris-lea/redis-server
sudo apt-get update
sudo apt-get install redis-server
ps aux | grep redis
sudo service redis-server restart
sudo apt-get remove redis-server
redis-cli
#注释bind
# bind 127.0.0.1
#守护进程启动
daemonize yes
#保护模式[无密码模式设置为no]
protected-mode no
#设置密码
requirepass test
Mac 下安装 Redis Desktop Manager(官方 Mac 版只支持源码重编译)
#install brew cask
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null
#install Redis Desktop Manager
brew cask install rdm
Asp.Net Core 集成Hangfire
Hangfire.Pro 是对 Hangfire 的一个扩展,使用自己搭建的 Nuget 源,Hangfire.Pro.Redis 是其中的一个扩展 ;我这里是使用的 Hangfire.Redis.StackExchange 基本满足需求。
/// <summary> /// 启动类 /// </summary> public class Startup { /// <summary> /// 配置接口 /// </summary> public IConfigurationRoot Configuration { get; } /// <summary> /// Redis 服务 /// </summary> public static ConnectionMultiplexer Redis; /// <summary> /// 构造方法 /// </summary> /// <param name="env"></param> public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); Redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("Redis")); } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOptions(); //自定义的配置 services.Configure<DbSetting>(Configuration.GetSection("ConnectionStrings")); //返回大小写问题 services.AddMvc() .AddJsonOptions(option => option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()); //注入Hangfire服务 services.AddHangfire(config => config.UseRedisStorage(Redis)); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseHangfireServer(); app.UseHangfireDashboard("/hangfire", new DashboardOptions { Authorization = new[] { new HangfireDashboardAuthorizationFilter() } }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Hq}/{action=Index}/{id?}"); }); } }
https://github.com/marcoCasamento/Hangfire.Redis.StackExchange
http://docs.hangfire.io/en/latest/configuration/using-redis.html
https://github.com/uglide/RedisDesktopManager
https://www.hangfire.io/extensions.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述