【问题标题】:Is it possible to load balance the Hangfire Front End?是否可以对 Hangfire 前端进行负载平衡?
【发布时间】:2021-07-18 04:28:42
【问题描述】:

在我的用例中,我有两个位于负载平衡器后面的 IIS 服务器,并且我们的 ASP.NET Web 应用程序部署到这两个服务器上。

我能否在这两个实例上运行 Hangfire 服务器并将工作人员部署在其他服务器上?

【问题讨论】:

  • AFAIK Hangfire 服务器指的是工作人员部分,即运行作业的部分。您绝对可以将这些部署在您的前端和后端服务器上,只要它们可以访问 hangfire 存储。

标签: asp.net iis load-balancing hangfire


【解决方案1】:

您可以将仪表板部署到您的 IIS 服务器上,并将工作人员留在您的“其他”服务器中执行这项工作。在我们的环境中,我们将仪表板部署到 4 个负载平衡的服务器上,以查看作业和活动,而其他服务器则执行实际工作。

我有一个较旧的配置,为了让仪表板正常运行,我在App_Start 文件夹中添加了一个HangfireDashboard.cs 文件。这是您为 Hangfire 定义存储等的配置部分。您需要参考 Hangfire 软件包以及存储(mongodb、MySql、MsSQL 等)产品来访问hangfire 后端数据库。

这是我们所拥有的示例,检查用户表以查看用户是否有权查看(查看所有正在运行的作业)或编辑访问权(允许用户“开始”或“取消”作业)。

using System;
using System.Linq;
using System.Security.Principal;
using System.Web;
using Hangfire;
using Hangfire.Annotations;
using Hangfire.Dashboard;
using Hangfire.Mongo;
using Microsoft.Owin;
using MongoDB.Driver;
using Owin;

[assembly: OwinStartup(typeof(HangfireNamespace.HangfireDashboard))]

namespace HangfireNamespace
{
    public class HangfireDashboard
    {
        public void Configuration(IAppBuilder app)
        {
            // Set up Connection to the backend. We use MongoDB.
            GlobalConfiguration.Configuration.UseMongoStorage(GetMongoClientSettings(), "hangfire", GetMongoClientOptions());

            var hangfireAuthz = new HangFireAuthorizationFilter();
            var dashboardOptions = new DashboardOptions
            {
                DashboardTitle = "Hangfire Dashboard",
                AppPath = VirtualPathUtility.ToAbsolute("~"),
                Authorization = new[] { hangfireAuthz },
                IsReadOnlyFunc = (DashboardContext context) => !hangfireAuthz.IsUserAuthorizedToEditHangfireDashboard(context)
            };

            app.UseHangfireDashboard("/hangfire", dashboardOptions);
        }

        public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter
        {            
            public bool Authorize([NotNull] DashboardContext context)
            {
                string user = GetLoggedInUserID();
                var adminAuthz = _db.UserAccess.FirstOrDefault(
                       aa =>
                           aa.UserName.Equals(user) && aa.SubSystem.Equals("Hangfire") &&
                           (aa.UserPerms.Equals("View") || aa.UserPerms.Equals("Edit")));

                return adminAuthz != null;
            }

            public bool IsUserAuthorizedToEditHangfireDashboard([NotNull] DashboardContext context)
            {
                string user = GetLoggedInUserID();
                var adminAuthz = _db.UserAccess.FirstOrDefault(
                       aa =>
                           aa.UserName.Equals(user) && aa.SubSystem.Equals("Hangfire") &&
                           aa.UserPerms.Equals("Edit"));

                return adminAuthz != null;
            }
        }
    }
}

在此之后,我们只需向hangfire 添加一个href 即可访问我们需要的仪表板。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多