zoukankan      html  css  js  c++  java
  • .Net Core定时调度hangfire:存储配置

    hangfire会将定时任务等信息存储起来,有内存存储、缓存存储和数据库存储三种方式。

    首先在nuget中安装适配.net core版本的库Hangfire.AspNetCore。

    一、内存存储

    在nuget中找到Hangfire.MemoryStorage进行安装。

    之后在Startup文件中添加如下即可:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddHangfire(t => t.UseMemoryStorage());
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseHangfireServer();
    }

     二、数据库存储

    数据库有很多种MySql、Oralce、SqlServer,不过SqlServer是hangfire的默认选择,所以以此为例在nuget安装对应的包Hangfire.SqlServer。

    其他的数据库只要找对应的安装包就行。

    支持的数据库为Microsoft SQL Server 2008R2(任何版本,包括 LocalDB)和更高版本。

    但是,仅适用于 Hangfire < 1.5.9:数据库不使用快照隔离级别,并且READ_COMMITED_SNAPSHOT选项(另一个名称是Is Read Committed Snapshot On是 disabled否则,某些后台作业可能不会被处理。

    具体在Startup文件中添加如下即可使用:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddHangfire(configuration => configuration
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)      // 设置数据兼容级别
        .UseSimpleAssemblyNameTypeSerializer()                          // 使用简单程序集名称类型序列化程序
        .UseRecommendedSerializerSettings()                             // 使用推荐的序列化设置
        .UseSqlServerStorage(Configuration.GetValue<string>("ConnectionStrings:BaseDb:ConnectionString"), new SqlServerStorageOptions   //数据库设置
        {
          TransactionIsolationLevel = IsolationLevel.ReadCommitted,   // 事务隔离级别。默认值为读提交。
          TransactionTimeout = TimeSpan.FromMinutes(1),               // 事务超时。默认为1分钟
          JobExpirationCheckInterval = TimeSpan.FromHours(1),         // 作业过期检查间隔(管理过期记录)。默认为1小时
          CountersAggregateInterval = TimeSpan.FromMinutes(5),        // 间隔到聚合计数器。默认为5分钟
          PrepareSchemaIfNecessary = true,                            // 如果设置为true,则创建数据库表。默认值为true
          CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),           // 命令批处理最大超时时间
          SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),       // 滑动超时时间
          QueuePollInterval = TimeSpan.Zero,                          // 作业队列轮询间隔。默认值为15秒
          UseRecommendedIsolationLevel = true,                        // 是否使用建议的隔离级别
          DisableGlobalLocks = true,                                  // 是否禁用全局锁,需要迁移到模式7
          DashboardJobListLimit = 50000,                              // 仪表板作业列表上限。默认值为50000 
        }));
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseHangfireServer();
    }

    启动之后因为我们设置了PrepareSchemaIfNecessary为true,所以会自动在数据库中创建数据表存储信息。

     给hangfire的数据库链接可以给予最小的权限,既只操作hangfire相关表的权限,以提高安全。

    三、Redis存储

    使用Redis 作业存储相比使用 SQL Server 存储能更快的处理作业。

    但是,官方的版本是收费的,有免费版HangFire.Redis.StackExchange。

    使用如下:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddHangfire(x =>
      {
        RedisStorageOptions options = new RedisStorageOptions()
        {
          Prefix = "hangfire:",   //键前缀
        };
        x.UseRedisStorage(Configuration.GetValue<string>("ConnectionStrings:Redis:ConnectionString"), options);
      });
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseHangfireServer();
    }

    查看redis可以发现,hangfire的信息已经加入到缓存中了

  • 相关阅读:
    3.5---用栈实现队列(CC150)
    3.3---集合栈(CC150)
    3.2---最小栈(CC150)
    3.1---一个数组实现三个栈(CC150)
    2.7---判断链表是否是回文(CC150)
    SpringCloud实战5-Feign声明式服务调用
    Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
    Spring Cloud Eureka 自我保护机制
    Spring Cloud Edgware新特性之七:可选的EnableDiscoveryClient注解
    Spring Cloud 声明式服务调用 Feign
  • 原文地址:https://www.cnblogs.com/xwc1996/p/14978794.html
Copyright © 2011-2022 走看看