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 基本滿足需求。

增加 package
<PackageReference Include="Hangfire.AspNetCore" Version="1.6.12" />
<PackageReference Include="Hangfire.Redis.StackExchange.StrongName" Version="1.7.0" />
<PackageReference Include="StackExchange.Redis.StrongName" Version="1.2.3" />
配置 Hangfire 服務
 /// <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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM